1.有理数类的代码
package cn.edu.jmu;
public class Rational {
private int numerator; //分子
private int denominator; //分母
public int getNumerator() {
return numerator;
}
public int getDenominator() {
return denominator;
}
//无参构造函数
public Rational () {
this.numerator = 0;
this.denominator = 1;
}
//有参构造函数
public Rational (int numerator, int denominator) {
if (numerator < 0 && denominator < 0) {
this.numerator = -numerator;
this.denominator = -denominator;
}else if (numerator > 0 && denominator < 0) {
this.numerator = -numerator;
this.denominator = -denominator;
}else {
this.numerator = numerator;
this.denominator = denominator;
}
}
//求最大公约数,化简用
private int gcd(int numerator, int denominator) {
int x = Math.abs(numerator);
int y = Math.abs(denominator);
int tmp;
while (y != 0) {
tmp = x % y;
x = y;
y = tmp;
}
return x;
}
//相加
public Rational add (Rational x) {
int numerator2 = numerator * x.denominator + x.numerator * denominator;
int denominator2 = denominator * x.denominator;
return new Rational(numerator2, denominator2);
}
//相减
public Rational subtract (Rational x) {
int numerator2 = numerator * x.denominator - x.numerator * denominator;
int denominator2 = denominator * x.denominator;
return new Rational(numerator2, denominator2);
}
//相乘
public Rational multiply (Rational x) {
int numerator2 = numerator * x.numerator;
int denominator2 = denominator * x.denominator;
return new Rational(numerator2, denominator2);
}
//相除
public Rational divide (Rational x) {
int numerator2 = numerator * x.denominator;
int denominator2 = denominator * x.numerator;
return new Rational(numerator2, denominator2);
}
//求绝对值
public Rational abs () {
int numerator2 = numerator;
int denominator2 = denominator;
if (numerator2 < 0) {
numerator2 = -numerator2;
}
if (denominator2 < 0) {
denominator2 = -denominator2;
}
return new Rational(numerator2, denominator2);
}
//判断是否相等
public boolean equals (Rational x) {
if (this.subtract(x).getNumerator() == 0) {
return true;
}else {
return false;
}
}
//比较大小,大于所比较的数返回1,小于返回-1,等于返回0
public int compareTo (Rational x) {
if (this.subtract(x).getNumerator() > 0) {
return 1;
}else if (this.subtract(x).getNumerator() < 0) {
return -1;
}else {
return 0;
}
}
//以字符串形式输出
public String toString () {
int t = gcd(numerator, denominator);
numerator /= t;
denominator /= t;
if(denominator == 1) {
return numerator+"";
}else if (numerator == 0) {
return 0+"";
}else {
return numerator + "/" + denominator ;
}
}
}
2.测试代码
package demo;
import cn.edu.jmu.Rational;
public class Main {
public static void main(String[] args) {
Rational s1 = new Rational(1, 2);
Rational s2 = new Rational(3, 4);
Rational s3 = new Rational(8, -7);
Rational s4 = new Rational(3, 4);
Rational s5 = new Rational();
System.out.println("s1+s2 = " + s1.add(s2).toString());
System.out.println("s1-s2 = " + s1.subtract(s2).toString());
System.out.println("s1*s2 = " + s1.multiply(s2).toString());
System.out.println("s1/s2 = " + s1.divide(s2).toString());
System.out.println("|s3| = " + s3.abs().toString());
System.out.println("判断s1与s2是否相等:" + s1.equals(s2));
System.out.println("判断s2与s4是否相等:" + s2.equals(s4));
System.out.println("s1与s2比大小:" + s1.compareTo(s2));
System.out.println("无参构造:" + s5.toString());
}
3.运行结果
4.尝试描述怎么与c语言的有理数代码相比较,为什么你设计的类更加面向对象?
c语言是以函数的形式编写,java是以类的形式编写,使用者在使用时只需导入即可,更加的方便,而且定义方法时可以进行重载,可为类似功能的方法提供统一名称,根据参数类型或个数的不同调用相对应的方法。而c语言一个函数只能有一种参数定义。
5.尝试从代码复用的角度来描述你设计的有理数类。从几个方面讨论。
a.别人如何复用你的代码?
导入对应包中的有理数类。例:import cn.edu.jmu.Rational;
b.别人的代码是否依赖你的有理数类的属性?当你的有理数类的属性修改时,是否会影响他人调用你有理数类的代码?
别人的代码依赖我的有理数类的属性。
当我的有理数类的属性修改时,因为属性设置为private,所以不影响他人调用有理数类的代码。
c.有理数类的public方法是否设置合适?为什么有的方法设置为private?
合适。
防止使用者对数据进行错误修改造成程序崩溃;辅助编写public方法的方法,不应被使用者调用。