1.RationalNumber类的代码
package RationalNumber;
public class RationalNumber {
private int numerator ;
private int denominator ;
public RationalNumber(int numerator,int denominator) {
this.numerator = numerator;
this.denominator = denominator;
if(this.denominator==0) { //分母为0强制退出;
throw new IllegalArgumentException("分母不可为0!");
}
}
public int getNumberator() { //获取分子
return this.numerator;
}
public int getDenominator() { //获取分母
return this.denominator;
}
public int gcd() { //约分
int t,m,n;
m=this.denominator;
n=this.numerator;
if(n == 0)
{
return 0;
}
while(m % n != 0) {
t=n;
n=m%n;
m=t;
}
this.denominator /= n;
this.numerator /= n;
return n;
}
public RationalNumber add(RationalNumber num) { //加法
int numerator,denominator;
denominator=num.denominator*this.denominator;
numerator=num.denominator*this.numerator+num.numerator*this.denominator;
RationalNumber sum = new RationalNumber(numerator,denominator);
sum.gcd();
return sum;
}
public RationalNumber subtraction(RationalNumber num) { //减法
int numerator,denominator;
denominator = num.denominator*this.denominator;
numerator = this.numerator*num.denominator-num.numerator*this.denominator;
RationalNumber difference = new RationalNumber(numerator,denominator);
difference.gcd();
return difference;
}
public RationalNumber multiply(RationalNumber num) { //乘法
int numerator=this.numerator*num.numerator;
int denominator=this.denominator*num.denominator;
RationalNumber product = new RationalNumber(numerator,denominator);
product.gcd();
return product;
}
public RationalNumber division(RationalNumber num) { //除法
if(num.numerator!=0)
{
int denominator = this.denominator * num.numerator;
int numerator = this.numerator * num.denominator;
RationalNumber quotient = new RationalNumber(numerator,denominator);
quotient.gcd();
return quotient;
}
else
{
throw new IllegalArgumentException("除数不可为0!");
}
}
public boolean equals(RationalNumber num) { //判断两个有理数是否相等;
RationalNumber temp1 = new RationalNumber(this.numerator,this.denominator);
RationalNumber temp2 = new RationalNumber(num.numerator,num.denominator);
temp1.gcd();
temp2.gcd();
if(temp1.denominator==temp2.denominator && temp1.numerator==temp2.numerator) {
return true;
}
else {
return false;
}
}
public double doubleValue() { //转换为double型;
return (double)this.numerator/this.denominator;
}
public int intValue() { //取整数部分;
return this.numerator/this.denominator;
}
public String toString() { //得到该有理数的字符串形式
return this.numerator+"/"+this.denominator;
}
public RationalNumber abs(){ //绝对值
RationalNumber result = new RationalNumber(0,1);
if(this.denominator<0) {
result.denominator = -this.denominator;
}
else {
result.denominator = this.denominator;
}
if(this.denominator<0) {
result.numerator = -this.numerator;
}
else
{
result.numerator = this.numerator;
}
return result;
}
}
2.1测试代码
package text;
import RationalNumber.RationalNumber;
public class text{
public static void main(String [] args) {
RationalNumber a = new RationalNumber (2,8);
RationalNumber b = new RationalNumber (1,4);
RationalNumber c = new RationalNumber (0,5);
RationalNumber d = new RationalNumber (-1,3);
System.out.println("获取a的分子分母:"+a.getNumberator()+"/"+a.getDenominator());
System.out.println("获取b的分子分母:"+b.getNumberator()+"/"+b.getDenominator());
System.out.println("获取c的分子分母:"+c.getNumberator()+"/"+c.getDenominator());
System.out.println("获取d的分子分母:"+d.getNumberator()+"/"+d.getDenominator());
System.out.println("a的绝对值:"+a.abs().toString());
System.out.println("d的绝对值:"+d.abs().toString());
System.out.println("a的值:"+a.doubleValue());
System.out.println("a+b = "+a.add(b).toString());
System.out.println("a-b = "+a.subtraction(b).toString());
System.out.println("c/d = "+c.division(d).toString());
System.out.println("c*d = "+c.multiply(d).toString());
System.out.println("a和b是否相等:"+b.equals(a));
System.out.println("c和d是否相等:"+c.equals(d));
System.out.println("d/c = "+d.division(c).toString());
RationalNumber r = new RationalNumber (1,0);//分母为0;
System.out.println("r的值:"+r.doubleValue());
}
}
2.2测试结果
3.尝试描述怎么与c语言的有理数代码相比较,为什么你设计的类更加面向对象?
在c语言的有理数代码中,其中设置的函数都是为了完成某个步骤,在解决某个问题时,这些函数都是解决该问题中的一个步骤;在java设计的类中,是根据有理数的特性设计出其方法,是依托于有理数这一类的对象,所有的对象都具有相同的属性方法,且可随时抽取其中的属性和方法进行编程。而c语言中,是设计了每个解决步骤,然后再利用每个变量进行传参套用。
4.尝试从代码复用的角度来描述你设计的有理数类。从几个方面讨论。
4.1别人如何复用你的代码?
别人如何复用你的代码:将自己的代码打包发给别人,然后使用import导入有理数的包;
4.2别人的代码是否依赖你的有理数类的属性?当你的有理数类的属性修改时,是否会影响他人调用你有理数类的代码?
别人的代码不会依赖我有理数的属性,当我的有理数类属性修改时,不会影响到他人调用我有理数的代码,因为我属性的修饰符都为private;
4.3有理数类的public方法是否设置合适?为什么有的方法设置为private?
public使得用户在其他包中的类可以直接调用该方法,对于执行某些基础操作的方法来说,直接定义为public可以尽快的完成程序功能。如果是稍微比较复杂的操作,为了隐藏其中的细节保护其隐私,可以设置成private来进行封装。