JAVA互评—有理数类的设计
1.相关代码
package rational;
public class Rational{
private int numerator; //分子
private int denominator; //分母
private int getGcd(int nrt,int dmt) { //获取最大公约数
int max;
int min;
int flag=0;
if(nrt<0&&dmt<0)flag=1;
nrt=Math.abs(nrt);
dmt=Math.abs(dmt);
if(nrt>dmt) {
max=nrt;
min=dmt;
}else {
max=dmt;
min=nrt;
}
if(min==0)return 1;
int r= max % min;
while(r>0) { //辗转相除法
max=min;
min=r;
r=max%min;
}
if(flag==1)return -min;
return min;
}
public static Rational abs(Rational x) { //取绝对值
Rational a=new Rational(0,1);
a.denominator=x.denominator;
a.numerator=x.numerator;
if(a.denominator<0)a.denominator*=-1;
if(a.numerator<0)a.numerator*=-1;
return a;
}
public Rational(int nrt,int dmt) { //两个整型的构造函数
this.numerator=nrt;
this.denominator=dmt;
if(dmt==0) {
System.out.println("Error In Denominator 0!");
System.exit(0);
}else if(nrt==0) {
this.numerator=0;
this.denominator=1;
}
int x=getGcd(nrt,dmt);
if(x>=1||-x>=1) {
this.numerator/=x;
this.denominator/=x;
}
}
public Rational(Double x) { //一个Double构造函数
String s=x.toString();
int index=s.indexOf(".");
int dmt=s.length()-index;
dmt=(int) Math.pow(10, dmt);
int nrt=(int) (x*dmt);
int k=getGcd(dmt,nrt);
if(k>=1||-k>=1) {
this.numerator=nrt/=k;
this.denominator=dmt/=k;
}
}
public Rational(String s) { //String构造函数
Double x=Double.parseDouble(s);
int index = s.indexOf(".");
if(index==-1) {
this.numerator=Integer.parseInt(s);
this.denominator=1;
return;
}
int dmt=s.length()-index;
dmt=(int) Math.pow(10, dmt);
int nrt=(int) (x*dmt);
int k=getGcd(nrt,dmt);
if(k>=1||-k>=1) {
this.numerator=nrt/=k;
this.denominator=dmt/=k;
}
}
public Rational plus(Rational x) { //加法
int nrt=this.numerator*x.denominator+this.denominator*x.numerator;
int dmt=this.denominator*x.denominator;
Rational a =new Rational(nrt,dmt);
return a;
}
public Rational minus(Rational x) { //减法
int nrt=this.numerator*x.denominator-this.denominator*x.numerator;
int dmt=this.denominator*x.denominator;
Rational a =new Rational(nrt,dmt);
return a;
}
public Rational multiply(Rational x) { //乘法
int nrt=this.numerator*x.numerator;
int dmt=this.denominator*x.denominator;
Rational a =new Rational(nrt,dmt);
return a;
}
public Rational divide(Rational x) { //除法
int nrt=this.numerator*x.denominator;
int dmt=this.denominator*x.numerator;
Rational a =new Rational(nrt,dmt);
return a;
}
public String toString() { //返回有理数的字符串形式
Rational x=new Rational(0,1);
String s=new String();
x.denominator=this.denominator;
x.numerator=this.numerator;
if(x.denominator*x.numerator<0) {
s+="-";
x=Rational.abs(x);
}
if (x.denominator == 1)s+=x.numerator;
else s+=x.numerator + "/" + x.denominator;
return s;
}
public boolean equals(Rational x) { //判断两数是否相等
if(this.minus(x).toString().equals("0"))return true;
return false;
}
public Rational compareTo (Rational x) { //返回两数中较大的数
int index=this.minus(x).toString().indexOf("-");
if(index==-1) {
return this;
}else {
return x;
}
}
}
import rational.Rational;
public class Main{
public static void main(String args[]){
int x = 1;
int y = -1;
Double x1=-1.3;
String s= "-2.5";
Rational a= new Rational(x,y);
Rational b= new Rational(x1);
Rational c= new Rational(s);
Rational d= new Rational(-5,2);
System.out.println(x+"/"+y+" to Rational is :"+a.toString());
System.out.println(x1+" to Rational is :"+b.toString());
System.out.println(s+" to Rational is :"+c.toString());
System.out.println();
System.out.println(b.toString()+" equals "+c.toString()+" ? : "+b.equals(c));
System.out.println(c.toString()+" equals "+d.toString()+" ? : "+c.equals(d));
System.out.println("bigger in "+b.toString()+" and "+c.toString()+" : "+b.compareTo(c).toString());
System.out.println(b.toString()+" to abs : "+Rational.abs(b));
System.out.println();
System.out.println(b.toString()+" + "+c.toString()+" : "+b.plus(c).toString());
System.out.println(b.toString()+" - "+c.toString()+" : "+b.minus(c).toString());
System.out.println(b.toString()+" * "+c.toString()+" : "+b.multiply(c).toString());
System.out.println(b.toString()+" / "+c.toString()+" : "+b.divide(c).toString());
}
}
2.测试数据及结果
3.尝试描述怎么与c语言的有理数代码相比较,为什么你设计的类更加面向对象?
- c语言侧重与过程,java中的类更侧重对象的操作。例如a+b,C语言中是plus(a,b);而在java中则是a.plus(b);具体指向了是a这个对象进行加法运算。
- java类中的方法是针对这个类中的对象,而C语言的函数并没有明确规定,只需传参符合即可。
4.尝试从代码复用的角度来描述你设计的有理数类。从几个方面讨论。
a. 别人如何复用你的代码?
答:直接导入rational包即可使用。
b. 别人的代码是否依赖你的有理数类的属性?当你的有理数类的属性修改时,是否会影响他人调用你有理数类的代码?
答:依赖,因为我的有理数类属性中仅有分子跟分母,且当修改该属性后,代码可能报错,会影响他人使用。
c. 有理数类的public方法是否设置合适?为什么有的方法设置为private?
答:public方法中有加、减、乘、除、toString()、equals()、cpmpareTo()和取绝对值abs(),设置合适;private方法中仅有取最大公约数getGcd(),当不想外界调用时设置为private方法。