20165104-java First pair Programming exercise

Source: Internet
Author: User

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
    • Randomly generated topics
      • Generate the number of topics according to user needs input
      • Automatic generation of primary arithmetic topics (add, subtract, multiply, divide)
      • Support for integers and true fractions
      • Support for multiple operators (such as generating a topic with 100 operators)
    • Statistics correct rate
    • can generate problems multiple times

    • Expansion requirements
      • Topic to go Heavy
      • Multi-lingual support
      • Process the build problem and output to a file
      • Read and award questions from a file after completing the question
  • 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

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.