20165232 Pair Programming Week 2 Summary

20165232 2017-2018-2 "Java Programming" Pair Programming I Summary of the Second Week

paired object

20165219 Wang Yanbo

20165232 He Yanda

demand analysis

Implement a program that requires:

1 supports integer arithmetic

2 supports multi-operator operations

3 Support fraction operation, support true fraction operation

Expansion requirements:

1 Process the generated questions and output to a file

2 After completing the question, read in from the file and judge the question

3 Multi-language support: Simplified Chinese, Traditional Chinese, English

4 Generate topic deduplication

Design ideas

This is a program that needs to support complex operations. It is different from the simple four operations of a single operator. It needs to design multiple classes to implement operations, such as addition, subtraction, multiplication and division, and square operations.

nml diagram (section)

the first week

image

the second week
image

code progress

Integer Computing Class

public class IntNumber {
    private int A;

    public int getA(){
        return A;
    }

    public IntNumber(int A){
        this.A = A;
    }

    public int add(IntNumber op1){
        int sum = A + op1.A;
        System.out.print(A + " + " + op1.A + "=");
        return sum;
    }

    public int subtract(IntNumber op1){
        int num = A - op1.A;
        System.out.print(A + " - " + op1.A + "=");
        return num;
    }

    public int multiply(IntNumber op1){
        int num = A * op1.A;
        System.out.print(A + " * " + op1.A + "=");
        return num;
    }

    public static IntNumber obj(){
        Random ran = new Random();
        return new IntNumber(ran.nextInt(20)-10);
    }

    public String divide(Score op1){
        System.out.print(op1.getNumerator() + " / " + op1.getDenominator() + "=");
        return op1.toString();
    }
}

Fractional operations

import java.util.Random;

public class Score {
    private int numerator, denominator;


    public  Score (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 Score reciprocal()
    {
        return new Score (denominator, numerator);
    }

    public Score add(Score op2)
    {
        int commonDenominator = denominator * op2.getDenominator();
        int numerator1 = numerator * op2.getDenominator();
        int numerator2 = op2.getNumerator() * denominator;
        int sum = numerator1 + numerator2;
        System.out.print("("+this.toString()+")" + " + " + "("+op2.toString()+")" + "=");
        return new Score (sum, commonDenominator);
    }

    public Score subtract(Score op2)
    {
        int commonDenominator = denominator * op2.getDenominator();
        int numerator1 = numerator * op2.getDenominator();
        int numerator2 = op2.getNumerator() * denominator;
        int difference = numerator1 - numerator2;
        System.out.print("("+this.toString()+")" + " - " + "("+op2.toString()+")" + "=");
        return new Score(difference,commonDenominator);
    }

    public Score multiply (Score op2)
    {
        int numer = numerator * op2.getNumerator();
        int denom = denominator * op2.getDenominator();
        System.out.print("("+this.toString()+")" + " * " + "("+op2.toString()+")" + "=");
        return new Score (numer, denom);
    }

    public Score divide (Score op2)
    {
        Score op1 = op2.reciprocal();
        int numer = numerator * op1.getNumerator();
        int denom = denominator * op1.getDenominator();
        System.out.print("("+this.toString()+")" + " / " + "("+op2.toString()+")" + "=");
        return new Score (numer, denom);
    }

    public boolean isLike (Score 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 static Score obj(){
        Random ran = new Random();
        return new Score(ran.nextInt(20)-10,ran.nextInt(20)-10);
    }
}

True or false judgment

public class CorrectJudgment {
    private static int trues = 0;

    public static void judgment(boolean same, String num1) {
        if (same) {
            trues++;
            System.out.println("正确!"+"\n");
        } else {
            System.out.println("错误!");
            System.out.println("正确结果为:" + num1+"\n");
        }
    }

    public static int getTrues() {
        return trues;
    }
}

main class

import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.*;

public class NumberCount {
    public static void main(String[] args) {
        NumberFormat nf = NumberFormat.getPercentInstance();
        IntNumber in1, in2;
        Score score1, score2,num2;
        int num,N;
        String Q,num1;

        Random ran = new Random();
        Scanner scan = new Scanner(System.in);

        System.out.print("请输入您所需要的题目数量:");
        int A = scan.nextInt();

        for (int i = 0; i < A; i++) {
                        int B = ran.nextInt(2);
                        int C = ran.nextInt(4);

                        in1 = IntNumber.obj();
                        in2 = IntNumber.obj();
                        score1 = Score.obj();
                        score2 = Score.obj();
                        if (B == 0) {
                            switch (C) {
                                case 0:
                                    num = in1.add(in2);
                                    num1 = "" + num;
                                    N = scan.nextInt();
                                    CorrectJudgment.judgment(N == num,num1);
                                    break;
                                case 1:
                                    num = in1.subtract(in2);
                                    num1 = "" + num;
                                    N = scan.nextInt();
                                    CorrectJudgment.judgment(N == num,num1);
                                    break;
                                case 2:
                                    num = in1.multiply(in2);
                                    num1 = "" + num;
                                    N = scan.nextInt();
                                    CorrectJudgment.judgment(N == num,num1);
                                    break;
                                case 3:
                                    num1 = in1.divide(score1);
                                    Q = scan.next();
                                    CorrectJudgment.judgment(Q.equals(num1),num1);
                                    break;
                            }
                        } else {
                            switch (C) {
                                case 0:
                                    num2 = score1.add(score2);
                                    num1 = num2.toString();
                                    Q = scan.next();
                                    CorrectJudgment.judgment(Q.equals(num1),num1);
                                    break;
                                case 1:
                                    num2 = score1.subtract(score2);
                        num1 = num2.toString();
                        Q = scan.next();
                        CorrectJudgment.judgment(Q.equals(num1),num1);
                        break;
                    case 2:
                        num2 = score1.multiply(score2);
                        num1 = num2.toString();
                        Q = scan.next();
                        CorrectJudgment.judgment(Q.equals(num1),num1);
                        break;
                    case 3:
                        num2 = score1.divide(score2);
                        num1 = num2.toString();
                        Q = scan.next();
                        CorrectJudgment.judgment(Q.equals(num1),num1);
                        break;
                }
            }
        }
        System.out.println("你答对的题目总数:" + CorrectJudgment.getTrues());
        double T = (double) CorrectJudgment.getTrues()/A;
        System.out.println("您的正确率为:" + nf.format(T));
    }

Functional screenshot

normal test
image

edge case testing
image

exception test
image

pair feeling

After two weeks of pair programming, I think that the four operations seem to be simple in terms of thinking, but there are many deficiencies in the specific operation. First, the code for fractional calculation is completed after consulting students, and secondly, the code for unit testing is not completed, and Multilevel operations cannot be supported. I believe that it can be gradually improved in the subsequent pairing study.

Pair evaluation

Wang Yanbo has helped me a lot in pair programming. I am not very clear about some code problems, and Wang Yanbo patiently answered me.

pairing photos

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326868215&siteId=291194637