1.有理数类代码:
public class RationalNum {
private int numerator = 0;
private int denominator = 1;
public int getnumerator() {
return numerator;
}
public int getdenominator() {
return denominator;
}
public RationalNum() {
this.numerator = 0;
this.denominator = 1;
}
public RationalNum(int numerator, int denominator) { // 以分子分母形式构造
if (denominator == 0) {
System.out.println("Denominator cannot be zero!");
System.exit(1);
} else if (numerator == 0) {
this.numerator = 0;
this.denominator = 1;
}
int gcd = GCD(numerator, denominator);
this.numerator = numerator / gcd;
this.denominator = denominator / gcd;
}
public int GCD(int num1, int num2) { // 辗转相除法获取最大公约数
if (num1 < num2) {
int k = 0;
k = num1;
num1 = num2;
num2 = k;
}
return num1 % num2 == 0 ? num2 : GCD(num2, num1 % num2);
}
public RationalNum(Double num) { // 输入小数形式构造
String str = num.toString();
int index = str.indexOf('.');
int den = (int) Math.pow(10, str.length() - index);
int nrt = (int) (num * den);
int gcd = 0;
if (nrt < 0) {
gcd = GCD(-nrt, den);
} else {
gcd = GCD(nrt, den);
}
this.numerator = nrt / gcd;
this.denominator = den / gcd;
}
public RationalNum(String str) { // 输入字符串构造
Double num = Double.parseDouble(str);
int index = str.indexOf('.');
if (index == -1) {
this.numerator = Integer.parseInt(str);
this.denominator = 1;
return;
}
int den = (int) Math.pow(10, str.length() - index);
int nrt = (int) (num * den);
int gcd = 0;
if (nrt < 0) {
gcd = GCD(-nrt, den);
} else {
gcd = GCD(nrt, den);
}
this.numerator = nrt / gcd;
this.denominator = den / gcd;
}
public RationalNum add(RationalNum a) { //加法
int nrt = a.numerator * this.denominator + a.denominator * this.numerator;
int den = a.denominator * this.denominator;
RationalNum num = new RationalNum(nrt, den);
return num;
}
public RationalNum minus(RationalNum a) { //减法
int nrt = this.numerator * a.denominator - a.numerator * this.denominator;
int den = a.denominator * this.denominator;
RationalNum num = new RationalNum(nrt, den);
return num;
}
public RationalNum mulitply(RationalNum a) { //乘法
int nrt = this.numerator * a.numerator;
int den = this.denominator * a.denominator;
RationalNum num = new RationalNum(nrt, den);
return num;
}
public RationalNum divide(RationalNum a) { //除法
int nrt = this.numerator * a.denominator;
int den = this.denominator * a.numerator;
RationalNum num = new RationalNum(nrt, den);
return num;
}
public RationalNum abs() { //求绝对值
int nrt=0,den=1;
if(this.numerator<0) {
nrt=-this.numerator;
}
if(this.denominator<0) {
den=-this.denominator;
}
RationalNum num=new RationalNum(nrt,den);
return num;
}
public boolean equals(RationalNum a) { //比较两个数是否相等
if(this.divide(a).toString().equals("1")) return true;
else return false;
}
public RationalNum returnLargerNum(RationalNum a) { //返回两个数中较大的数
RationalNum num =new RationalNum(0,1);
num=this.minus(a);
if(num.intValue()<0)
{
return a;
}
else {
return this;
}
}
public int intValue() { //转化为int类型
return (int) this.numerator / this.denominator;
}
}
2.测试代码
package Main;
import rational.RationalNum;
public class Main {
public static void main(String[] args) {
int x1=-2,y1=5;
int x2=3,y2=7;
double x=3.4;
int n=3,m=0;
String num="66.66";
RationalNum a=new RationalNum(x1,y1); //-2/5
RationalNum b=new RationalNum(x2,y2); //3/7
RationalNum c=new RationalNum(x);
RationalNum d=new RationalNum(num);
System.out.println("a="+a.toString()+" ; "+"b="+b.toString()+" ; "+"c="+c.toString()+" ; "+"d="+d.toString());
System.out.println("a + b = "+a.add(b).toString());
System.out.println("a - b = "+a.minus(b).toString());
System.out.println("a * b = "+a.mulitply(b).toString());
System.out.println("a / b = "+a.divide(b).toString());
System.out.println("Absolute value of a = "+a.abs().toString());
System.out.println("The nth power of a = "+a.pow(a, n));
System.out.println("The int type of a is "+a.intValue());
System.out.println("The double type of a is "+a.doubleValue());
System.out.println("The larger number of "+a.toString()+" and "+b.toString()+" is "+a.returnLargerNum(b));
System.out.println("Is "+a.toString()+" and "+b.toString()+" the same? "+a.equals(b));
}
}
测试结果:
3.尝试描述怎么与c语言的有理数代码相比较,为什么你设计的类更加面向对象?
- C语言强调的是过程,在写代码是更侧重函数的设计编写来实现过程,而在JAVA中强调的是根据对象属性和目标来编写代码,如何让属性间相互联系,如何更好封装是考虑的重点。
4.尝试从代码复用的角度来描述你设计的有理数类。从几个方面讨论。
<1>别人如何复用你的代码?
- 通过import关键字导入包rational中的类RationalNum。
<2>别人的代码是否依赖你的有理数类的属性?当你的有理数类的属性修改时,是否会影响他人调用你有理数类的代码?
- (1)是。(2)是,当我将类中方法的入参修改后,其他人想要用这个类也要做出相应修改。
<3>有理数类的public方法是否设置合适?为什么有的方法设置为private?
- (1)合适,因为这样方便复用,而且在别的包也能调用。(2)有的方法(例如GCD)设置成private是因为获取最大公约数一般在化简分数时才有较大用处,外界一般不需要调用。