2017-2018-2 20172310 "Java Programming" Course Pair Programming Exercise_Four Operations_First Week

2017-2018-2 20172310 "Java Programming" Course Pair Programming Exercise_Four Operations_First Week

Pairing friends:

Requirements analysis (describe your own understanding of the requirements, and the possibility of subsequent expansion)

    • A certain amount of questions can be automatically generated according to the user's needs, and the level of the questions can be changed.
      Such as: 2 + 5 =
      2+5 3=
      (2+5)
      3=
      (2+5)*3/2=
      These are the questions of different levels. (The number of operands and operators is different, and the advanced questions contain different levels of operations)
    • parentheses are supported
    • Can be used independently (can realize the function of writing test classes to generate questions independently)
    • Question operation (judgment question), can be used independently
    • Convert infix expressions to postfix expressions and calculate
    • Determine whether the user's answer is right or wrong, and output the correct result
    • True Score
    • Can be used independently
    • Implement fractional calculations
    • Subsequent expansion: Complete the expression with random parentheses, and de-duplicate the title.

Design ideas (while outputting UML class diagrams)

  1. When thinking about the first question, I originally planned to create only one class, and design three methods in this class to get three different levels of questions, but then I have to keep creating methods, and in the method You also need to keep initializing different Random objects. At this time, the teammates proposed that the requirement is to randomly generate topics of different levels, which is too wasteful, and it is impossible to determine how many operators and operands there are according to actual needs. It is also prone to errors.

At this time, I thought about the inheritance that I learned last week. Since every method is indispensable for operators and operands, why not create a parent class and write these in the parent class, so we chose the second a method.

  • Here are some key codes at this stage
    public class Expressions2 extends Issue { String num;// protected String Expression;// String mount; String ope;

    //我现在需要一个随机数,来表示题目等级,然后随机确定题目的等级

    public String getStage()
    {
    num = getRandomNumber();
    Expression = num +" ";

    Random stsge = new Random();
    int a = stsge.nextInt(4);
    
    for (int b =0 ;b <= a;b++)
    {
        ope = getOperator();
        Expression += ope + " ";
        num = getRandomNumber();
        Expression += num + " ";
    }
    
    return Expression;

    }

    }

2. The infix expression is converted into a postfix expression expression, and the result is calculated.
The stack is new knowledge learned in the process of solving this problem.

Lists, pairs of columns, and stacks are all linear data structures. A stack is similar to a queue, except that elements of the stack enter and move out of the stack at the same end of the stack, and the last
element to enter the stack is the first to move out of the stack, like a stack of plates or stacks in a cupboard A stack of hay in the barn. A stack is a linear data structure that manages data in a last-in, first-out manner.

