gpt4 book ai didi

java - 关于assertEquals的问题

转载 作者:行者123 更新时间:2023-12-02 06:15:34 25 4
gpt4 key购买 nike

好吧,我的任务是为 Fraction 类创建一个测试类,其中包括加法、减法、除法和乘法。我首先从 testAdd 方法开始,但似乎无论我如何尝试测试它,它总是失败。一个例子是我将预期的答案放入assertEquals(“10/8”,f2.add(f1));中它会说它失败并带有警告标志,指出预期 <10/8> 但实际为 <10/8>。我什至会使用另一个分数实例变量通过输入assertEquals(f3,f2.add(f1));来补充答案。仍然给我同样的答复。

这是 Fraction 类(已经提供了 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(){

}



}

最佳答案

Fraction 类实现 equals()hashcode() 后,执行 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) 返回 Fraction 所以因为有类型不匹配,测试失败。

关于java - 关于assertEquals的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21515190/

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