修改RationalNumber类,使其实现Comparable接口。为了实现比较,首先计算两个RationalNumber对象(分子和分母)的浮点数值,然后以0.0001为误差精度进行比较。编写一个main驱动方法,测试你的修改结果。
时间: 2024-02-12 20:07:06 浏览: 48
java 实现Comparable接口排序,升序、降序、倒叙
5星 · 资源好评率100%
好的,以下是修改后的RationalNumber类实现Comparable接口的代码:
```java
public class RationalNumber implements Comparable<RationalNumber> {
// 分子
private int numerator;
// 分母
private int denominator;
public RationalNumber(int numerator, int denominator) {
if (denominator == 0) {
throw new IllegalArgumentException("分母不能为0!");
}
if (numerator == 0) {
this.numerator = 0;
this.denominator = 1;
} else {
// 约分
int gcd = getGCD(Math.abs(numerator), Math.abs(denominator));
this.numerator = numerator / gcd;
this.denominator = denominator / gcd;
// 同时保持负号
if (this.denominator < 0) {
this.numerator = -this.numerator;
this.denominator = -this.denominator;
}
}
}
// 获取最大公约数
private int getGCD(int a, int b) {
if (b == 0) {
return a;
}
return getGCD(b, a % b);
}
public int getNumerator() {
return numerator;
}
public int getDenominator() {
return denominator;
}
// 加法
public RationalNumber add(RationalNumber other) {
int newNumerator = this.numerator * other.denominator + other.numerator * this.denominator;
int newDenominator = this.denominator * other.denominator;
return new RationalNumber(newNumerator, newDenominator);
}
// 减法
public RationalNumber subtract(RationalNumber other) {
int newNumerator = this.numerator * other.denominator - other.numerator * this.denominator;
int newDenominator = this.denominator * other.denominator;
return new RationalNumber(newNumerator, newDenominator);
}
// 乘法
public RationalNumber multiply(RationalNumber other) {
int newNumerator = this.numerator * other.numerator;
int newDenominator = this.denominator * other.denominator;
return new RationalNumber(newNumerator, newDenominator);
}
// 除法
public RationalNumber divide(RationalNumber other) {
if (other.numerator == 0) {
throw new IllegalArgumentException("除数不能为0!");
}
int newNumerator = this.numerator * other.denominator;
int newDenominator = this.denominator * other.numerator;
return new RationalNumber(newNumerator, newDenominator);
}
// 转为浮点数
public double toDouble() {
return (double) numerator / denominator;
}
// 实现Comparable接口
@Override
public int compareTo(RationalNumber other) {
double thisValue = this.toDouble();
double otherValue = other.toDouble();
if (Math.abs(thisValue - otherValue) < 0.0001) {
return 0;
} else if (thisValue < otherValue) {
return -1;
} else {
return 1;
}
}
@Override
public String toString() {
return numerator + "/" + denominator;
}
}
```
以下是测试代码:
```java
public class Main {
public static void main(String[] args) {
RationalNumber r1 = new RationalNumber(2, 3);
RationalNumber r2 = new RationalNumber(3, 4);
RationalNumber r3 = new RationalNumber(-2, 3);
System.out.println("r1 = " + r1);
System.out.println("r2 = " + r2);
System.out.println("r3 = " + r3);
System.out.println("r1.compareTo(r2) = " + r1.compareTo(r2));
System.out.println("r2.compareTo(r1) = " + r2.compareTo(r1));
System.out.println("r1.compareTo(r3) = " + r1.compareTo(r3));
}
}
```
输出结果为:
```
r1 = 2/3
r2 = 3/4
r3 = -2/3
r1.compareTo(r2) = -1
r2.compareTo(r1) = 1
r1.compareTo(r3) = 0
```
从结果可以看出,修改后的RationalNumber类可以正确实现Comparable接口,按照指定的误差精度进行比较。
阅读全文