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

I think this is an error. Maybe not, but it's still a bad idea and is confusing. In this code:

// INTIALIZES THE PRIVATE VARIABLES.
public Fraction( int whole ,int numerator, int denominator)
{
this.whole = whole;
this.numerator = numerator;
this.denominator = denominator;
}

You are naming the parameters you are passing the constructor the same names as the private data members themselves. You should give them slightly different names to avoid confusion.

and why can't you test methods?

your add(), subtract(), multiply(), and divide() methods modify their arguments, without changing them back; i am not sure if this is what you want

i don't understand your reduce() method. it creates three new fractions (initialized to 0/0), then tests the denominators of two of them (which of course are both 0), and then sets the "count" field on a third (to 0 of course), and then the loop never runs. this code must be incorrect

also, the count field is used nowhere else and the sum field is never used; perhaps these shouldn't be instance variables

I am new to this and don't know how. Since it's void I know it can't return anything. Could you show me how to test one of these.

I am new to this and don't know how. Since it's void I know it can't return anything. Could you show me how to test one of these.

Write a function within the class that displays the fraction and write a main function that does some fraction manipulations and calls the function that displays the fraction. See if the output is what it should be.

Write a function within the class that displays the fraction and write a main function that does some fraction manipulations and calls the function that displays the fraction. See if the output is what it should be.

Call me retarded, but I can't figure out how to test this thing. It keeps giving me an error oF void type not allowed here. Please show me how to test the add. Just one, and I will be fine. Here is the corrected code.

// THIS CLASS DECLARES PRIVATE VARIABLES.
 public class Fraction {
  
  private int whole;
  private int numerator;
  private int denominator, sum,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 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 = 0;
        
            }
            
        }
    } 
 }

Add this method to the Fraction class:

public void Display ()
{
    System.out.println (toString());
}

At the top of your Fraction.java class, put this line:

package fraction;

Add this file called Main.java

// Filename: Main.java
package fraction;

public class Main 
{
    public static void main(String[] args) 
    {
        Fraction f1 = new Fraction(1, 2, 5);
        Fraction f2 = new Fraction(1, 4, 5);
        Fraction f3 = new Fraction ();
        f3.add (f1, f2);
        f3.Display ();
    }
}

Compile the code and run it. 1 2/5 + 1 4/5 = 3 1/5.
See if you get that answer. Experiment with other numbers too and see if you get the desired results with your various methods.

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.