1. 程式人生 > 實用技巧 >有理數類的設計

有理數類的設計

1.有理數類的程式碼

package cn.edu.jmu;

public class Rational {
	private int numerator;		//分子
	private int denominator;	//分母
	
	public int getNumerator() {
		return numerator;
	}
	public int getDenominator() {
		return denominator;
	}

	//無參建構函式
	public Rational () {
		this.numerator = 0;
		this.denominator = 1;
	}
	//有參建構函式
	public Rational (int numerator, int denominator) {
		if (numerator < 0 && denominator < 0) {
			this.numerator = -numerator;
			this.denominator = -denominator;
		}else if (numerator > 0 && denominator < 0) {
			this.numerator = -numerator;
			this.denominator = -denominator;
		}else {
			this.numerator = numerator;
			this.denominator = denominator;
		}
	}
	
	//求最大公約數,化簡用
	private int gcd(int numerator, int denominator) {
        int x = Math.abs(numerator);
        int y = Math.abs(denominator);
        int tmp;
        while (y != 0) {
            tmp = x % y;
            x = y;
            y = tmp;
        }
        return x;
    }
	
	//相加
	public Rational add (Rational x) {
		int numerator2 = numerator * x.denominator + x.numerator * denominator;
		int denominator2 = denominator * x.denominator;
		return new Rational(numerator2, denominator2);
	}
	//相減
	public Rational subtract (Rational x) {
		int numerator2 = numerator * x.denominator - x.numerator * denominator;
		int denominator2 = denominator * x.denominator;
		return new Rational(numerator2, denominator2);
	}
	//相乘
	public Rational multiply (Rational x) {
		int numerator2 = numerator * x.numerator;
		int denominator2 = denominator * x.denominator;
		return new Rational(numerator2, denominator2);
	}
	//相除
	public Rational divide (Rational x) {
		int numerator2 = numerator * x.denominator;
		int denominator2 = denominator * x.numerator;
		return new Rational(numerator2, denominator2);
	}
	//求絕對值
	public Rational abs () {
		int numerator2 = numerator;
		int denominator2 = denominator;
		if (numerator2 < 0) {
			numerator2 = -numerator2;
		}
		if (denominator2 < 0) {
			denominator2 = -denominator2;
		}
		
		return new Rational(numerator2, denominator2);
	}
	//判斷是否相等
	public boolean equals (Rational x) {
		if (this.subtract(x).getNumerator() == 0) {
			return true;
		}else {
			return false;
		}
	}
	//比較大小,大於所比較的數返回1,小於返回-1,等於返回0
	public int compareTo (Rational x) {
		if (this.subtract(x).getNumerator() > 0) {
			return 1;
		}else if (this.subtract(x).getNumerator() < 0) {
			return -1;
		}else {
			return 0;
		}
	}
	//以字串形式輸出
	public String toString () {
		int t = gcd(numerator, denominator);
		numerator /= t;
		denominator /= t;
		
		if(denominator == 1) {
			return numerator+"";
		}else if (numerator == 0) {
			return 0+"";
		}else {
			return numerator + "/" + denominator ;
		}
	}
}

2.測試程式碼

package demo;

import cn.edu.jmu.Rational;

public class Main {

	public static void main(String[] args) {
		Rational s1 = new Rational(1, 2);
		Rational s2 = new Rational(3, 4);
		Rational s3 = new Rational(8, -7);
		Rational s4 = new Rational(3, 4);
		Rational s5 = new Rational();
		
		System.out.println("s1+s2 = " + s1.add(s2).toString());
		System.out.println("s1-s2 = " + s1.subtract(s2).toString());
		System.out.println("s1*s2 = " + s1.multiply(s2).toString());
		System.out.println("s1/s2 = " + s1.divide(s2).toString());	
		System.out.println("|s3| = " + s3.abs().toString());
		System.out.println("判斷s1與s2是否相等:" + s1.equals(s2));
		System.out.println("判斷s2與s4是否相等:" + s2.equals(s4));
		System.out.println("s1與s2比大小:" + s1.compareTo(s2));
		System.out.println("無參構造:" + s5.toString());
}

3.執行結果

4.嘗試描述怎麼與c語言的有理數程式碼相比較,為什麼你設計的類更加面向物件?

c語言是以函式的形式編寫,java是以類的形式編寫,使用者在使用時只需匯入即可,更加的方便,而且定義方法時可以進行過載,可為類似功能的方法提供統一名稱,根據引數型別或個數的不同調用相對應的方法。而c語言一個函式只能有一種引數定義。

5.嘗試從程式碼複用的角度來描述你設計的有理數類。從幾個方面討論。

a.別人如何複用你的程式碼?

匯入對應包中的有理數類。例:import cn.edu.jmu.Rational;

b.別人的程式碼是否依賴你的有理數類的屬性?當你的有理數類的屬性修改時,是否會影響他人呼叫你有理數類的程式碼?

別人的程式碼依賴我的有理數類的屬性。
當我的有理數類的屬性修改時,因為屬性設定為private,所以不影響他人呼叫有理數類的程式碼。

c.有理數類的public方法是否設定合適?為什麼有的方法設定為private?

合適。
防止使用者對資料進行錯誤修改造成程式崩潰;輔助編寫public方法的方法,不應被使用者呼叫。