473,390 Members | 1,339 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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
  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.      }        
  35.  //public boolean equals(Fraction otherFraction);
  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 11089
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
  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
  2. public boolean equals(Fraction otherFraction)    
  3. {
  4.       return ((n == otherFraction.n) && (d == otherFraction.d));    
  5. }
Oct 29 '08 #4

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

Similar topics

by: emma middlebrook | last post by:
Hi Having difficulty getting myself clear on how a type's operator== fits in with Object.Equals. Let's just consider reference types. The default operator== tests for object identity...
by: Fei Li | last post by:
Hi, take string class as an example, who can explain the difference? Thanks
by: Chris | last post by:
This is something that has been bugging me for some time. When exactly should I be using == instead of .Equals() ? I see a lot of code that performs object evaluation e.g. Is Object1 the same as...
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
by: Omiris | last post by:
I'm trying to use the Equals() method that is defined on the List(Of T) class. The docs seem to state that if T implements the IEquatable interface, then it will use the Equals method that is...
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.