有理数类的设计
仿照BigDecimal类以面向对象的方式设计有理数类
1、有理数类源代码:
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;
}
}
2、测试代码:
(在RationalTest.java中测试Rational.java)
//import java.util.Scanner;
public class RationalTest {
/*Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
int nextNumber = scanner.nextInt();
*/
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()));
}
}
3、测试结果
4、Q&A
Q1:与C语言相比何处体现更加面向对象?
A1:C语言的面向过程就是分析出解决问题所需要的步骤,之后使用函数一步步解决。而Java语言则通过把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙某个事物在整个解决问题的步骤中的行为。例如在此代码中,针对各个不同对象,进行数据封装,所有的通信都通过对象的方法来实现。
Q2:如何使用此代码?
A2:下载包到本地,导入包到IDE中,通过“类名.方法”调用函数。使用setter/getter设置和访问private。
Q3:调用时的代码是否依赖该有理数类的属性?当有理数类修改时,是否会影响调用?
A3:调用时的代码对该类的属性有依赖性。当有理数类修改时,会影响调用。
Q4:为什么有的方法设置为private?
A4:在如下场景时将某些方法设置为private:
当一个方法希望只被本类所使用时。
与本类的内部状态无关。
最简单的例子就是平时经常见到的工具方法,我们希望不分配内存创建类的实例就能够使用他们,所以会将他们声明public static, 在此基础之上,如果我们喜欢只有该类能够访问到此方法的话,可以声明为private static。