20172311 "Java Programming" Course Pair Programming Exercise_Four Algorithms Second Week Phase Summary

20172311 "Java Programming" Course Pair Programming Exercise_Four Algorithms Second Week Phase Summary

pairing partner

  • Student ID: 20172307

  • Name: Huang Yutang

  • Partner's first week blog address:
  • Evaluation of paired partners: Huang Yutang's advantage lies in being able to come up with some optimized solutions and discover the shortcomings of the code, but the ability to write code needs to be improved, continue to work hard! !


photo of group pair programming

HERE!

own part of the project

specific code

personal contribution division

In my opinion, although I mainly carried out the specific coding work, the final realization of the code is inseparable from the testing of the code by Huang Yutang and the lack of corrections. I think it should be 55

Screenshots of the relevant process

  • Generate running screenshots of topic-driven classes


  • Screenshot of the driver class that converts infix expressions to postfix expressions


  • Screenshot of driver class running for postfix expression calculation


Key code explanation

Create1. The class that generates random scores written by myself is used in the class that generates expressionsFraction
  • CreateThe class code is as follows:
/*
Create.java         作者:赵晓海 黄宇瑭
用于生成用户指定个数指定等级的题目,且题目中含有分数
 */
public class Create {
    String[]Arraylist;//保存题目的数组
    int num,rate;//题目的个数和等级

    //构造函数
    public Create(int num ,int rate)
    {
        this.num=num;
        this.rate=rate;
        Arraylist=new String[num];
    }

    //生成相应等级的一个题目的方法
    public String questionRate(int a)
    {
        String express="";
        String[]OC={"+","-","×","÷"};

        for (int c=0;c<a;c++) {
            Fraction b = new Fraction();
            String d=b.getFraction();
            String e=OC[(int) (Math.random() * 4)];
            while (d=="0"&&e=="÷") {
                Fraction f=new Fraction();
                d = f.getFraction();

            }
            express +=d+" "+e+ " ";
        }

        Fraction c=new Fraction();
        String e=c.getFraction();
        while (express.charAt(4*rate-1)=='÷'&&e=="0")
        {
            Fraction d=new Fraction();
            e=d.getFraction();
        }

        express+=e+" "+"=";
        return express;
    }
    //生成相应个数,相应等级题目,且将其放入ArrayList数组中保存的方法
    public  void QuestionNum(){
        Create F=new Create(num,rate);
        for(int a=0;a<num;a++)
            Arraylist[a]=F.questionRate(rate);
    }

    //返回ArrayList数组的方法
    public String[] getArraylist() {
        return Arraylist;
    }

    //返回数组中指定索引处表达式的方法
    public String getArraylist2(int a)
    {
        String b;
        b=Arraylist[a];
        return b;
    }
    //输出的得到的表达式的方法
    public String toString() {
        String a="";
        for (int b=0;b<num;b++)
            a+=Arraylist[b]+"\n";
        return a;
    }
}
  • FractionThe class code is as follows:
/*
 Fraction.java           作者:赵晓海  黄宇瑭
 用于生成随机最简分数或者整数且不大于50
 */
public class Fraction {
    int numerator, denominator;

    //构造函数
    public Fraction() {
       numerator= (int) (Math.random()*51);
       denominator= (int) (Math.random()*51);
       if(denominator==0)
           denominator=1;
       reduce();
 }
    //保证遵循分数的规则
    public 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 String getFraction()
    {
        String result;
        if(numerator==0)
            result="0";
        else
            if(denominator==1)
                result=numerator+"";
        else
            result=numerator+"/"+denominator;
        return result;
    }
}
Calculate2. The RationalNumber class in Chapter 7 is used directly in the class that calculates the postfix expression
  • CalculateThe class code is as follows:
/*
Calculate.java         作者:赵晓海  黄宇瑭
用于计算一个后缀表达式
 */
import chap7.RationalNumber;

