OO设计–JAVA-有理数类的设计
问题
按照BigDecimal类以面向对象的方式设计有理数类。
给出你的测试代码。要在与有理数类不同包的其他类中调用有理数类。
尝试回答与c语言的有理数代码相比较。
Rational函数
package test; import java.math.BigDecimal; public class Rational { private long numerator = 0; //分子 private long denominator = 1; //分母 public long getNumerator() { return numerator; } public void setNumerator(long numerator) { this.numerator = numerator; } public long getDenominator() { return denominator; } public void setDenominator(long denominator) { this.denominator = denominator; } public Rational(){//分子分母初始化 this.numerator = 0; this.denominator = 1; } public Rational(long numerator, long denominator){ this.numerator = numerator/gcd(Math.abs(numerator), Math.abs(denominator)); //分子化简(除以分子、分母的最大公约数) this.denominator = denominator/gcd(Math.abs(numerator), Math.abs(denominator));//分母化简(除以分子、分母的最大公约数) } public static long gcd (long number1, long number2){//最大公约数计算 while (number1 != number2) { if (number1 > number2) number1 = number1 - number2; else number2 = number2 - number1; } return number1; } public static Rational add (Rational num1, Rational num2) {//有理数求和 long tempNumerator = num1.numerator*num2.denominator + num1.denominator*num2.numerator; long tempDenominator = num1.denominator*num2.denominator; long gcd = gcd(Math.abs(tempNumerator),Math.abs(tempDenominator)); Rational rationalNumber = new Rational(tempNumerator/gcd,tempDenominator/gcd); return rationalNumber; } public static Rational subtract (Rational num1, Rational num2) {//有理数求差 long tempNumerator = num1.numerator*num2.denominator - num1.denominator*num2.numerator; long tempDenominator = num1.denominator*num2.denominator; long gcd = gcd(Math.abs(tempNumerator),Math.abs(tempDenominator)); Rational rationalNumber = new Rational(tempNumerator/gcd,tempDenominator/gcd); return rationalNumber; } public static Rational multiply (Rational num1, Rational num2) {//有理数求积 long tempNumerator = num1.numerator*num2.numerator; long tempDenominator = num1.denominator*num2.denominator; long gcd = gcd(Math.abs(tempNumerator),Math.abs(tempDenominator)); Rational rationalNumber = new Rational(tempNumerator/gcd,tempDenominator/gcd); return rationalNumber; } public static Rational divide (Rational num1, Rational num2) {//有理数求商 long tempNumerator = num1.numerator*num2.denominator; long tempDenominator = num1.denominator*num2.numerator; long gcd = gcd(Math.abs(tempNumerator),Math.abs(tempDenominator)); Rational rationalNumber = new Rational(tempNumerator/gcd,tempDenominator/gcd); return rationalNumber; } }
测试Class文件
package test; public class RationalTes { private static Rational num1=new Rational(1,2); //第一个分数 private static Rational num2=new Rational(12,4); //第二个分数 private static double test = Double.parseDouble("3457367536")/(double)6769656; public static void main(String[] args) { Rational result=Rational.add(num1, num2); System.out.println("有理数求和:"); System.out.println("(分数形式):1/2+12/4="+result.getNumerator()+"/"+result.getDenominator()); System.out.println("(小数形式):1/2+12/4="+(result.getNumerator()/(double)result.getDenominator())); result=Rational.subtract(num1, num2); System.out.println("有理数求差:"); System.out.println("(分数形式):1/2-12/4="+result.getNumerator()+"/"+result.getDenominator()); System.out.println("(小数形式):1/2-12/4="+(result.getNumerator()/(double)result.getDenominator())); result=Rational.multiply(num1, num2); System.out.println("有理数求积:"); System.out.println("(分数形式):1/2*12/4="+result.getNumerator()+"/"+result.getDenominator()); System.out.println("(小数形式):1/2*12/4="+(result.getNumerator()/(double)result.getDenominator())); result=Rational.divide(num1, num2); System.out.println("有理数求商:"); System.out.println("(分数形式):1/2-12/4="+result.getNumerator()+"/"+result.getDenominator()); System.out.println("(小数形式):1/2*12/4="+(result.getNumerator()/(double)result.getDenominator())); } }
运行结果
问题总结
Q1:与C语言相比何处体现更加面向对象?
A1:C语言的面向过程就是分析出解决问题所需要的步骤,之后使用函数一步步解决。而Java语言则通过把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙某个事物在整个解决问题的步骤中的行为。例如在此代码中,针对各个不同对象,进行数据封装,所有的通信都通过对象的方法来实现。
Q2:如何使用此代码?
A2:下载包到本地,导入包到IDE中,通过“类名.方法”调用函数。使用setter/getter设置和访问private。
Q3:调用时的代码是否依赖该有理数类的属性?当有理数类修改时,是否会影响调用?
A3:调用时的代码对该类的属性有依赖性。当有理数类修改时,会影响调用。
Q4:为什么有的方法设置为private?
A4:在如下场景时将某些方法设置为private:
当一个方法希望只被本类所使用时。
与本类的内部状态无关。