Java 使用有理数的方法
Java Methods Using Rationals
我有一个理性的class和一个主要的class。我的很多代码都是在 rational class 中完成的,然后我在 main class 中调用它。然而,一些有理数并没有像实数那样打印出来。它们看起来像这样:rational.Rational@7852e922。我不知道如何正确打印它们。此外,在添加方法中添加有理数后,我无法弄清楚如何使用我的 reduce 方法。 main class 代码的最后一行出现错误。
public class Rational {
/**
* @param args the command line arguments
*/
double num;
double den;
public Rational() {
this.num = 0.0;
this.den = 0.0;
}
public static void printRational(Rational r) {
System.out.println(r.num + " / " + r.den);
}
public Rational(double num, double den) {
this.num = num;
this.den = den;
}
public static void negate(Rational r) {
r.num = -r.num;
}
public static double invert(Rational r) {
return Math.pow(r.num / r.den, -1);
}
public double toDouble() {
return (double) num / den;
}
public static int gcd(int n, int d) {
if (n < 0) {
n = -n;
}
if (d < 0) {
d = -d;
}
return n * (d / gcd (n, d));
}
public Rational reduce() {
double g = num;
double gcd = den;
double tmp;
if (g < gcd) {
tmp = g;
g = gcd;
gcd = tmp;
}
while (g != 0) {
tmp = g;
g = gcd % g;
gcd = tmp;
}
return new Rational(num / gcd, den / gcd);
}
public static Rational add(Rational a, Rational b) {
double den = a.den * b.den;
double num = a.num * b.num;
return new Rational(num, den);
}
}
这是我的主class。
public class Main {
public static void main(String[] args) {
Rational x = new Rational();
x.num = 2.0;
x.den = 3.0;
System.out.println(x);
System.out.println();
Rational.negate(x);
System.out.println(x);
System.out.println();
Rational y = new Rational(2.0, 3.0);
System.out.println(Rational.invert(y));
System.out.println();
System.out.println(y.toDouble());
System.out.println();
Rational z = new Rational();
Rational w = new Rational();
z.num = 5.0;
z.den = 3.0;
w.num = 4.0;
w.den = 3.0;
Rational.add(z, w);
System.out.println(Rational.add(z, w));
System.out.println(Rational.reduce(z,w));
}
}
您需要重写 Rational 中的 toString()
方法。
例如
public String toString() {
return num + " / " + den;
}
将此方法添加到您的 Rational
class:
@Override
public String toString() {
return printRational(this);
}
toString
的默认实现只会给出 class 名称及其哈希码的十六进制字符串。
我有一个理性的class和一个主要的class。我的很多代码都是在 rational class 中完成的,然后我在 main class 中调用它。然而,一些有理数并没有像实数那样打印出来。它们看起来像这样:rational.Rational@7852e922。我不知道如何正确打印它们。此外,在添加方法中添加有理数后,我无法弄清楚如何使用我的 reduce 方法。 main class 代码的最后一行出现错误。
public class Rational {
/**
* @param args the command line arguments
*/
double num;
double den;
public Rational() {
this.num = 0.0;
this.den = 0.0;
}
public static void printRational(Rational r) {
System.out.println(r.num + " / " + r.den);
}
public Rational(double num, double den) {
this.num = num;
this.den = den;
}
public static void negate(Rational r) {
r.num = -r.num;
}
public static double invert(Rational r) {
return Math.pow(r.num / r.den, -1);
}
public double toDouble() {
return (double) num / den;
}
public static int gcd(int n, int d) {
if (n < 0) {
n = -n;
}
if (d < 0) {
d = -d;
}
return n * (d / gcd (n, d));
}
public Rational reduce() {
double g = num;
double gcd = den;
double tmp;
if (g < gcd) {
tmp = g;
g = gcd;
gcd = tmp;
}
while (g != 0) {
tmp = g;
g = gcd % g;
gcd = tmp;
}
return new Rational(num / gcd, den / gcd);
}
public static Rational add(Rational a, Rational b) {
double den = a.den * b.den;
double num = a.num * b.num;
return new Rational(num, den);
}
}
这是我的主class。
public class Main {
public static void main(String[] args) {
Rational x = new Rational();
x.num = 2.0;
x.den = 3.0;
System.out.println(x);
System.out.println();
Rational.negate(x);
System.out.println(x);
System.out.println();
Rational y = new Rational(2.0, 3.0);
System.out.println(Rational.invert(y));
System.out.println();
System.out.println(y.toDouble());
System.out.println();
Rational z = new Rational();
Rational w = new Rational();
z.num = 5.0;
z.den = 3.0;
w.num = 4.0;
w.den = 3.0;
Rational.add(z, w);
System.out.println(Rational.add(z, w));
System.out.println(Rational.reduce(z,w));
}
}
您需要重写 Rational 中的 toString()
方法。
例如
public String toString() {
return num + " / " + den;
}
将此方法添加到您的 Rational
class:
@Override
public String toString() {
return printRational(this);
}
toString
的默认实现只会给出 class 名称及其哈希码的十六进制字符串。