participate


New To Java - Help with fraction class
This question is answered. Correct Answer available

<<   Back to Forum  |   Give us Feedback
This topic has 9 replies on 1 page.
RandellK
Posts:17
Registered: 3/12/09
Help with fraction class   
Nov 3, 2009 5:08 PM
 
 
I am making a fraction class and am having problems with the equals method that compares two fractions.
I am having trouble implementing my helper method gcd() within the methods getReducedFraction and the equals method.


Here is what i have so far: (comments included)

public class Fraction
{
	private int numerator;
	private int denominator;
	
	public void setNumerator(int newNumerator)
	{
		this.numerator = newNumerator;
	}
	
	public boolean isDenominator(int newDenominator)
	{	
		boolean success = true;
		if(newDenominator == 0)
			success = false;
		else
		{
			this.denominator = newDenominator;
			success = true;
		} 
		return success;
	}
	
	public int getNumerator()
	{
		return numerator;
	}
	
	public int getDenominator()
	{
		return denominator;
	}
	
	public double getDoubleFraction()
	{
		double fraction;
		fraction = numerator/denominator;
		return fraction;
	}
	
	public Fraction reducedFraction()
	{
		Fraction reducedFraction = new Fraction();
		reducedFraction = gcd(); //error expects Fraction type!
		return reducedFraction;
	} 
	
	public boolean equals(Fraction otherFraction)
	{
		this.gcd(); //need to get reduced fractions to compare them
		return (this.numerator == otherFraction.numerator 
				   && this.denominator == otherFraction.denominator);
	}
	
	public String toString()
	{
		return numerator + "/" + denominator;
	}
	
	private int gcd()
	{
		int gcd;
		if(numerator < denominator)
			gcd = numerator;
		else
			gcd = denominator;
		while(!(numerator % gcd == 0 && denominator % gcd == 0)
				&& gcd > 1)
			gcd--;
		return gcd;
	}
}


Edited by: RandellK on Nov 4, 2009 1:08 AM
 
jverd
Posts:51,846
Registered: 3/30/99
Re: Help with fraction class   
Nov 3, 2009 5:23 PM (reply 1 of 9)  (In reply to original post )
 
 
Okay, and what specific problems are you having?
 
flounder
Posts:14,643
Registered: 3/2/05
Re: Help with fraction class   
Nov 3, 2009 5:38 PM (reply 2 of 9)  (In reply to original post )
 
 
this.gcd(); //need to get reduced fractions to compare them

So why are you calling the gcd method? Also, why do you throw away the returned value?
 
RandellK
Posts:17
Registered: 3/12/09
Re: Help with fraction class   
Nov 3, 2009 5:40 PM (reply 3 of 9)  (In reply to #1 )
 
 
In my reduced fraction method-- getting the passed in fraction to use the helper method to get it to reduced terms and
in my equals method-- I need to get the 2 fractions into lowest terms and and then compare them to see if they are equal.
I am having problems figuring out this logic...
 
flounder
Posts:14,643
Registered: 3/2/05
Re: Help with fraction class   
Nov 3, 2009 5:43 PM (reply 4 of 9)  (In reply to #3 )
 
 
Can you write down on a piece of paper in plain English the step-by-step instructions of how to reduce a fraction?

Once you have done that it should be simple to translate it into code.
 
RandellK
Posts:17
Registered: 3/12/09
Re: Help with fraction class   
Nov 3, 2009 5:54 PM (reply 5 of 9)  (In reply to #4 )
 
 
This is what my gcd helper method does, but how do i take the value returned by the gcd method and use it, such as compare two reduced fractions.
 
jverd
Posts:51,846
Registered: 3/30/99
Re: Help with fraction class   
Nov 3, 2009 6:15 PM (reply 6 of 9)  (In reply to #5 )
 
 
RandellK wrote:
This is what my gcd helper method does,

You're missing the point. That's what you think that method does. But how can you be sure?

but how do i take the value returned by the gcd method and use it, such as compare two reduced fractions.

Write down in English, in very basic, simple, precise steps, how you use gcd to compare two reduced fractions "manually", without Java.
 
flounder
Posts:14,643
Registered: 3/2/05
Re: Help with fraction class   
Nov 3, 2009 6:34 PM (reply 7 of 9)  (In reply to #5 )
Correct
 
RandellK wrote:
This is what my gcd helper method does,

No. The gcd method finds the greatest common divisor. The reduceFraction method should reduce the fraction but it doesn't work either.
 
tjacobs01
Posts:10,234
Registered: 10/11/01
Re: Help with fraction class   
Nov 3, 2009 6:51 PM (reply 8 of 9)  (In reply to #7 )
 
 
...psst...you didn't hear it here...BigInteger...

AFTER you get it to work, the biggest problem with what you are doing is that there is a bounding problem, and your class won't have any definable bounds. This is because as the denominator gets bigger, the maximum number you can represent is going to shrink.
 
keleb
Posts:1
Registered: 11/3/09
Re: Help with fraction class   
Nov 3, 2009 9:14 PM (reply 9 of 9)  (In reply to original post )
 
 
Your first problem lies in your reducedFraction method. This error is because you try to give a Fraction object an int value since your gcd method returns an int. If I understand you right what you want to do in this method, all you need to do is to divide the numerator and the denominator with their gcd which you get from your method gcd. So the code for reducedFraction would look like this:
public Fraction reducedFraction(){
	int gcd = gcd();
	int reducedNumerator = numerator / gcd;
	int reducedDenominator = denominator / gcd;
	Fraction reducedFraction = new Fraction();
	reducedFraction.setNumerator(reducedNumerator);
	reducedFraction.isDominator(reducedNumerator);
	return reducedFraction;
}

That with using your code. I would make changes in your code and add a constructor that takes a numenator and a denominator so you can skip the last two lines in the above code.

Your second problem is your equals method. To compare the two Fractions you need to compare their fully reduced form. But since you now have the method to do so (see above) that shouldn't be too hard. Simply reduce both Fractions and compare them.

public boolean equals(Fraction otherFraction){
	Fraction f1 = this.reducedFraction();
	Fraction f2 = otherFraction.reducedFraction();
	
	boolean b = ((f1.getNumerator() == f2.getNumerator) && (f1.getDenominator() == f2.getDenominator()));
	
	return b;
}


You can of course simplify the methods by writing more on the same line. I did it this way to make it a little easier to follow.

There are a few things more you can do with your code to optimize it but it should work ith the above changes.
 
This topic has 9 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums