Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 21st, 2005, 9:32 PM   #1
wik865
Newbie
 
Join Date: Sep 2005
Posts: 3
Rep Power: 0 wik865 is on a distinguished road
Java fraction help

I have a program due that requires me to deal with adding, multiplying, dividing, subtracting, and simplifying fractions. I'm stuck on the simplifying part. This is a generic coding for the simplification part:

class Reduce
{
	int den, num, i = 2, cd = 0;
	Reduce(int n, int d)
	{
		num = n;
		den = d;
	}
	

	void reduction()
	{
		do
		{
			if ((den % i == 0) && (num % i == 0))
				cd = i;
			else i++;
		}
		while (cd == 0);

		System.out.println(cd);

		num /= cd;
		den /= cd;

		System.out.println(num + "/" + den);
	}
}

class Test
{
	static public void main(String[] a)
	{
		
		Reduce r1 = new Reduce(3,9);
		r1.reduction();
	}
}

It seems to work with fractions like 3/9 and 2/6, fractions that are able to be simplified. If I try with say 3/8, the output is 1/4. I was wondering if anyone out there can help me figure out why it is doing that. It's frustrating me a lot and I would like to finish this program soon.
wik865 is offline   Reply With Quote
Old Sep 21st, 2005, 9:45 PM   #2
2roll4life7
Programmer
 
2roll4life7's Avatar
 
Join Date: Aug 2005
Location: 0x0010 * 0x0091 + 0x0004
Posts: 65
Rep Power: 4 2roll4life7 is on a distinguished road
I'd say an easier way of doing this would be to just divide both numbers by their GCF. Finding the GCF will proabably be the hardest part.
__________________
#if 0 /* in case someone actually tries to compile this */
- libpng version 1.2.8 (example.c)

<Jim_McNeat> Is there like a way to put a compiler in "Just trust me on that one" mode?
2roll4life7 is offline   Reply With Quote
Old Sep 21st, 2005, 9:52 PM   #3
wik865
Newbie
 
Join Date: Sep 2005
Posts: 3
Rep Power: 0 wik865 is on a distinguished road
Thanks for the response, I'll see what I can do about that and post back.
wik865 is offline   Reply With Quote
Old Sep 21st, 2005, 10:29 PM   #4
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
GCF - Euclid'sche
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty
stevengs is offline   Reply With Quote
Old Sep 22nd, 2005, 10:16 AM   #5
wik865
Newbie
 
Join Date: Sep 2005
Posts: 3
Rep Power: 0 wik865 is on a distinguished road
Thanks a lot for that info. I figured it out. Thanks for all the help guys.
wik865 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:01 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC