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