3.1-互评-OO设计-有理数类的设计

一、给出你的有理数类的代码。

package rational;

public class Rational {
	private int numerator;    //分子
    private int denominator;    //分母

    public Rational(int numerator,int denominator)    //构造函数
    {
    	int gcd = gcd(numerator, denominator);
    	this.numerator = ((denominator>0)? 1 : -1)*numerator / gcd;
    	this.denominator = Math.abs(denominator) / gcd;
    }
    
    public static int gcd(int numerator,int denominator){    //greatest common divisor gcd返回最大公约数
    	int Numerator = Math.abs(numerator);
    	int Denominator = Math.abs(denominator);
    	int gcd = 1;
        for (int i = 1; i<= Numerator && i <= Denominator; i++) {
    		if (Numerator % i == 0 &&  Denominator % i == 0)
    			gcd = i;
    	}
        return gcd;
  }
    
    public Rational()    //无参构造函数
    {
        this.numerator=0;
        this.denominator=1;
    }

    public int getNumerator()    //获取分子
    {
        return this.numerator;
    }
    
    public int getDenominator()    //获取分母
    {
        return this.denominator;
    }
    
    public Rational add(Rational next) {    //两数相加
    	int N = this.numerator * next.denominator + this.denominator * next.numerator;
    	int D = this.denominator * next.denominator;
    	return new Rational(N ,D);
    }

    public Rational subtract(Rational next) {    //两数相减
    	int N = this.numerator * next.denominator - this.denominator * next.numerator;
    	int D = this.denominator * next.denominator;
    	return new Rational(N ,D);
    }

    public Rational multiply(Rational next) {    //两数相乘
    	int N = this.numerator * next.numerator;
    	int D = this.denominator * next.denominator;
    	return new Rational(N ,D);
    }

    public Rational divide(Rational next) {    //两数相除
    	int N = this.numerator * next.denominator;
    	int D = this.denominator * next.numerator;
    	return new Rational(N ,D);
    }

    public int intValue(){    //返回整数
        return (int)this.numerator/this.denominator;
    }
 
    public double doubleValue(){    //返回浮点数
        return (double)this.numerator*1.0/this.denominator;
    }
    
    public String toString(){    //返回字符串
        return numerator+"/"+denominator;
    }
}

二、测试代码。

package rational;
import rational.Rational;

public class Main {
	public static void main(String[] args) {
		Rational test1 = new Rational(1,3);
        Rational test2 = new Rational(-2,5);
        
        System.out.println("test1 + test2 = "+test1.add(test2));
        System.out.println("test1 - test2 = "+test1.subtract(test2));
        System.out.println("test1 * test2 = "+test1.multiply(test2));
        System.out.println("test1 / test2 = "+test1.divide(test2));
        System.out.println("测试一的整型部分为: :" + test1.intValue());
        System.out.println("测试二的浮点型为::" + test2.doubleValue());
        System.out.println("测试二转化为字符串为:" + test2.toString());
	}
}

三、描述怎么与c语言的有理数代码相比较,为什么你设计的类更加面向对象?

  • c语言和Java都为编程语言,c语言侧重过程,java中的类更侧重对象的操作。c语言更倾向于过程,Java倾向对对象的处理,例如a、b两数相加,c语言add(a,b),Java为a.add(b)。
  • Java更面对对象,在实际操作的时候可以很方便直接调用。

四、别人如何复用你的代码?

导入rational包即可使用

五、别人的代码是否依赖你的有理数类的属性?当你的有理数类的属性修改时,是否会影响他人调用你有理数类的代码?

依赖,有理数类的属性被修改会影响调用有理数的代码,可能会出错。

六、6.有理数类的public方法是否设置合适?为什么有的方法设置为private?

public是公共的,private是私有的用于声明类和成员的可见性。public可以被其他代码调用,所以设置为pubilc方法合适。有的方法设置为private,别的代码就难以访问该private,以此来控制代码的访问。

posted @ 2020-10-04 11:03  宋思坡201921123103  阅读(115)  评论(0编辑  收藏  举报