Question

Ok I am tasked with making a test class for a class Fraction that includes addition, subtraction, division, and multiplication. I started with the testAdd method first, but seems no matter how I try to approach testing it, it always fails. An example would be that i put the expected answer in assertEquals("10/8",f2.add(f1)); and it would say that it fails with the warning sign stating expected <10/8> but actual was <10/8>. I would even use another fraction instance variable to supplement the answer by typing assertEquals(f3,f2.add(f1)); and still give me the same reply.

Here is the Fraction class(fraction class was already provided):

    // 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

I also have the package in the class so that is not the problem. Here is the testclass I have tried to create:

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(){

  }



}
Was it helpful?

Solution 3

Without equals(), you're relying on Object.equals() for testing equality. This will return true only if two Fractions are the same object. You want them to be equal if they represent the same value.

When writing equals(), you have to write hashCode() too, according to the contract of those methods.

And when writing equals(), don't forget that new Fraction(5, 4).equals(new Fraction(20, 16)) == true.

OTHER TIPS

After implementing equals() and hashcode() for class Fraction, do assertEquals(new Fraction(10,8), f2.add(f1));

Or if you're not sure about equals() and hashcode() methods, just hack out assertEquals("10/8", f2.add(f1).toString());

The add method returns Fraction but when you write assertEquals("10/8", f2.add(f1)), the "10/8" is of String type but f2.add(f1) is returning Fraction so since there's a type mismatch, test fails.

You need to create equals() and hashCode() methods for your Fraction class.

For instance, your equals() method would look something like this:

public boolean equals(Object other) {
    if (!(other instanceof Fraction)) {
        return false;
    }
    otherFraction = (Fraction) other;
    return numerator * otherFraction.denominator == otherFraction.numerator * denominator;

While your hashCode() method would look something like this:

public int hashCode() {
    return new Double((double) numerator / denominator).hashCode();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top