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 ");
        
    }
    }   
    }  

}

Recommended Answers

All 4 Replies

what exactly is your question?

why can't the classes be separate and you just put each class in its own file?

also the "main" method should be static

The calculator class should be in the fraction class but when i try to put in there it won't give me any output. How do i get around that?

The whole thing works but due to my assignment specs I have to do it that way.

Well, Calculator actually uses Fraction, so really Fraction should be an inner class of Calculator if you have to combine them.

Thanks that worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.