Hi everyone!
I have written two classes, Fraction and useFraction. Fraction holds all of the methods required to process the data from useFraction, which consists of private ints that are turned into separate fractions. The source for the classes looks like this:
useFraction looks like this:
Fraction looks like this:
My problem is that I need to write a .equals method in the Fraction class that will compare two fractions and return a boolean. It is the last line that has been commented out, that's where I'm stuck. I'm stuck on the syntax, and most examples I can find are just serving to confuse me more. I really hope someone here can help out someone new to programming! This has been bugging me for hours. Thanks!
I have written two classes, Fraction and useFraction. Fraction holds all of the methods required to process the data from useFraction, which consists of private ints that are turned into separate fractions. The source for the classes looks like this:
useFraction looks like this:
Code:
public class useFraction { public static void main(String[] args) { Fraction first=new Fraction(); // assigns 0/1 Fraction second=new Fraction(4); // assigns 4/1 Fraction third = new Fraction(6,8); // assigns 6/8 in lowest form, so assigns 3/4 Fraction last=new Fraction(9,12); // assigns 9/12 in lowest form, so assigns 3/4 System.out.println(first); //ADDED System.out.println(second); //ADDED System.out.println(third); // outputs on screen: 3/4 System.out.println(last); //ADDED if (third.equals(last)) System.out.println("They are equal"); else System.out.println("They are not equal"); if (second.equals(third)) System.out.println("They are equal"); else System.out.println("They are not equal"); if (third.value() >= 0.70) System.out.println("third is greater than 0.70 "); } }
Fraction looks like this:
Code:
public class Fraction { private int n=0; private int d=0; public Fraction() { n=0; d=1; } public Fraction(int exn) { n=exn; d=1; } public Fraction(int exn, int exd) { int GCD; //System.out.println("It is "+findGCD(exn,exd)+"."); n=exn/(findGCD(exn,exd)); d=exd/(findGCD(exn,exd)); } public static int findGCD(int a, int b) { return ( b != 0 ? findGCD(b, a % b) : a ); } public String toString () { return (n+"/"+d); } public double value() { return (n/d); } //public boolean equals(Fraction otherFraction); }
Comment