20165203 Four operations (second stage) summary

20165203 Four operations (second stage) summary

1. Demand analysis

1. Input the number of generated questions through the command line, and randomly generate questions.
2. The generated questions are not repeated.
3. The generated four arithmetic problems support integers and multiple operators.
4. The generated four arithmetic questions support true scores.
5. Be able to count the correct number according to the answering situation. Able to count the correct number according to the answer.
6. It can output the correct rate according to the answering situation.

2. Design ideas

This week's new features are mainly on the ability to support true fractions in arithmetic problems, so the design is mainly around this. Add a class, this class is used to determine whether it is a true score. If it is a true score, enter the calculation class of the true score. If it is not a true fraction, enter the calculation class of integers. In addition, we have added a function that can output the correct rate. This is to add a variable whose value is divided by the number of correct questions divided by the total number of questions.

UML graphics

3. Core code

1. Calculation of each operation value:


public class RationalNumber {
    private int numerator, denominator;

    public RationalNumber(int numer, int denom) {
        if (denom == 0)
            denom = 1;
        if (denom < 0) {
            numer = numer * -1;
            denom = denom * -1;
        }

        numerator = numer;
        denominator = denom;
        reduce();
    }

    public int getNumerator() {
        return numerator;
    }

    public int getDenominator() {
        return denominator;
    }

    public RationalNumber reciprocal() {
        return new RationalNumber(denominator, numerator);
    }

    public RationalNumber add(RationalNumber op2) {
        int commonDenominator = denominator * op2.getDenominator();
        int numerator1 = numerator * op2.getDenominator();
        int numerator2 = op2.getNumerator() * denominator;
        int sum = numerator1 + numerator2;

        return new RationalNumber(sum, commonDenominator);
    }

    public RationalNumber subtract(RationalNumber op2) {
        int commonDenominator = denominator * op2.getDenominator();
        int numerator1 = numerator * op2.getDenominator();
        int numerator2 = op2.getNumerator() * denominator;
        int difference = numerator1 - numerator2;

        return new RationalNumber(difference, commonDenominator);
    }

    public RationalNumber multiply(RationalNumber op2) {
        int numer = numerator * op2.getNumerator();
        int demon = denominator * op2.getDenominator();

        return new RationalNumber(numer, demon);
    }

    public RationalNumber divide(RationalNumber op2) {
        return multiply(op2.reciprocal());
    }

    public boolean isLike(RationalNumber op2) {
        return (numerator == op2.getNumerator() && denominator == op2.getDenominator());
    }

    public String toString() {
        String result;

        if (numerator == 0)
            result = "0";
        else if (denominator == 1)
            result = numerator + "";
        else
            result = numerator + "/" + denominator;
        return result;
    }

    private void reduce() {
        if (numerator != 0) {
            int common = gcd(Math.abs(numerator), denominator);

            numerator = numerator / common;
            denominator = denominator / common;
        }
    }

    private int gcd(int num1, int num2) {
        while (num1 != num2)
            if (num1 > num2)
                num1 = num1 - num2;
            else
                num2 = num2 - num1;
        return num1;
    }


    public int compareTo(RationalNumber answer1) {
        if (answer1.getDenominator() == this.getDenominator() && answer1.getNumerator() == this.getNumerator())
            return 1;
        if (answer1.getNumerator() != this.getNumerator() && answer1.getDenominator() != this.getDenominator())
            return 0;
        else
            return Integer.parseInt(null);
    }
}  

2. Calculate the correct rate

num=scan.nextInt();
accuracy = (float)correct/num;
System.out.printf("正确率:%.2f",accuracy*100  );

3. Using the timer knowledge learned this week, design a timer code:


public static void timer1() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                System.out.println("--请加快操作速度(Quickly)--");
            }
        }, 5000);// 毫秒
    }

code link

4. Running screenshots


5. Pairing thoughts

My pairing partner is 20165206 Han Xiao. The success of our design lies in designing the calculation of fractions, and using the knowledge of timers learned this week to design a timer algorithm.

6. Pairing creative map

Guess you like

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