2017-2018-2 1723 "Java Programming" course pair programming exercises-arithmetic-prep phase

Source: Internet
Author: User
Tags arithmetic


2017-2018-2 1723 "Java Programming" course Pairing programming exercises-arithmetic-preparation phase

After a person struggles for nearly half a semester, finally ushered in our first team collaboration coding, that is, our first pair of programming exercises-arithmetic. Obviously, he is the test of our knowledge of the previous level of mastery, and test our ability, since it is a pair of programming exercises, there must be difficulties, but we will meet the difficult to solve problems together, because "Unity is POWER!!!" ”

Pair programming of comrade-in-arms


20172316 Zhao Chen: Responsible for the programming of Integer question;
20172319 Tang Caiming: Responsible for the programming of the score question;
Me: 20172329 Wenbin Wang: Responsible for calculating and topic de-writing;
The test was tested separately, and the group test was carried out by Zhao Hu Chen.
Everyone is the pilot, is each other's pilot, team, the Division of labor is a clear part, but the collective strength of the crystallization is the greatest!!!


See the idea of the topic


1, the beginning of the calculation of the idea:
First, when we first get this topic, the first thing to think about is subtraction's entire computational logic, because there was no understanding of the suffix notation, so I have been thinking about how to start the calculation with infix expression, on the one hand, the question of whether the output should be a string type or data type, and then think about it, Each have their own advantages and disadvantages, for example, when the calculation is true, the number is very convenient, but when it comes to the calculation, encountered the "+", "-", "*", "/", a few symbols on my current level, there is no way to a good solution, then thought, if the few operational symbols to change, Turn it into a few numbers to represent them, not on it, finally there is a problem, if the "0" to represent the "+", there is a problem, because "+" is a string, the assignment of its value also has to be forced to convert, for the process itself such is actually unfavorable, there are certain problems, so, first put the calculation problem, Start thinking, how to make a quiz;



2. Start the quiz:
At the beginning, or with the topic of the topic, considering the complexity of the "if-else" statement, we chose the "Swich statement", because, this can be human interaction, on the one hand can achieve the input we need to a number of orders, you can complete the order of the question; the idea is that, first, use the assignment to the symbol , for the subtraction four symbols for the assignment of 0123, so that in order to produce a 0~4 (left closed right open) of a random number to achieve the randomness of the string, the output of the symbol to a character object to assign values, and then start adding numbers inside, how to solve the order of control, on the one hand, The use of the Swich statement is convenient, secondly, the use of the cumulative "+ =" output, will produce according to our needs of the topic; On the other hand, we need to output the number of questions we typed to the output, we think of the use of arrays and while statements, so that, according to conditions, output array, We can achieve a result we want, which is an ideal model.