import java.util.Stack;

public class Calculate {

    String [] str1;
    String result;//计算结果

    //构造函数
    public Calculate (String calculate){
        str1=calculate.split("\\s");
    }

    //将字符串形式的整数或者分数转换为RationalNumber类的对象的方法
    public RationalNumber toRationalNumber(String num){
        if (num.contains("/")==false){
            int a =Integer.parseInt(num);
            RationalNumber rationalNumber1 =new RationalNumber(a,1);
            return rationalNumber1;
        }
        else {
           /* StringTokenizer st = new StringTokenizer(num,"/");
            int numerator =Integer.parseInt(st.nextToken("/"));
            int denominator=Integer.parseInt(st.nextToken("/"));
            */
           String[] Array =num.split("/");
           int numerator = Integer.parseInt(Array[0]);
           int denominator=Integer.parseInt(Array[1]);

            RationalNumber rationalNumber2 =new RationalNumber(numerator,denominator);
            return rationalNumber2;
        }
    }

    //计算中缀表达式,并将结果保存在result中的方法
    public void ToResult(){
        Stack stack1=new Stack();
        int start =0;

        while (start<str1.length){
            if (str1[start].equalsIgnoreCase("+")==false&&str1[start].equalsIgnoreCase("-")==false&&str1[start].equalsIgnoreCase("×")==false&&str1[start].equalsIgnoreCase("÷")==false){

                stack1.push(str1[start]);
            }
            else
                if (str1[start].equalsIgnoreCase("+")==true){
                RationalNumber num1=this.toRationalNumber(String.valueOf(stack1.peek()));
                stack1.pop();
                RationalNumber num2=this.toRationalNumber(String.valueOf(stack1.peek()));
                stack1.pop();
                RationalNumber finish =num2.add(num1);
                String str3=finish.toString();
                stack1.push(str3);
                }
                else
                    if (str1[start].equalsIgnoreCase("-")==true){
                        RationalNumber num1=this.toRationalNumber(stack1.peek().toString());
                        stack1.pop();
                        RationalNumber num2=this.toRationalNumber(stack1.peek().toString());
                        stack1.pop();
                        RationalNumber finish =num2.subtract(num1);
                        String str3=finish.toString();
                        stack1.push(str3);
                    }
                    else
                        if (str1[start].equalsIgnoreCase("×")==true){
                            RationalNumber num1=this.toRationalNumber(stack1.peek().toString());
                            stack1.pop();
                            RationalNumber num2=this.toRationalNumber(stack1.peek().toString());
                            stack1.pop();
                            RationalNumber finish =num2.multiply(num1);
                            String str3=finish.toString();
                            stack1.push(str3);
                        }
                        else
                        {
                            RationalNumber num1=this.toRationalNumber(String.valueOf(stack1.peek()));
                            stack1.pop();
                            RationalNumber num2=this.toRationalNumber(String.valueOf(stack1.peek()));
                            stack1.pop();
                            RationalNumber finish =num2.divide(num1);
                            String str3=finish.toString();
                            stack1.push(str3);
                        }

                start++;
        }
        String str4=stack1.peek().toString();
        result=str4;
    }

    //得到计算结果的方法
    public String getResult() {
        return result;
    }

}

Difficulties encountered and solutions

  • Question 1: When writing code to judge whether two strings are equal, using `==` will cause an error.

  • Problem one solution:

    Identify problems through group discussions

  • Problem 2: An out-of-bounds error occurs when running the driver class that calculates the postfix expression

  • The screenshot of the running error is as follows:

  • Problem two solution:

    Through single-step debugging, I suddenly realized that the length of a String integer should not be 1. If it is an integer, there is no character in the string , so I found the method /in the String class by checking the API.Contains

  • The screenshot of successful debugging is as follows:

  • What's wrong with the code:

  • Corrections to the code:


The code cloud link of the project

Guess you like

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