以面向对象的方式设计有理数类

1.有理数类的代码

package rationalNumber;

import java.util.Scanner;

public class Rational {
         private int molecular = 0;           //分子
         private int denominator = 1;         //分母

   public Rational() {                        //构造函数
         this(0,1);
   }
   public Rational(int molecular, int denominator) {
         int gcd = gcd(molecular, denominator);
         this.molecular = ((denominator>0) ? 1 : -1) * molecular / gcd;
         this.denominator = Math.abs(denominator) / gcd;
   }
   public static int gcd(int molecular, int denominator) {       //用辗转相除法求最大公约数以便化简
         int n = Math.abs(molecular);
         int d = Math.abs(denominator);
         int gcd = 1;
         while (d != 0) {
             gcd = n % d;
             n = d;
             d = gcd;
         }
         return n;
   }
   public int getMolecular() {                    
         return molecular;
   }
   public int getDenominator() {
         return denominator;
   }
   public Rational add(Rational secondRational) {   //加法
         int n = this.molecular*secondRational.getDenominator() + this.denominator*secondRational.getMolecular();
         int d = this.denominator*secondRational.getDenominator();
         return new Rational(n,d);
   }
   public Rational subtract(Rational secondRational){//减法
         int n = this.molecular*secondRational.getDenominator() - this.denominator*secondRational.getMolecular();
         int d = this.denominator*secondRational.getDenominator();
         return new Rational(n,d);
   }
   public Rational multiply(Rational secondRational){//乘法
	     int n = this.molecular*secondRational.getMolecular();
	     int d = this.denominator*secondRational.getDenominator();
         return new Rational(n,d);
   }
   public Rational divide(Rational secondRational){  //除法
         int n = molecular*secondRational.getDenominator();
         int d = denominator*secondRational.molecular;
         return new Rational(n,d);
   }
   public boolean equals(Object praml){               //判断
         if((this.subtract((Rational)(praml))).getMolecular() == 0) {
               return true;
         }
         else {
        	 return false;
         }
   }
   public int intValue(){                             //将Rational转化成int型
         return (int)doubleValue();
   }
   public float floatValue(){                         //将Rational转化成float型
         return (float)doubleValue();
   }
   public double doubleValue(){                       //将Rational转化成double型
         return molecular * 1.0 / denominator;
   }
   public long longValue(){                           //将Rational转化成long型
         return (long)doubleValue();
   }
   public String toString(){                          //toString类
         if(denominator==1){
               return molecular+"";
         }
         else return molecular+"/"+denominator;
   }
}

2.测试代码

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext())
		{
			int m = sc.nextInt();
			int n = sc.nextInt();
			int p = sc.nextInt();
			int q = sc.nextInt();
			Rational R1 = new Rational(m, n);
			Rational R2 = new Rational(p, q);
			Rational R;
			R= R1.add(R1, R2);
			System.out.println("相加:"+R);
			R=R1.subtract(R1, R2);
			System.out.println("相减:"+R);
			R=R1.multiply(R1, R2);
			System.out.println("相乘:"+R);
			R=R1.divide(R1, R2);
			System.out.println("相除:"+R);
		}
	}
}

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

面向对象编程是对类进行封装成新的类以实现对象的直接调用

4.尝试从代码复用的角度来描述你设计的有理数类。从几个方面讨论。

a.别人如何复用你的代码?

通过导入有理数类的包复用。

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

别人的代码会依赖我的有理数类的属性。
当我的有理数类的属性修改时,会影响他人调用你有理数类的代码。

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

public定义的东西是允许外界共享的。
private 定义的字段,属性,方法是私有的,方法、函数均不允许外界应用程序(包括本程序的其它模块、窗体等)条用,别的类就不能访问该成员变量,而只能通过该类的方法(该方法要求是public的)访问该成员变量,是对私有数据的保护。

说明:怎么查看BigDecimal的设计?
使用JDK文档查看BigDecimal的公共方法(入参、返回值、功能)。
进阶:在Eclipse查看BigDecimal的代码。先看其属性,再看其公共方法的声明。可以不用详细看代码实现。

posted @ 2020-10-04 08:34  zhuzii  阅读(126)  评论(0编辑  收藏  举报