gpt4 book ai didi

java - 测试类使用的主要方法

转载 作者:行者123 更新时间:2023-11-28 21:25:24 25 4
gpt4 key购买 nike

你好,我有我的主类,但我无法弄清楚如何从我的测试类输出我的代码。我不明白,尽管尝试了很多方法如何输出两个分数的简单加法和减法,因为它应该在我的主要方法中做,但似乎无法将它放入我的测试类中。

这是我为该类编写的具有所有功能的代码:

package rational;

public class Rational {

private int numer, denom;

//constructors
public Rational(){
int num = 1;
int den = 2;
reduce();
}
public Rational(int num, int den){
numer = num;
denom = den;
reduce();
}
public Rational(Rational x){
numer = x.numer;
denom = x.denom;
reduce();
}

//setters
public void setNumer(int num){
numer = num;
reduce();
}
public void setDenom(int den){
denom = den;
reduce();
}
public void setRational(int num, int den){
numer = num;
denom = den;
reduce();
}

//getters
public int getNumer(){
return numer;
}
public int getDenom(){
return denom;
}

//Copy method
public void copyFrom(Rational x){
numer = x.numer;
denom = x.denom;
reduce();
}

//Equals method
public boolean equals(Rational x){
if (numer / denom == x.numer / x.denom){
return(true);
}
else {
return(false);
}
}

//Compare to method
public int compareTo(Rational x){
if (numer / denom == x.numer / x.denom){
return (0);
}
else if (numer / denom < x.numer / x.denom){
return (-1);
}
else{
return (1);
}
}

//Find greatest common divisor
static int gcd(int x, int y){
int r;
while (y != 0) {
r = x % y;
x = y;
y = r;
}
return x;
}

//Rational Addition
public void plus(Rational x){
int greatdenom = x.denom * denom;
int multx = greatdenom / x.denom;
int mult = greatdenom / denom;
denom = x.denom * denom;
numer = (x.numer * multx) + (numer * mult);
reduce();
}

//Rational Subtraction
public void minus(Rational x){
int greatdenom = x.denom * denom;
int multx = greatdenom / x.denom;
int mult = greatdenom / denom;
denom = x.denom * denom;
if (x.numer > numer){
numer = (x.numer * multx) - (numer * mult);
}
else {
numer = (numer * mult) - (x.numer * multx);
}
reduce();
}

//Multiplication
public void times(Rational x){
numer = numer * x.numer;
denom = denom * x.denom;
reduce();
}

//Division
public void divBy(Rational x){
numer = numer / x.numer;
denom = denom / x.denom;
reduce();
}

//Fraction simplifier
private void reduce(){
int divisor;
divisor = Rational.gcd(numer, denom);
numer = numer / divisor;
denom = denom / divisor;
}

@Override
public String toString(){
if (denom == 1){
return numer + "";
}
else{
return numer + " / " + denom;
}
}
}

最佳答案

您需要在 plus 方法中返回一个 Rational(而不是 void)。

public Rational plus(Rational x){
//do addition stuff.
return new Rational(//what numerator be//,//what denom should be//)
}

作为一个建议,我会将所有这些运算符方法设为静态并接受 2 个参数。然后,您将为两个有理数的分子和分母分别使用“ setter/getter ”。

像这样:

public static Rational plus(Rational r1, Rational r2) {
int r1Num=r1.getNum();
int r1Denom=r1.getDenom();
int r2Num=r2.getNum();
int r2Denom=r2.getDenom();
//do all your plus stuff
return new Rational(//new num, //new denom);
}

关于java - 测试类使用的主要方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41971446/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com