0

They are two classes. Fraction and calculator. Fraction does things like add, multiply....

The calculator class is supposed to call the methods in the Fraction class and output the data. The problem is i can't figure out where to put the calculator class so that it can output data. Here is the code for the fraction and calculator class. I have tested the calculator code in the main of the fraction and it works. I can't use inheritance due to assignment specifications.

////////////////////////////////////////FRACTION CLASS/////////////////////////////////////////////

```
public class Fraction {
private int whole;
private int numerator;
private int denominator, count;
// UNINITIALIZED 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)
{
Fraction temp = new Fraction(f1.whole, f1.numerator, f1.denominator);
Fraction temp2 = new Fraction(f2.whole, f2.numerator, f2.denominator);
temp.makeImproper(); // CONVERTS F1 TO IMPROPER
temp2.makeImproper(); // CONVERTS F2 TO IMPROPER
// SETS NUMERATOR, DENOMINATOR, AND WHOLE
numerator = (temp.numerator *temp2.denominator)
+ (temp.denominator *temp2.numerator);
denominator = temp.denominator * temp2.denominator;
whole = 0;
reduce(); // CALLS AND REDUCES THE NUMERATOR AND DENOMINATOR.
}
// SUBTRACTS THE TWO FRACTIONS
public void subtract (Fraction f1, Fraction f2)
{
Fraction temp = new Fraction(f1.whole, f1.numerator, f1.denominator);
Fraction temp2 = new Fraction(f2.whole, f2.numerator, f2.denominator);
temp.makeImproper(); // CONVERTS F1 TO IMPROPER
temp2.makeImproper(); // CONVERTS F2 TO IMPROPER
// SETS NUMERATOR, DENOMINATOR, AND WHOLE
numerator = (temp.numerator *temp2.denominator)
- (temp.denominator *temp2.numerator);
denominator = temp.denominator * temp2.denominator;
whole = 0;
reduce(); // CALLS AND REDUCES THE NUM AND DENOM
}
//DIVIDES
public void multiply( Fraction f1, Fraction f2 )
{
Fraction temp = new Fraction(f1.whole, f1.numerator, f1.denominator);
Fraction temp2 = new Fraction(f2.whole, f2.numerator, f2.denominator);
temp.makeImproper(); // CONVERTS F1 TO IMPROPER
temp2.makeImproper(); // CONVERTS F2 TO IMPROPER
// SETS NUMERATOR, DENOMINATOR, AND WHOLE
numerator = temp.numerator * temp2.numerator;
denominator = temp.denominator * temp2.denominator;
whole = 0;
reduce(); // CALLS AND REDUCES THE NUM AND DENOM
}
// DIVIDES
public void divide( Fraction f1, Fraction f2)
{
Fraction temp = new Fraction(f1.whole, f1.numerator, f1.denominator);
Fraction temp2 = new Fraction(f2.whole, f2.numerator, f2.denominator);
temp.makeImproper(); // CONVERTS F1 TO IMPROPER
temp2.makeImproper(); // CONVERTS F2 TO IMPROPER
// SETS NUMERATOR, DENOMINATOR, AND WHOLE
numerator = temp.numerator * temp2.denominator;
denominator = temp.denominator * temp2.numerator;
whole = 0;
reduce(); // CALLS AND REDUCES THE NUM AND DENOM
}
public void output ()
{
System.out.print(toString());
}
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 ()
{
if (this.denominator > this.numerator)
count = this.denominator;
else
count = this.numerator;
for (int i = count; i > 1; i--)
{
if(denominator %i == 0 && numerator%i == 0)
{
denominator /= i;
numerator /= i;
whole = numerator%denominator;
numerator = numerator - (whole * denominator);
}
}
}
```

//////////////////////////////////////////////////////////////////////////////

//////////////////CALCULATORCLASS/////////////////////////////////////////

```
class Calculato
{
public void main (String [] args)
{
// public static void main ( String [] args )
// {
BufferedReader in = new BufferedReader( new InputStreamReader(System.in));
Scanner stdin = new Scanner(System.in);
System.out.println("Enter several integers separated by &, /, spaces");
String input = stdin.nextLine();
String delims = "&/ ";
String operators = "-*+%";
StringTokenizer st = new StringTokenizer(input, delims);
StringTokenizer op = new StringTokenizer(input, operators, true);
//2&5/8 - 1&1/8
int whole = Integer.parseInt(st.nextToken()); // whole
int numerator = Integer.parseInt(st.nextToken()); // numerator
int denominator = Integer.parseInt(st.nextToken()); // denominator
char del = ( op.nextToken() ).charAt( 0 );
del = ( st.nextToken() ).charAt( 0 );
int whole2 = Integer.parseInt(st.nextToken()); // whole
int numerator2 = Integer.parseInt(st.nextToken()); // numerator
int denominator2 = Integer.parseInt(st.nextToken()); // denominator
////////////////////////////////////////
Fraction f1 = new Fraction(whole,numerator,denominator); //1,2,5
Fraction f2 = new Fraction(whole2,numerator2,denominator2); //1,4,5
Fraction f3 = new Fraction ();
/////////////////////////////////////////
switch (del)
{
case '+':
System.out.println("ADDS");
f3.add (f1, f2);
f3.output();
break;
case '-':
System.out.println("Minus");
f3.subtract(f1,f2);
f3.output();
break;
case '*':
System.out.println("Multiply");
f3.multiply(f1,f2);
f3.output ();
break;
case '%':
System.out.println("divides");
f3.divide(f1,f2);
f3.output();
break;
default :
System.out.println("Wrong operator ");
}
}
}
}
```