According to the principle of stack explained by the teacher, I sorted out the idea of ​​​​turning an infix expression into a postfix expression, for example:
9+(3-1) 3+6/2, converted to a postfix expression
1. Go to 9 and output 9. At this time, the screen displays 9, the stack is empty
. 2. When the + sign is encountered, the stack is empty at this time, and directly into the stack. At this time, the screen displays 9, and the stack element has only one + sign.
3. When encountering a left bracket, directly Push into the stack, the screen displays 9 at this time, the stack element is + (
4, encounters 3, outputs 3, at this time the screen displays 9 3, the stack element is + (
5, encounters a - sign, the top element of the stack is (, pushes the stack , at this time the screen displays 9 3, the stack element is + (-
6, encounter 1, output 1, at this time the screen displays 9 3 1, the stack element is + (-
7, encounter), continue to pop the stack until the top of the stack is (, and (pops but does not output, and) does not push onto the stack. At this time, the screen displays 9 3 1 -, and the stack element is +
8. When encountered
, the top element of the stack is +, and the priority is greater than the top element of the stack. , into the stack, at this time, the screen displays 9 3 1 -, the stack element is +
9, encounters 3, outputs 3, at this time the screen displays 9 3 1 - 3, the stack element is +

10, encounters +, at this time the stack The top element is , the priority is greater than or equal to +, pop the stack and output, then the top element of the stack is +, the priority is still greater than or equal to +, pop the stack and output, at this time the stack is empty, + is pushed into the stack, and
the screen displays 9 3 1 - 3
+, stack element is +
11, encounter 6, output 6. At this time, the screen displays 9 3 1 - 3 * + 6, stack element is +
12, encounter /, priority is greater than the top element of the stack, push into the stack, At this time, the screen displays 9 3 1 - 3 * + 6, the stack element is + /
13, encounters 2, and outputs 2, then the screen displays 9 3 1 - 3 * + 6 2
14. At this point, all the inputs are read, and the elements in the stack are popped out of the stack until empty. Finally, the screen displays 9 3 1 - 3 * + 6 2 / +

  • The code at this stage (there may be some flaws, and will continue to be modified in the future)
    public class Transvertor {
    private int num1, num2, value1, value2, number;
    private Stack stack1 = new Stack();
    private String result1;
    private int[][ ] array = {{0, 0, 0, 0, 0},
    {0, 1, 1, -1, -1},
    {0, 1, 1, -1, -1},
    {0, 1, 1 , 1, 1},
    {0, 1, 1, 1, 1}};

    public Transvertor() {
    num1 = 0;
    num2 = 0;
    value1 = 0;
    value2 = 0;
    result1=" ";
    
    }
    
    
    public String getAnswer(String Expression) { 
    
    StringTokenizer st = new StringTokenizer(Expression);
    while (st.hasMoreTokens()) {
        String A = st.nextToken();
    
        if (A.equals("-")  || A.equals("+") || A.equals("×") || A.equals("÷")) {
            switch (A) {
                case "+":
                number = 1;
                    break;
                case "-":
                    number = 2;
                    break;
                case "×":
                    number = 3;
                    break;
                case "÷":
                    number = 4;
                    break;
            }
    
            if (stack1.empty()) {
    
                stack1.push(A);
                num1 = number;
                value1++;
    
            } else {
    
                num2 = number;
    
                if (array[num1][num2] <= 0) {
                    stack1.push(A);
                    value1 += 1;
                } else {
                    result1 += stack1.pop()+" ";
                    stack1.push(A);
                }
            }
        }
        else
            result1 += A + " ";
    
    }
    for (int y = 0; y < value1; y++)
        result1 += stack1.pop()+" ";
    
    return result1;

    }

    public String getResult()
    {
    return result1;
    }
    }

3. The realization of the true score is only a simple idea at this stage, and the code has not been written yet. ٩(๑❛ᴗ❛๑)۶

Class Diagram

Notes:

  • The Fraction class is intended to generate a true fraction and can be used independently.
  • The Issue class generates random operands and operators, and is the parent class of the Expressions class. Expressions is used to generate an expression containing a true score but without parentheses at this stage.
  • The Transvertor class is the key and contains two methods, one converts the infix expression to the postfix expression, and the other calculates the result according to the postfix expression.
  • The Questions class is instantiated.
  • The Judgement class is used to test whether the user's answer is correct, and count the correct number to calculate the correct rate

Comment on paired friends

  • Comments on teammates:
    • The ability to write code is very strong, and the logic of the code is clear and simple.
    • The flexibility is very strong (〃'▽'〃), just like when we convert an infix expression to a postfix expression, we need to judge whether it is an operator or an operand. The teacher's advice is to use the ASCII character set. To judge whether it is within the range of 48-57, but we already have a score when we generate the expression, so it is more troublesome to judge in this way, so the teammates tactfully changed the way of thinking-judging the operator directly. (.^▽^) Awesome.
    • However, because the idea is relatively simple, the code is not concise enough, and it can continue to be improved in the later stage.
    • There are some small aspects that have not been considered clearly, and there are some small mistakes.
  • In the early stage, we planned to complete all the tasks together, but because of the difference in time, and although it is effective to discuss together, the place where there
    are should be the focus of discussion, and everyone has different ideas, we It is necessary to rely on a certain amount of our own strength to complete some things, instead of thinking for ourselves when we encounter problems in the whole process.
    In order to be more efficient and team members can show their abilities, we have adopted the method of division of labor and cooperation.
    When generating expressions, I mainly write the code. The conversion of infix expressions into postfix expressions is mainly completed by my teammates. Of course, we all accept each other's opinions,
    and modify and debug the code together, so that Complete our self-learning tasks more efficiently

Estimated completion time for each stage of the project

PSP2.1 Personal Software Process Stages Estimated time (hours) Actual time (hours)
Planning plan 1 5/6
Estimate Estimate how long this task will take 0.5
Development develop 16
Analysis Requirements analysis (including learning new technologies) 4
Coding Standard Code Specifications (to develop appropriate specifications for current development) 0.5 0.5
Design UML Design project UML class diagram 1
Coding specific code 18
Code Review code review 2
Test Testing (self-testing, modifying code, committing changes) 2
Size Measurement Computational workload (actual time) 0.5
Postmortem & Process Improvement Plan Post-event summary, and propose a process improvement plan 3
total 48.5

References

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325136361&siteId=291194637