Personal Job 1-four calculation question generation programs (based on the console), four calculation Console

Source: Internet
Author: User
Tags integer division

Personal Job 1-four calculation question generation programs (based on the console), four calculation Console

Personal Code: HuanWong

A. Requirement Analysis:

It is a simple small task for most parents to come out with a four-Rule calculation. However, it is too cumbersome and time-consuming to repeat the same task every day. Therefore, it is necessary to design a small program to help parents relieve the burden while allowing them to exercise.

B. Functional Design:

According to the requirements of the questions and considering the actual situation, the program should have the following functions:

C. Design implementation:

Considering the knowledge level of primary school students, the Integer Operation part can be randomly generated with two zeros ~ 100 of numbers to perform random arithmetic operations. For a real score, a createFraction method is required to randomly generate two integers as the denominator of the numerator. To ensure that the score is true, when the numerator is large and the elimination numerator is zero and the two numbers are equal, the value is 1. The GCD method and CLM method are used to obtain the maximum and least common multiples of two numbers. The GCD method is used to calculate the maximum common divisor of the two numbers by the moving phase division, LCM calculates the least common multiple by using the "maximum common approx. * least common multiple = product of two numbers. The four arithmetic operations of scores follow the calculation rules of scores and convert and simplify them using the maximum common number and the minimum common number. In order to test the answers to each question, the calculation result is converted to string storage. According to the Conventions, the integer division is based on rounding to retain two decimal places.

Considering the computing difficulty, the ratio of each integer operation to the score calculation question is. The score is one point and the incorrect question is marked.

D. Code Description:

1. createFraction method:

