Ok so I'm brand new to programming and Java so go easy on me. The assignment is to take fractions and do all the operations to them, add, subtract, multiply, and divide. We are also required to do this with a test file and then the actual file.
So far I have down:
making the fractions display correctly
flipping the negative sign to the numerator
putting the fraction in lowest terms
public class TestFraction {
public static void main(String[] arg)
{
//Given example of how Fraction.common works
System.out.println("The greatest common divisor of 15 and 35 is "+Fraction.common(15,35));
System.out.println("So 15/35 is really 3/7, dividing top and bottom by "+Fraction.common(15,35));
//Test general to see if answer is displayed correctly
Fraction f1 = new Fraction( 1, 3);
if (f1.toString().equals("1/3"))
{
System.out.println("test f1.toString(): passed");
}
else
{
System.out.println("test f1.toString(): FAILED");
}
//Test to see if negative sign goes to numerator
Fraction f2 = new Fraction( 2, -5);
if (f2.toString().equals("-2/5"))
{
System.out.println("test f2.toString(): passed");
}
else
{
System.out.println("test f2.toString(): FAILED");
}
//Test to see if fraction gets put in lowest terms
Fraction f3 = new Fraction( 2, 10);
if (f3.toString().equals("1/5"))
{
System.out.println("test f3.toString(): passed");
}
else
{
System.out.println("test f3.toString(): FAILED");
}
//Multiply
//Fraction a3 = f1.multiply(f2);
//
//if (a3.toString().equals("-2/15"))
//{
// System.out.println("test a3.toString(): passed");
//}
//else
//{
// System.out.println("test a3.toString(): FAILED");
//}
}
}
There is the test file
public class Fraction
{
//positive, zero or negative numerator and positive denominator
//do not use any other instance variables
//greatest common divisor, use this to reduce a fraction to its lowest terms
private int top, bottom;
public static int common(int x, int y)
{
if ( y == 0 ) return x;
else return common(y, x%y);
}
private int numerator, denominator;
public Fraction(int numerator, int denominator)
{
this.numerator = numerator;
if (denominator == 0)
throw new RuntimeException("Attemt to create zero denominator Fraction");
else if(denominator < 0)
{
this.numerator = (-1 * numerator);
this.denominator = (-1 * denominator);
}
else if (denominator >= 0)
this.denominator = denominator;
this.top = this.numerator;
this.bottom = this.denominator;
}
public String toString()
{
return (numerator / +Fraction.common(top, bottom)) + "/" + (denominator / +Fraction.common(top, bottom));
}
}
There is the other file.
So I decided to start with multiply since that is the easiest, simply multiply numerator of fraction 1 by numerator fraction 2, same with denom. and put in lowest terms.
But I am confused on how to call my fractions f1 and f2 in the Fraction class.
Do I do??
public Fraction(int top, int bottom)
but how do I get the fractions I used in the testfraction file into the fraction file? If someone could just point me in the right direction or show me how to do multiply at least that would be greatly appreciated.
Thank you soo much