Java First pair programming exercise
- Study Notes
- infix expression converted to suffix expression
- If a number is encountered, we will output it directly.
- If a non-number is encountered, if the stack is empty or the symbol is an opening parenthesis or the top element of the stack is parentheses, directly into the stack.
- If a closing parenthesis is encountered, the stack is persisted and the symbol is output until the top of the stack is left parenthesis, then the left parenthesis is stacked (note that the left parenthesis is only out of the stack, not the output), and the closing parenthesis is not stacked.
- If an operation symbol is encountered and the stack is not empty, the top element of the stack is viewed, and if the top element of the stack has an operation priority greater than or equal to the operation symbol, the stack is persisted until the top element of the stack is less than the operator. Finally, the element is put into the stack.
- If we read the end of the input, it pops up all the elements in the stack sequentially.
- The use of generating random numbers
Random ran = new Random();Random number method to create an object
int B = ran.nextInt(2);Generate a random number within 0-1
- Demand analysis
- Design ideas
- The first experiment is to complete a calculator function, can achieve a simple + 、-、 *,/arithmetic
- Implement a multi-operator, programmed into four classes to implement integer arithmetic, true fractional operation, correct judgment result and calculate correct rate
- Using JUnit to detect illegal input
- Design a main class to generate random numbers, generate problems, and determine the correct rate
- Designing test classes, using JUnit to test the arithmetic of integer classes and fractional classes
UML diagram:
Core code and comments
public class Calculate {public static void main (string[] args) {NumberFormat number = Numberformat.getpercenti Nstance (); Random ran = new random (); Scanner scan = new Scanner (system.in); while (true) {System.out.print ("Please enter the number of topics you need to generate:"); int A = Scan.nextint (); A: The number of topics for (int i = 0; i < A; i++) {int B = Ran.nextint (2); Generate random number 0/1, randomly pick integer or fractional operation int C = Ran.nextint (4); Generate random number 0-3, randomly pick 4 kinds of operators intnumber in1 = Intnumber.obj (); Integer operation intnumber in2 = Intnumber.obj (); Fraction score1 = Fraction.obj (); Fractional operation fraction score2 = Fraction.obj (); if (B = = 0) {switch (C) {//integer subtraction operation case 0: int num = In1.add (in2); StringNUM1 = "" + num; int n = scan.nextint (); Judgement.judge (n = = num, num1);
Test code
Run results
Pair partner Evaluation
Reason, two dishes chicken, this difficult programming problem really let us burn ....
But
The days still have to go on, the code still knock, from a person asked to become now two people to ask students to learn, to tell the truth, indeed two people ask together, do not know how to communicate with each other, learn to be happy a bit. Thank you, my partner.
20165104-java First pair Programming exercise