Writing a .equals method to compare fractions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zlwilly
    New Member
    • Oct 2008
    • 3

    Writing a .equals method to compare fractions

    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:
    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);
    
    }
    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!
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    It looks like you're storing the fractions in lowest terms. This is good. Then, two fractions F1 and F2 are equal if and only if the numerators of F1 and F2 are equal and the denominators of F1 and F2 are equal. Do you know how to implement this in code?

    (For a quick hint, the signature of the equals() method is public boolean equals(Object). You will need to cast the Object to an object of your class.)

    Comment

    • zlwilly
      New Member
      • Oct 2008
      • 3

      #3
      Right, I used Euclid's algorithm to bring them down (by finding the greatest common denominator.) I understand the concept, but it was the code that was giving me trouble. The examples I had been looking at were comparing dates consisting of month, day, and year, which meant the boolean was checking that all were the same using &&. Is this the way I should check both denominator and numerator? It's the syntax. That's what always gets me.

      I replaced the commented code with this:

      Code:
              
      public boolean equals(Fraction otherFraction)   
      {            
            return (n.equals(otherFraction.n)) && (d.equals(otherFraction.d));    
      }
      I end up with a compiling error of "int cannot be dereferenced".

      :[

      Comment

      • zlwilly
        New Member
        • Oct 2008
        • 3

        #4
        I figured it out! Needed to do this instead. Don't know what I was thinking. Thanks!

        Code:
         
        public boolean equals(Fraction otherFraction)    
        {
              return ((n == otherFraction.n) && (d == otherFraction.d));    
        }

        Comment

        Working...