好的我的任务是为类Fraction制作一个测试类,包括加法,减法,除法和乘法。我首先使用testAdd方法开始,但无论我如何尝试进行测试,它总是失败。一个例子是我把预期的答案放在assertEquals(“10/8”,f2.add(f1));并且它会说它失败并带有预期的警告标志< 10/8>但实际是< 10/8>。我甚至会通过输入assertEquals(f3,f2.add(f1))来使用另一个分数实例变量来补充答案。并且仍然给我同样的答复。
这是Fraction类(已经提供了分数类):
// import ArithmeticException class for use in division method
import java.lang.ArithmeticException;
public class Fraction
{
/**
* Instance Variables
**/
private int numerator; // Stores the numerator of the fraction
private int denominator; // Stores the denominator of the fraction
/**
* Constructor
* Takes two integer arguments, the numerator and denominator for the fraction
* being created.
**/
public Fraction(int numerator, int denominator)
{
// initialize instance variables
this.numerator = numerator;
this.denominator = denominator;
}
// end constructor
/**
* Additon Method
* Takes one Fraction argument, calculates the sum of the
* calling Fraction object and its argument, constructs a new Fraction
* object that stores the sum, and returns this new Fraction object.
**/
public Fraction add( Fraction otherFraction )
{
// declare and initialize local variables for the numerator and denominator
int commonDenominator = this.denominator * otherFraction.denominator;
int newNumerator = ( this.numerator * otherFraction.denominator ) + ( otherFraction.numerator * this.denominator );
// Declare and initialize resulting Fraction object using the above numerator and denominator
Fraction result = new Fraction( newNumerator, commonDenominator );
return result;
}
// end add method
/**
* Subtraction Method
* Takes one Fraction argument, calculates the difference of the
* calling Fraction object and its argument, constructs a new Fraction
* object that stores the difference, and returns this new Fraction object.
**/
public Fraction subtract( Fraction otherFraction )
{
// declare and initialize local variables for the numerator and denominator
int commonDenominator = this.denominator * otherFraction.denominator;
int newNumerator = ( this.numerator * otherFraction.denominator ) - ( otherFraction.numerator * this.denominator );
// Declare and initialize resulting Fraction object using the above numerator and denominator
Fraction result = new Fraction( newNumerator, commonDenominator );
return result;
}
// end subtract method
/**
* Multiplication Method
* Takes one Fraction argument, calculates the multiple of the
* calling Fraction object and its argument, constructs a new Fraction
* object that stores the multiple, and returns this new Fraction object.
**/
public Fraction multiply( Fraction otherFraction )
{
// declare and initialize local variables for the numerator and denominator
int newNumerator = this.numerator * otherFraction.numerator;
int newDenominator = this.denominator * otherFraction.denominator;
// Declare and initialize resulting Fraction object using the above numerator and denominator
Fraction result = new Fraction( newNumerator, newDenominator );
return result;
}
// end multiply method
/**
* Division Method
* Takes one Fraction argument, calculates the dividend of the
* calling Fraction object and its argument, constructs a new Fraction
* object that stores the dividend, and returns this new Fraction object.
**/
public Fraction divide( Fraction otherFraction ) throws ArithmeticException
{
// If the nominator of the divisor is zero throw a division by zero exception
if( otherFraction.numerator == 0 )
{
throw new ArithmeticException( "Division by Zero" );
}
// Construct a new Fraction object that is the inverse of the divisor
Fraction inverse = new Fraction( otherFraction.denominator, otherFraction.numerator );
// Calculate the result of the division by multiplying by the inverse of the divisor
// and store the result in a new Fraction object.
Fraction result = this.multiply( inverse);
return result;
}
// end divide method
/**
* String Conversion Method
* Uses the state of the object (the numerator and denominator), converts
* them to strings and the returns a String representation of the Fraction
* object.
**/
public String toString()
{
String text = Integer.toString(this.numerator) + "/" + Integer.toString(this.denominator);
return text;
}
// end toString method
}
// END CLASS FRACTION
我也有类中的包,所以这不是问题。 这是我试图创建的测试类:
import junit.framework.TestCase;
public class TestFraction extends TestCase{
Fraction f1;
Fraction f2;
Fraction f3;
Fraction f4;
Fraction result1;
Fraction result2;
Fraction result3;
Fraction result4;
protected void setUp(){
f1 = new Fraction(1,2);
f2 = new Fraction(3,4);
f3 = new Fraction(10,8);
f4 = new Fraction(2,3);
}
public void testAdd(){
assertEquals(,f2.add(f1));
}
public void testSubtract(){
}
public void testDivide(){
}
public void testMultiply(){
}
}
答案 0 :(得分:1)
为课程equals()
实施hashcode()
和Fraction
后,执行assertEquals(new Fraction(10,8), f2.add(f1));
或者,如果您不确定equals()
和hashcode()
方法,请立即退出
assertEquals("10/8", f2.add(f1).toString());
add
方法返回Fraction
但是当您撰写assertEquals("10/8", f2.add(f1))
时,"10/8"
属于String
类型,但f2.add(f1)
正在返回{{ 1}}因为类型不匹配,测试失败。
答案 1 :(得分:1)
您需要为Fraction类创建equals()
和hashCode()
方法。
例如,您的equals()
方法看起来像这样:
public boolean equals(Object other) {
if (!(other instanceof Fraction)) {
return false;
}
otherFraction = (Fraction) other;
return numerator * otherFraction.denominator == otherFraction.numerator * denominator;
虽然您的hashCode()
方法看起来像这样:
public int hashCode() {
return new Double((double) numerator / denominator).hashCode();
}
答案 2 :(得分:0)
如果没有equals()
,您依靠Object.equals()
来测试相等性。仅当两个分数是同一个对象时,才会返回true。如果它们代表相同的值,您希望它们相等。
撰写equals()
时,您必须根据这些方法的合同编写hashCode()
。
在撰写equals()
时,请不要忘记new Fraction(5, 4).equals(new Fraction(20, 16)) == true
。