I am trying to use Euclid's Algorithm in a fraction class. I am not finished with the class yet so there are parts that are incomplete. I am unsure of how I would use the method. Would I put in the setter methods? or Public fraction (int n, int d)?
public class Fraction{ private int numerator; private int denominator; int gcd; public Fraction(){ } public Fraction(int n, int d){ setNumerator(n); setDenominator(d); } public static int gcd(int p, int q) { if (q == 0) return p; else return gcd(q, p % q); } public String toString(){ return String.format("%d/%d", getNumerator(), getDenominator()); } public boolean equals(Object otherObject){ if(otherObject == null){ return false; }else{ if(!getClass().equals(otherObject.getClass())){ return false; }else{ Fraction otherFraction = (Fraction) otherObject; return getNumerator()== otherFraction.getNumerator() && getDenominator()== otherFraction.getDenominator(); } } } public int getNumerator(){ return numerator; } public void setNumerator(int newNumerator){ numerator = newNumerator/gcd; } public int getDenominator(){ return denominator ; } public void setDenominator(int newDenominator){ if(newDenominator == 0 ){ throw new IllegalStateException(String.format("Illegal denominator: %d", newDenominator)); } else{ denominator = newDenominator; } } }