Write tests for the Fraction class definition discussed in lecture using the exp
ID: 3906720 • Letter: W
Question
Write tests for the Fraction class definition discussed in lecture using the expected value actual value method of testing. For example if you created a method int mult (int a, int b),to test if it worked you would create a variable actual and a variable expected and compare them like so: int actual = mult (2, 3); int expected-6; boolean result - expected -- actual: System. out.println("TestMult:"+ result); Doing this allows for quicker evaluation of the tests, since you don't need to inspect the results any further than looking for any test that returned false.Explanation / Answer
/*FractionTests.java*/
public class FractionTests
{
// Expected output:
// f1 = 2/3
// f2 = 4/5
// f1 ~ 0.6666666666666666
// f3 = 8/15
// f1 = 8/15
// f4 = 1/2
public static void main(String[] args)
{
Fraction f1 = new Fraction();
f1.setNumerator(2);
f1.setDenominator(3);
String actual=new String(f1.getNumerator()+"/"+f1.getDenominator());
String expected=new String("2/3");
System.out.println("f1 test:"+actual.equals(expected));
System.out.print("f1 = ");
System.out.print(f1.getNumerator());
System.out.print("/");
System.out.println(f1.getDenominator());
Fraction f2 = new Fraction();
f2.setNumerator(4);
f2.setDenominator(5);
actual=new String(f2.getNumerator()+"/"+f2.getDenominator());
expected=new String("4/5");
System.out.println("f2 test:"+actual.equals(expected));
System.out.print("f2 = ");
System.out.print(f2.getNumerator());
System.out.print("/");
System.out.println(f2.getDenominator());
double f1Approx = f1.toDouble();
double Expected= 0.6666666666666666;
double Actual= f1Approx;
System.out.println("f1 approx test:"+(Actual==Expected));
System.out.print("f1 ~ ");
System.out.println(f1Approx);
Fraction f3 = f1.times(f2);
// f3.numerator is 8
// f3.denominator is 15
// f1 is still 2/3 and f2 is still 4/5
actual=new String(f3.getNumerator()+"/"+f3.getDenominator());
expected=new String("8/15");
System.out.println("f3 fraction test:"+actual.equals(expected));
System.out.print("f3 = ");
System.out.print(f3.getNumerator());
System.out.print("/");
System.out.println(f3.getDenominator());
Fraction f4 = Fraction.multiplication(f1, f2);
// f4.numerator is 8
// f4.denominator is 15
// f1 is still 2/3 and f2 is still 4/5
actual=new String(f4.getNumerator()+"/"+f4.getDenominator());
expected=new String("8/15");
System.out.println("f4 multiplication test:"+actual.equals(expected));
System.out.print("f4 = ");
System.out.print(f4.getNumerator());
System.out.print("/");
System.out.println(f4.getDenominator());
f1.multiply(f2);
// f1.numerator is now 8
// f1.denominator is now 15
actual=new String(f1.getNumerator()+"/"+f1.getDenominator());
expected=new String("8/15");
System.out.println("f1 multiplication test:"+actual.equals(expected));
System.out.print("f1 = ");
System.out.print(f1.getNumerator());
System.out.print("/");
System.out.println(f1.getDenominator());
Fraction f5 = new Fraction();
f5.setNumerator(3);
f5.setDenominator(6);
f5.reduce();
// numerator is now 1, denominator is 2.
actual=new String(f5.getNumerator()+"/"+f5.getDenominator());
expected=new String("1/2");
System.out.println("f5 reduce test:"+actual.equals(expected));
System.out.print("f5 = ");
System.out.print(f5.getNumerator());
System.out.print("/");
System.out.println(f5.getDenominator());
if (f1.equals(f2))
{
System.out.println("f1 and f2 are equal.");
}
else
{
System.out.println("f1 and f2 are not equal.");
}
if (f1.equals(f3))
{
System.out.println("f1 and f3 are equal.");
}
else
{
System.out.println("f1 and f3 are not equal.");
}
if (f1.equals("Hello, World"))
{
System.out.println("f1 and "Hello, World" are equal.");
}
else
{
System.out.println("f1 and "Hello, World" are not equal.");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.