一. 论述题(共1题,5分)
1. (论述题, 5分)
仿照BigDecimal类以面向对象的方式设计有理数类。
给出你的完整的有理数类的代码:
//有理数类Rational
package test;
public class Rational {
private int molecule=0;//分子
private int denominator=0;//分母
public Rational(int molecule, int denominator) {
super();
this.molecule = molecule;
this.denominator = denominator;
}
public int getMolecule() { //获取分子
return molecule;
}
public void setMolecule(int molecule) {
this.molecule = molecule;
}
public int getDenominator() { //获取分母
return denominator;
}
public void setDenominator(int denominator) {
this.denominator = denominator;
}
public Rational rationalAdd(Rational X) {//加法
int a=this.molecule*X.denominator+X.molecule*this.denominator;
int b=this.denominator*X.denominator;
return new Rational(a,b);
}
public Rational rationalSub(Rational X) {//减法
int a=this.molecule*X.denominator-X.molecule*this.denominator;
int b=this.denominator*X.denominator;
return new Rational(a,b);
}
public Rational rationalMul(Rational X) {//乘法
int a=this.molecule*X.molecule;
int b=this.denominator*X.denominator;
return new Rational(a,b);
}
public Rational rationalDiv(Rational X) {//除法
int a=this.molecule*X.denominator;
int b=this.denominator*X.molecule;
return new Rational(a,b);
}
public int compareTo(Rational X) { //比较大小
if(this.rationalSub(X).getMolecule()>0)
return 1;//大于
else if(this.rationalSub(X).getMolecule()==0)
return 0;//等于
else
return -1;//小于
}
public String toString() { //转换成String型,输出Rational类默认转换为该型
if(this.denominator==1)
return this.molecule+" ";
else
return this.molecule+"/"+this.denominator;
}
public double doubleValue() { //转换成double型
return 1.0*this.molecule/this.denominator;
}
public int intValue() { //转换成int型
return (int)doubleValue();
}
public long longValue() { //转换成long型
return (long)doubleValue();
}
}
给出你的测试代码。要在与有理数类不同包的其他类中调用有理数类:
package test;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("输入两个数字构成有理数分数");
Rational a = new Rational(sc.nextInt(), sc.nextInt());
System.out.println("输入两个数字构成有理数分数");
Rational b = new Rational(sc.nextInt(), sc.nextInt());
System.out.println("两个有理数相加结果");
System.out.println(a.rationalAdd(b));
System.out.println("两个有理数相减结果");
System.out.println(a.rationalSub(b));
System.out.println("两个有理数相乘结果");
System.out.println(a.rationalMul(b));
System.out.println("两个有理数相除结果");
System.out.println(a.rationalDiv(b));
System.out.println("比较两个有理数大小结果:大于输出1,等于输出0,小于输出-1");
System.out.println(a.compareTo(b));
System.out.println("将第一个有理数转换成字符串型");
System.out.println(a.toString());
System.out.println("将第一个有理数转换成double型");
System.out.println(a.doubleValue());
System.out.println("将第一个有理数转换成int型");
System.out.println(a.intValue());
System.out.println("将第一个有理数转换成long型");
System.out.println(a.longValue());
}
}
输入:
输入两个数字构成有理数分数
1 3
输入两个数字构成有理数分数
1 4
输出结果:
两个有理数相加结果
7/12
两个有理数相减结果
1/12
两个有理数相乘结果
1/12
两个有理数相除结果
4/3
比较两个有理数大小结果:大于输出1,等于输出0,小于输出-1
1
将第一个有理数转换成字符串型
1/3
将第一个有理数转换成double型
0.3333333333333333
将第一个有理数转换成int型
0
将第一个有理数转换成long型
0
尝试回答与c语言的有理数代码相比较,为什么你设计的类更加面向对象?
首先,C语言是基于面向过程编程,基于过程调用的编程范式,所以如果使用C语言的有理代码,就需要用怎样的函数来实现这个过程。
而Java是面向对象编程,基于对象的编程范式,程序设计基于类和对象。所以在设计类时,需要根据实际考虑本身的属性和目标来设计代码,以设计出功能性代码。
此次设计的类,是针对有理数设计出来的类,考虑了有理数的属性并且设计出了有理数针对各种问题的方法,方便随时抽取相应的属性或者方法进行编程。
尝试从代码复用的角度来描述你设计的有理数类。从几个方面讨论:
- 别人如何复用你的代码?
直接导入我的类,因为我使用public进行修饰。
- 别人的代码是否依赖你的有理数类的内部属性?当你的有理数类的属性修改时,是否会影响他人调用你有理数类的代码?
是。不会影响。因为我的代码的有理数的属性使用的是private,当修改时,不会影响他人调用我的有理数代码。
- 有理数类的public方法是否设置合适?为什么有的方法设置为private?
合适,有理数类的public方法可以方便随时抽取调用。防止他人调用时出现错误。
- 你的类里面有static属性或方法吗?如果有,为什么要设置为static的?
没有。