# Fractions Code help

Printable View

• November 8th, 2010, 08:34 PM
SOK
Fractions Code help
Hey All I have three classes. FractionGenerator, Fraction and MyProgram which is the main.

I have FractionGenerator working, it is generating random numbers for numerator and denominator. My code has one error with the return type in my public boolean equals. I am not sure what to return. Also I am not sure if my add, subtract, multiply and divide methods are correct.

Code Java:

```  //Program Summary: Fraction class represents a ratio of two integers.   public class Fraction { private int fNumerator; private int fDenominator;     public Fraction(int numerator, int denominator) { this.fNumerator = fNumerator; this.fDenominator = fDenominator; } // public Fraction() { this.fNumerator = 0; this.fDenominator = 1; } public Fraction(int numerator) { this.fNumerator = numerator; fDenominator = 1; }     // Mutator for numerator public void setNumerator(int numer) { this.fNumerator = numer; }   // Mutator for denominator public void setDenominator(int denom) { this.fDenominator = denom; }   public void writeFraction() { System.out.println(fNumerator + "/" + fDenominator); }   public void reduceFraction() { int reduce;   if(this.fNumerator > this.fDenominator) { reduce = this.fDenominator; } else { reduce = this.fNumerator; }   while (reduce > 1) { if(this.fNumerator / reduce == 0 && this.fDenominator / reduce == 0) { this.fNumerator = this.fNumerator / reduce;   } reduce --;   } } public boolean equals(Fraction other) { if (this.fNumerator == other.fNumerator && this.fDenominator == other.fDenominator) { System.out.println("Equal"); }   else   System.out.println("Not Equal");   return a;   }   public void add(Fraction other) { this.fNumerator = this.fNumerator * this.fDenominator + this.fDenominator + fNumerator; this.fDenominator = this.fNumerator * this.fDenominator; }   public void subtract() { this.fNumerator = this.fNumerator * fDenominator - this.fDenominator - fNumerator; this.fDenominator = this.fNumerator * this.fDenominator; }   public void multiply() { this.fNumerator = this.fNumerator * fDenominator * this.fDenominator * fNumerator; this.fDenominator = this.fNumerator * this.fDenominator; }   public void divide() { this.fNumerator = this.fNumerator * fDenominator / this.fDenominator / fNumerator; this.fDenominator = this.fNumerator * this.fDenominator; }   }```
• November 8th, 2010, 09:02 PM
DavidFongs
Re: Fractions Code help
You need to return true if they are equal and false otherwise
• November 8th, 2010, 09:11 PM
SOK
Re: Fractions Code help
Thanks David,

That takes care of that. Since this is my first program actually using more than one class, I am not sure how to call from the fraction class into my main program called MyProgram. It appears to be printing 0/0 for some reason.

Code Java:

```  public class MyProgram { public static void main(String[] args) { FractionGenerator a = new FractionGenerator(); FractionGenerator b = new FractionGenerator(); Fraction f1 = new Fraction(a.getNumerator(),a.getDenominator()); Fraction f2 = new Fraction(b.getNumerator(),b.getDenominator());     System.out.println("**************"); f1.writeFraction(); f2.writeFraction(); f1.equals(f2); System.out.println("**************");   System.out.println("Multiply: "); f1.multiply(f2);   System.out.println(" Add: "); f1.add(f2);   System.out.println(" Divide: "); f1.divide(f2);   System.out.println("Subtract: "); f1.subtract(f2);   System.out.println("**************");           }   }```
• November 9th, 2010, 10:07 AM
DavidFongs
Re: Fractions Code help
I have to assume you mean it is printing 0/0 when you are calling f1.writeFraction() and f2.writeFraction()...... The only way this could happen is if you are constructing the fraction with zeros.

Put a print statement before these lines:

Fraction f1 = new Fraction(a.getNumerator(),a.getDenominator());
Fraction f2 = new Fraction(b.getNumerator(),b.getDenominator());

That prints out the values of a & b: getNumerator() & getDenominator()

The error is probably in your FractionGenerator class, but you haven't posted that code, so no help there yet.