Public static int [] createFraction () {// generate two numbers to construct a real score. The first number in the array is the int [] num = new int [2]; int num1 = (int) (Math. random () * 100 + 1); int num2 = (int) (Math. random () * 100 + 1); // avoid zero denominator (to be improved) if (num1! = Num2) {if (num1 <num2) {// avoid the constructed score value being 1 num [0] = num1; num [1] = num2; return num ;} else {num [0] = num2; num [1] = num1; return num ;}} else createFraction (); // call the return num method recursively if the condition is not met ;}

2. GCD and LCM methods:

Public static int GCD (int m, int n) {// calculate the maximum common divisor of the moving phase division while (true) {if (m = m % n) = 0) return n; if (n = n % m) = 0) return m ;}} public static int LCM (int m, int n) {return m * n/GCD (m, n); // maximum common approx. * minimum public multiple = returns the product of two numbers to the minimum public multiple}

3. Main function:

Public static void main (String [] args) {ArrayList <String> List = new ArrayList <String> (); // question storage ArrayList <String> Answers = new ArrayList <String> (); // refer to the answer storage System. out. println ("Enter the number of questions you want:"); // the required number is required. Required SC = new limit (System. in); int n = SC. nextInt (); int m = n/3; // score question count accounts for 1/3 int num1; int num2; for (int I = 0; I <(n-m ); I ++) {// start random integer question type num1 = (int) (Math. random () * 100); num2 = (int) (Math. random () * 10 0); // generate two random numbers if (num2! = 0) {// exclusion 0 has an impact on Division (to be improved) int op = (int) (Math. random () * 4 + 1); // The calculation type is randomly determined. 1234 corresponds to addition, subtraction, multiplication, division, if (op = 1) {List. add (num1 + "+" + num2 + "="); Answers. add (num1 + num2 + "");} if (op = 2) {if (num1> num2) {// pupils have limited knowledge to avoid negative result List. add (num1 + "-" + num2 + "="); Answers. add (num1-num2 + "");} else {List. add (num2 + "-" + num1 + "="); Answers. add (num2-num1 + "") ;}}if (op = 3) {List. add (num1 + "×" + num2 + "="); Answers. add (num1 * num2 + "");} if (op = 4 ){ List. add (num1 + "numeric" + num2 + "="); BigDecimal a = new BigDecimal (float) num1/num2); float ans =. setScale (2, BigDecimal. ROUND_HALF_UP ). floatValue (); // retain two digits, rounded to Answers. add (ans + "") ;}} else {// 0 is not Division (to be improved) int op = (int) (Math. random () * 3 + 1); if (op = 1) {List. add (num1 + "+" + num2 + "="); Answers. add (num1 + num2 + "");} if (op = 2) {if (num1> num2) {// avoid negative result List. add (num1 + "-" + num2 + "="); Answers. add (num1-num2 + "");} Else {List. add (num2 + "-" + num1 + "="); Answers. add (num2-num1 + "") ;}}if (op = 3) {List. add (num1 + "×" + num2 + "="); Answers. add (num1 * num2 + "") ;}}for (int j = 0; j <m; j ++) {int [] fa1 = createFraction (); int [] fa2 = createFraction (); int gbs = LCM (fa1 [1], fa2 [1]); if (fa2 [0]! = 0) {int op = (int) (Math. random () * 4 + 1); if (op = 1) {List. add ("(" + fa1 [0] + "/" + fa1 [1] + ") + ("+ fa2 [0] +"/"+ fa2 [1] +") = "); int sum = gbs/fa1 [1] * fa1 [0] + (gbs/fa2 [1] * fa2 [0]); int gys1 = GCD (sum, gbs ); answers. add (sum/gys1 + "/" + gbs/gys1 + ""); // simplify the result and store} if (op = 2) {if (fa1 [0]/fa1 [1]> fa2 [0]/fa2 [1]) {// avoid negative result List. add ("(" + fa1 [0] + "/" + fa1 [1] + ") -("+ fa2 [0] +"/"+ fa2 [1] +") = "); int cha = gbs/fa1 [1] * fa1 [0]-(gbs/fa2 [1] * fa2 [0]); int gys2 = GCD (cha, gbs); Answers. add (cha/gys2 + "/" + gbs/gys2 + "");} else {List. add ("(" + fa2 [0] + "/" + fa2 [1] + ") + ("+ fa1 [0] +"/"+ fa1 [1] +") = "); int cha = gbs/fa2 [1] * fa2 [0]-(gbs/fa1 [1] * fa1 [0]); int gys2 = GCD (cha, gbs ); answers. add (cha/gys2 + "/" + gbs/gys2 + ") ;}} if (op = 3) {List. add ("(" + fa1 [0] + "/" + fa1 [1] + ") × ("+ fa2 [0] +"/"+ fa2 [1] +") = "); int a = fa1 [0] * fa2 [0]; int B = fa1 [1] * fa2 [1]; int gys3 = GCD (a, B); Answer S. add (a/gys3 + "/" + B/gys3 + ");} if (op = 4) {List. add ("(" + fa1 [0] + "/" + fa1 [1] + ") forward ("+ fa2 [0] +"/"+ fa2 [1] +") = "); int c = fa1 [0] * fa2 [1]; int d = fa2 [0] * fa1 [1]; // convert to multiplication int gys4 = GCD (c, d); Answers. add (c/gys4 + "/" + d/gys4 + "") ;}} else {int op = (int) (Math. random () * 3 + 1); if (op = 1) {List. add ("(" + fa1 [0] + "/" + fa1 [1] + ") + ("+ fa2 [0] +"/"+ fa2 [1] +") = "); int sum = gbs/fa1 [1] * fa1 [0] + (gbs/fa2 [1] * fa2 [0]); int gys1 = GCD (sum, gb S); Answers. add (sum/gys1 + "/" + gbs/gys1 + "");} if (op = 2) {if (fa1 [0]/fa1 [1]> fa2 [0]/fa2 [1]) {// avoid negative result List. add ("(" + fa1 [0] + "/" + fa1 [1] + ") -("+ fa2 [0] +"/"+ fa2 [1] +") = "); int cha = gbs/fa1 [1] * fa1 [0]-(gbs/fa2 [1] * fa2 [0]); int gys2 = GCD (cha, gbs ); answers. add (cha/gys2 + "/" + gbs/gys2 + "");} else {List. add ("(" + fa2 [0] + "/" + fa2 [1] + ") + ("+ fa1 [0] +"/"+ fa1 [1] +") = "); int cha = gbs/fa2 [1] * fa2 [0]-(gbs/fa1 [1] * fa1 [0]); in T gys2 = GCD (cha, gbs); Answers. add (cha/gys2 + "/" + gbs/gys2 + ") ;}} if (op = 3) {List. add ("(" + fa1 [0] + "/" + fa1 [1] + ") × ("+ fa2 [0] +"/"+ fa2 [1] +") = "); int a = fa1 [0] * fa2 [0]; int B = fa1 [1] * fa2 [1]; int gys3 = GCD (a, B); Answers. add (a/gys3 + "/" + B/gys3 + "") ;}} int qnum = 0; int count = 0; for (String string: List) {// print the question qnum ++; count ++; if (count> 3) {System. out. println (); count = 1;} System. out. print ("[" + qnum + "]" + String + "\ t");} System. out. println (); radians = new partition (System. in); ArrayList <String> anslist = new ArrayList <> (); String str = ""; System. out. println ("Enter the answer, keep two decimal places in the division result, and enter" OK "to end:"); while (! Str. equals ("OK") {str = ans. nextLine (); anslist. add (str) ;}int correct = 0; int t = 0; System. out. println ("[question number] Refer to answer \ t submit result"); System. out. println (); for (int x = 0; x <Answers. size (); x ++) {// test the answer and count if (Answers. get (x ). equals (anslist. get (x) {correct ++; t = x + 1; System. out. println ("[" + t + "]" + "\ t" + Answers. get (x) + "\ t" + "\ t" + anslist. get (x);} else {t = x + 1; System. err. println ("[" + t + "]" + "\ t" + Answers. get (x) + "\ T" + "\ t" + anslist. get (x) + "!!!! ") ;}} System. out. println ("exercise statistics:"); System. out. println ("score:" + correct + "Total" + n + "score, correct rate:" + (float) correct/n * 100 + "%, !!!! Is the correct question. ");}
Main

E. Test run:

F. PSP

PSP2.1 Personal Software Process Stages Time Senior Student Time
Planning Plan 30 min 20 min
· Estimate Estimated time required for this task 5 h 7 h
Development Development --- ---
· Analysis Demand Analysis (including learning new technologies) 5 min 8 min
· Design Spec Generate design document 6 min 6 min
· Design Review Design Review 8 min 8 min
· Coding Standard Code Specification --- ---
· Design Specific design 40 min 30 min
· Coding Code 3 h 3.5 h
· Code Review Code Review 15 min 15 min
· Test Test (self-test, code modification, and modification submission) 30 min 30 min
Reporting Report 2 h 1.5 h
· Test report 30 min 30 min
· Computing workload 5 min 8 min
· And propose a process improvement plan 8 min 12 min

H. Summary:

It is not appropriate to spend a lot of time re-understanding the Code due to lack of programming practices. Some of the procedures are not very reasonable, and there is still much room for improvement. Although the program is small, the structure is not clear enough, and the logic is not clear enough. This may cause reading difficulties. In future development, you should pay attention to clarifying the structure in the design phase.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.