473,408 Members | 1,676 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,408 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
  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 11091
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

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

Similar topics

2
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...
3
by: Fei Li | last post by:
Hi, take string class as an example, who can explain the difference? Thanks
4
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...
12
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...
18
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...
12
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...
1
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...
5
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...
10
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...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.