469,282 Members | 2,078 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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:
Expand|Select|Wrap|Line Numbers
  1.  
  2. public class useFraction 
  3. {
  4.      public static void main(String[] args)  
  5.     {        
  6.         Fraction first=new Fraction();            //   assigns 0/1         
  7.         Fraction second=new Fraction(4);        //  assigns  4/1       
  8.         Fraction third = new Fraction(6,8);        // assigns 6/8 in lowest form, so assigns 3/4    
  9.         Fraction last=new Fraction(9,12);        //  assigns 9/12 in lowest form, so assigns 3/4         
  10.         System.out.println(first);                  //ADDED                
  11.         System.out.println(second);                  //ADDED   
  12.         System.out.println(third);                    // outputs on screen:     3/4          
  13.         System.out.println(last);                   //ADDED       
  14.         if (third.equals(last))           
  15.                  System.out.println("They are equal");     
  16.         else             
  17.                  System.out.println("They are not equal");       
  18.         if (second.equals(third))       
  19.                System.out.println("They are equal");      
  20.         else            
  21.                System.out.println("They are not equal");         
  22.         if (third.value() >= 0.70)           
  23.                System.out.println("third is greater than 0.70 ");     
  24.       }
  25.  }


Fraction looks like this:

Expand|Select|Wrap|Line Numbers
  1. public class Fraction 
  2. {     
  3.      private int n=0;     
  4.      private int d=0;          
  5.      public Fraction()     
  6.      {        
  7.          n=0;         
  8.          d=1;     
  9.      }     
  10.      public Fraction(int exn)     
  11.      {         
  12.          n=exn;         
  13.          d=1;      
  14.      }     
  15.       public Fraction(int exn, int exd)     
  16.      {        
  17.           int GCD;             
  18.           //System.out.println("It is "+findGCD(exn,exd)+".");   
  19.           n=exn/(findGCD(exn,exd));          
  20.           d=exd/(findGCD(exn,exd));    
  21.      }                   
  22.       public static int findGCD(int a, int b)    
  23.      {                
  24.            return ( b != 0 ? findGCD(b, a % b) : a );      
  25.      }          
  26.      public String toString ()     
  27.      {        
  28.          return (n+"/"+d);    
  29.      }    
  30.      public double value()    
  31.      {        
  32.           return (n/d);   
  33.      }        
  34.  
  35.  //public boolean equals(Fraction otherFraction);
  36.  
  37. }  
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!
Oct 29 '08 #1
3 10144
Laharl
849 Expert 512MB
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.)
Oct 29 '08 #2
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. public boolean equals(Fraction otherFraction)   
  3. {            
  4.       return (n.equals(otherFraction.n)) && (d.equals(otherFraction.d));    
  5. }
I end up with a compiling error of "int cannot be dereferenced".

:[
Oct 29 '08 #3
I figured it out! Needed to do this instead. Don't know what I was thinking. Thanks!

Expand|Select|Wrap|Line Numbers
  1.  
  2. public boolean equals(Fraction otherFraction)    
  3. {
  4.       return ((n == otherFraction.n) && (d == otherFraction.d));    
  5. }
Oct 29 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Chris | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.