protected int a;
    protected String s1 = "", str = "";

    public Compute2 () {
        Scanner scan = new Scanner (System.in);
        System.out.println ("How many calculations?");
        int n = scan.nextInt ();
        Scanner qqq = new Scanner (System.in);
        System.out.println ("How many questions are generated?");
        int q = qqq.nextInt ();
        Object [] p = new Object [q];
        for (int index = 0; index <q; index ++) {
            int a1 = (int) (Math.random () * 100);
            int a2 = (int) (Math.random () * 100);
            int t1 = (int) (Math.random () * 4);
            switch (t1) {
                case 0: {
                    s1 = "+";
                    break;
                }
                case 1: {
                    s1 = "-";
                    break;
                }
                case 2: {
                    s1 = "*";
                    break;
                }
                case 3: {
                    s1 = "/";
                }
            }
            for (int i = 0; i <n-1; i ++) {
                int a = (int) (Math.random () * 100);
                int t = (int) (Math.random () * 4);
                String s = "";
                switch (t) {
                    case 0: {
                        s = "+";
                        break;
                    }
                    case 1: {
                        s = "-";
                        break;
                    }
                    case 2: {
                        s = "*";
                        break;
                    }
                    case 3: {
                        s = "/";
                    }
                }
                str + = a + s;

            }

            str + = a1 + s1 + a2;
            p [index] = str;


3, learning the change from the prefix expression to the suffix expression:
in class, learn to understand the suffix expression, this expression is convenient for us to write the calculation code, the use of the concept of "stack", in the process of thinking operations to simplify a lot of steps;
in the use of the stack to solve the problem, there is a problem: if it is a two-digit, how to solve, this problem in one day of the evening study, seniors gave us this question to explain, using Stringtokenzer method for the separation of strings, This method can be two numbers or three or even more than three characters to divide, so that they become a whole, so that the convenience of our calculations, we can solve 1234+ is not 123+4 but 12+34 problem, thank seniors.


public String infixToSuffix () {
                Stack <Character> s = new Stack <Character> ();
               String suffix = "";
        int length = str.length (); for (int i = 0; i <length; i ++) {
            char temp; // temporary character variable
                        char ch = str.charAt (i);
            switch (ch) {
                               case '':
                    break;
                               case '(':
                    s.push (ch);
                    break;
                               case '+':
                case '-':
                    while (s.size ()! = 0) {
                        temp = s.pop ();
                        if (temp == '(') {
                                                       s.push ('(');
                            break;
                        }
                        suffix + = temp;
                    }
                                       s.push (ch);
                    break;
                                case '*':
                case '/':
                    while (s.size ()! = 0) {
                        temp = s.pop ();
                                                if (temp == '+' || temp == '-' || temp == '(') {
                            s.push (temp);
                            break;
                        } else {
                            suffix + = temp;
                        }
                    }
                                       s.push (ch);
                    break;
                               case ')':
                                       while (! s.isEmpty ()) {
                        temp = s.pop ();
                        if (temp == '(') {
                            break;
                        } else {
                            suffix + = temp;
                        }
                    }
                    break;
                               default:
                    suffix + = ch;
                    break;
            }
        }
               while (s.size ()! = 0) {
            suffix + = s.pop ();
        } return suffix;


4, in the process of the issue of the problem of thinking:
How to go heavy, on the one hand, we think of as long as the results are the same is the same, and later, the discovery is not so simple, because, in the teacher's blog read:


The problem of the generation of a program run can not be repeated, that is, any two topics cannot be transformed into the same topic by means of a finite exchange + and an arithmetic expression around X. For example, 23 + 45 = and 45 + 23 = are repetitive topics, 6x8 = and 8x6 = are also repetitive topics. The two topics of 2+1 and 1+2+3 are repetitive, because the + is left-associative, and 1+2+3 is equivalent to (1+2) +3, which is the total (1+2), which is (2+1). However, 1+2+3 and 3+2+1 are two non-repetitive problems, because 1+2+3 is equivalent to (1+2) +3, and 3+2+1 is equivalent to (3+2) +1, and they cannot be changed to the same subject by a finite exchange.


When you see such a request, you begin to think;



5, calculation problem: (I am responsible for the part of this writing)
When meeting the calculation, the main rely on the use of the suffix expression method, the use of stacks to calculate, the approximate idea is as follows:


public Integer suffixToArithmetic() {
               Pattern pattern = Pattern.compile("\\d+||(\\d+\\.\\d+)");
               String[] strings = str.split("");
        Stack<Integer> stack = new Stack<Integer>();
        for (int i = 0; i < strings.length; i++) {
                       if (strings[i].equals("")) {
                continue;
            }
                        if (pattern.matcher(strings[i]).matches()) {
                stack.push((int) Double.parseDouble(strings[i]));
            }
                       else {
                             double y = stack.pop();
                double x = stack.pop();
                               stack.push((int) calculate(x, y, strings[i]));
            }
        }
             return stack.pop();
    }
    private  double calculate(double x, double y, String string) {

               if (string.trim().equals("+")) {
            return x + y;
        }
        if (string.trim().equals("-")) {
            return x - y;
        }
        if (string.trim().equals("*")) {
            return x * y;
        }
        if (string.trim().equals("/")) {
            return x / y;
        }
        return (double) 0;
    }
Test results





Turn suffix:


Some of the problems encountered


First question:
How to solve the problem of how to add parentheses, because the position of the parentheses of uncertainty limits how we choose, for example, if there is a mix of addition and multiplication, the parentheses should choose to add to the location of the addition?
Solution:
We want to bind the addition and parentheses together, and then use the Swich statement to determine when you don't need to add parentheses around the addition subtraction, and when not.



Second question:
Because we have written the code first, and then through the code to draw UML diagram, so now there is a problem, because some code naming is different, so there are some problems, (this is our group's mistakes, the next will be corrected)
Solution:
You are now summarizing, and then you intend to unify the encoding format for the UML diagram, and then modify the respective code.


Mutual evaluation


20172316 Zhao Chen
Xiao Zhao classmate began to write code of the large, in the release of the task of the second day, because I and the old Tang classmate in a continuous two days have late class, so at the beginning, Xiao Zhao classmate of the credit, and he is our team programming ability the strongest, so, we will be to him on par.
20172319 Tang Caiming
Old Tang classmate assigned our task, and also took up the team of the big flag, for each other cheer.
Everybody Cheer!!


UML diagram




Psp
PSP2.1 Personal software Process Stages Estimated time-consuming (minutes) actual time elapsed (minutes)
Planning Plan 60
Estimate Estimate how long this task will take 100
Development Development 800
Analysis Demand analysis (including learning new technologies) 100
Coding Standard Code specification (to develop appropriate specifications for current development) 30
Design UML Design Project UML class diagram 50
Coding Specific code 30
Code Review Code review 50
Test Test (self-test, modify code, commit changes) 30
Size Measurement Calculation effort (Actual time 30
Postmortem & Process Improvement Plan Summarize afterwards and propose process improvement plan 30
Total 1240
reference

Java stack application 2----------infix expression to postfix expression calculation Java implementation
Subtraction operation for complex numbers with Java
Inverse Polish expression
expression compute java suffix expression
Java implements infix expression to postfix expression and evaluates the result

2017-2018-2 1723 "Java Programming" course pair programming exercises-arithmetic-prep phase

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.