I am building a class that gets a proper fraction converts it to improper and does things like add, multiply... I have no way of testing this since they all have methods. Could someone tell me if this works. Thanks.
// THIS CLASS DECLARES PRIVATE VARIABLES.
public class Fraction {
private int whole;
private int numerator;
private int denominator,sum,count;
// CONSTRUCTOR
public Fraction() {
}
//MAKE PROPER FRACTION IMPROPER
private void makeImproper()
{
numerator = (whole * denominator) + numerator ;
whole = 0;
}
// INTIALIZES THE PRIVATE VARIABLES.
public Fraction( int whole ,int numerator, int denominator)
{
this.whole = whole;
this.numerator = numerator;
this.denominator = denominator;
}
// ADDS THE TWO IMPROPER FRACTIONS.
public void add( Fraction f1, Fraction f2)
{
f1.makeImproper(); // CONVERTS F1 TO IMPROPER
f2.makeImproper(); // CONVERTS F2 TO IMPROPER
f1.numerator *= f2.denominator;
f1.denominator *= f2.denominator;
// SETS NUMERATOR, DENOMINATOR, AND WHOLE
numerator = f1.numerator + f2.numerator;
denominator =f1.denominator + f2.denominator;
whole = 0;
reduce(); // CALLS AND REDUCES THE NUMERATOR AND DENOMINATOR.
}
// SUBTRACTS THE TWO FRACTIONS
public void subtract (Fraction f1, Fraction f2)
{
f1.makeImproper(); // CONVERTS F1 TO IMPROPER
f2.makeImproper(); // CONVERTS F2 TO IMPROPER
f1.numerator *= f2.denominator;
f1.denominator *= f2.denominator;
// SETS NUMERATOR, DENOMINATOR, AND WHOLE
numerator = f1.numerator - f2.numerator;
denominator =f1.denominator - f2.denominator;
whole = 0;
reduce(); // CALLS AND REDUCES THE NUM AND DENOM
}
//DIVIDES
public void multiply( Fraction f1, Fraction f2 )
{
f1.makeImproper(); // CONVERTS F1 TO IMPROPER
f2.makeImproper(); // CONVERTS F2 TO IMPROPER
f1.numerator *= f2.denominator;
f1.denominator *= f2.denominator;
// SETS NUMERATOR, DENOMINATOR, AND WHOLE
numerator = f1.numerator * f2.numerator;
denominator =f1.denominator * f2.denominator;
whole = 0;
reduce(); // CALLS AND REDUCES THE NUM AND DENOM
}
// DIVIDES
public void divide( Fraction f1, Fraction f2)
{
f1.makeImproper(); // CONVERTS F1 TO IMPROPER
f2.makeImproper(); // CONVERTS F2 TO IMPROPER
f1.numerator *= f2.denominator;
f1.denominator *= f2.denominator;
// SETS NUMERATOR, DENOMINATOR, AND WHOLE
numerator = f1.numerator * f2.denominator;
denominator =f1.denominator * f2.numerator;
whole = 0;
reduce(); // CALLS AND REDUCES THE NUM AND DENOM
}
public String toString()
{
return ( whole + "&" + numerator + "/" + denominator);
}
public boolean equals(Object rhs)
{
if(!(rhs instanceof Fraction))
return false;
Fraction rhFraction = (Fraction) rhs;
return rhFraction.whole == whole &&
rhFraction.numerator == numerator &&
rhFraction.denominator == denominator;
}
private void reduce ()
{
Fraction f1 = new Fraction();
Fraction f2 = new Fraction();
Fraction test = new Fraction();
if (f1.denominator < f2.denominator)
test.count = f2.denominator;
else
test.count = test.denominator;
int j = test.count;
for (int i = 2; i < j; i++)
{
denominator /= i;
numerator /= i;
whole = 0;
}
}
}