Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Java fraction help (http://www.programmingforums.org/showthread.php?t=6014)

wik865 Sep 21st, 2005 9:32 PM

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.

2roll4life7 Sep 21st, 2005 9:45 PM

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.

wik865 Sep 21st, 2005 9:52 PM

Thanks for the response, I'll see what I can do about that and post back.

stevengs Sep 21st, 2005 10:29 PM

GCF - Euclid'sche ;)

wik865 Sep 22nd, 2005 10:16 AM

Thanks a lot for that info. I figured it out. Thanks for all the help guys.


All times are GMT -5. The time now is 2:27 AM.

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