我正在创建要使用jUnit测试进行测试的代码。我想在两个分数之间做基本运算,例如加法,减法,乘法和除法。我在使用这些对象时遇到了麻烦,我总是总是返回我开始的内容,而不是预期的结果。
我目前正在使用一种快速的主要方法进行测试。我的toString方法可以正常工作,构造函数方法也可以。我只是对操作方法有疑问。它永远不会将我的对象更改为正确的答案。
因此,仅举例来说,它应打印出“分子为2,分母为3”。我只是不断取回1和3。
public class FractionalNumber {
private int numerator;
private int denominator;
/*
* Sets default values for numerator & denominator */
public FractionalNumber(){
numerator = 1;
denominator = 1;
}
/*
Precondition: denominator cannot be 0
@param num the numerator of the fraction
@param denom the denominator of the fraction, may not be 0 */
public FractionalNumber(int num, int denom){
this.numerator = num;
if(denom != 0)
this.denominator = denom;
else
throw new IllegalArgumentException("Denominator cannot be 0.");
}
/*
* Adjusts the sign of the numerator and denominator so the
* denominator is positive, if necessary */
private void normalize(){
if((denominator < 0 && numerator < 0) || denominator < 0){
denominator *= -1;
numerator *= -1;
}
}
/*
* Adds the paramter to this fractional number
* Precondition: this and otherNum are valid FractionalNumbers
* Postcondition: this is the sum of the two fractions
* Class Invariant: otherNum is not changed
* @param otherNum a FractionalNumber*/
public void add(final FractionalNumber otherNum){
FractionalNumber num1 = new FractionalNumber();
FractionalNumber num2 = otherNum;
num1.numerator = num1.numerator * num2.denominator + num2.numerator * num1.denominator;
num1.denominator = num1.denominator * num2.denominator;
FractionalNumber sum = new FractionalNumber();
sum.numerator = num1.numerator;
sum.denominator = num1.denominator;
}
public void subtract(final FractionalNumber otherNum){
FractionalNumber num1 = new FractionalNumber();
num1.numerator = num1.numerator * otherNum.denominator - otherNum.numerator * num1.denominator;
num1.denominator = num1.denominator * otherNum.denominator;
num1.normalize();
}
public void multiply(final FractionalNumber otherNum){
FractionalNumber num1 = new FractionalNumber();
num1.numerator = num1.numerator * otherNum.numerator;
num1.denominator = num1.denominator * otherNum.denominator;
num1.normalize();
}
public void divide(final FractionalNumber otherNum){
if (otherNum.numerator == 0 || otherNum.denominator == 0){
FractionalNumber num1 = new FractionalNumber();
num1.numerator = num1.numerator * otherNum.denominator;
num1.denominator = num1.denominator * otherNum.numerator;
num1.normalize();
}
}
public int getNumerator(){
return numerator;
}
public int getDenominator(){
return denominator;
}
public void setNumerator(int num){
numerator = num;
}
public void setDenominator(int denom){
denominator = denom;
}
/*
* @return true if this fraction and other would reduce to the
* same numerator and denominator, false otherwise
* @param other a FractionalNumber */
@Override
public boolean equals(Object other){
if (this == other) return true;
if (other == null) return false;
if(!(other instanceof FractionalNumber)) return false;
FractionalNumber otherNum = (FractionalNumber) other;
return numerator * otherNum.getDenominator() == otherNum.getNumerator() * denominator;
}
@Override
public String toString(){
String number = "numerator is " + numerator + " and denominator is " + denominator;
return number;
}
public static void main(String[] args){
FractionalNumber num = new FractionalNumber(1, 3);
FractionalNumber num1 = new FractionalNumber(1, 3);
num.add(num1);
System.out.println(num);
}
}