用java 创建一个Fraction类执行分数运算。要求如下: ①定义类的private成员变量:分子f1和分母£2: ②提供构造方法,创建分数; ③提供两个分数相加的运算方法; ④提供两个分数相减的运算方法; ⑤提供两个分数相乘的运算方法; ⑥提供两个分数相除的运算方法; ⑦以a/的形式打印Fraction数; ⑧以浮点数的形式打印Fraction数; ⑨主程序中创建分数,完成分数运算,输出结果
时间: 2024-01-22 19:20:04 浏览: 78
设计一个表示分数的类Fraction。这个类用两个int类型的变量分别表示分子和分母。
5星 · 资源好评率100%
下面是Fraction类的实现:
```java
public class Fraction {
private int numerator; // 分子
private int denominator; // 分母
// 构造方法
public Fraction(int numerator, int denominator) {
if (denominator == 0) {
throw new IllegalArgumentException("分母不能为0");
}
if (numerator == 0) {
this.numerator = 0;
this.denominator = 1;
} else {
int gcd = gcd(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 gcd(int a, int b) {
if (a < b) {
return gcd(b, a);
}
while (b != 0) {
int r = a % b;
a = b;
b = r;
}
return a;
}
// 分数加法
public Fraction add(Fraction other) {
int newNumerator = this.numerator * other.denominator + other.numerator * this.denominator;
int newDenominator = this.denominator * other.denominator;
return new Fraction(newNumerator, newDenominator);
}
// 分数减法
public Fraction subtract(Fraction other) {
int newNumerator = this.numerator * other.denominator - other.numerator * this.denominator;
int newDenominator = this.denominator * other.denominator;
return new Fraction(newNumerator, newDenominator);
}
// 分数乘法
public Fraction multiply(Fraction other) {
int newNumerator = this.numerator * other.numerator;
int newDenominator = this.denominator * other.denominator;
return new Fraction(newNumerator, newDenominator);
}
// 分数除法
public Fraction divide(Fraction other) {
if (other.numerator == 0) {
throw new IllegalArgumentException("除数不能为0");
}
int newNumerator = this.numerator * other.denominator;
int newDenominator = this.denominator * other.numerator;
return new Fraction(newNumerator, newDenominator);
}
// 以a/b的形式打印分数
public String toString() {
if (denominator == 1) {
return Integer.toString(numerator);
} else {
return numerator + "/" + denominator;
}
}
// 以浮点数的形式打印分数
public double toDouble() {
return (double) numerator / denominator;
}
}
```
然后可以编写一个测试程序来测试Fraction类:
```java
public class TestFraction {
public static void main(String[] args) {
Fraction f1 = new Fraction(1, 2);
Fraction f2 = new Fraction(3, 4);
// 分数加法
Fraction sum = f1.add(f2);
System.out.println(f1 + " + " + f2 + " = " + sum);
// 分数减法
Fraction diff = f1.subtract(f2);
System.out.println(f1 + " - " + f2 + " = " + diff);
// 分数乘法
Fraction product = f1.multiply(f2);
System.out.println(f1 + " * " + f2 + " = " + product);
// 分数除法
Fraction quotient = f1.divide(f2);
System.out.println(f1 + " / " + f2 + " = " + quotient);
// 以a/b的形式打印分数
System.out.println(f1 + " = " + f1.toString());
System.out.println(f2 + " = " + f2.toString());
// 以浮点数的形式打印分数
System.out.println(f1 + " = " + f1.toDouble());
System.out.println(f2 + " = " + f2.toDouble());
}
}
```
输出结果为:
```
1/2 + 3/4 = 5/4
1/2 - 3/4 = -1/4
1/2 * 3/4 = 3/8
1/2 / 3/4 = 2/3
1/2 = 1/2
3/4 = 3/4
1/2 = 0.5
3/4 = 0.75
```
阅读全文