Results 1 to 11 of 11
Thread: Reducing fractions
 01092010, 02:08 AM #1
 01092010, 02:16 AM #2
May we see those several attempts?
 01092010, 09:53 AM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,679
 Blog Entries
 7
 Rep Power
 21
 01102010, 07:28 AM #4Member
 Join Date
 Dec 2009
 Posts
 68
 Rep Power
 0
can you show us some of your attempts or rather codes?
and ill share you my program this will be very useful for your problem.. and you can also study returning objects as well
 01122010, 04:42 AM #5
so this is what i wrote so far
Java Code:public double Reduce(double n1,double d1) { int gcf=1; int gcf2=1; n1=num1; d1=den1; do { double result=gcf%num1; if(result!=0) gcf++; } while(result!=0); return num1; do { double result2=gcf2%den1; if(result2!=0); gcf2++; } while(result2!=0); return den1; }
Java Code:result cannot be resolved result2 cannot be resolved
Are you suggesting that Cocunuts migrate?!! Monty Python
 01122010, 06:16 AM #6Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,565
 Rep Power
 12
Could you post that again properly formatted?
I think that it won't compile as written. You have code following the first return statement, and that's not valid Java.
(Also it would probably make more sense if n1 and d1 were ints.)
 01132010, 02:34 AM #7
so i almost fixed it only one error but i have a feeling its not an easy fix
Java Code:public double Reduce(double n1,double d1) { int gcf=1; int gcf2=1; n1=num1; d1=den1; double result=gcf%num1; double result2=gcf2%den1; do { gcf++; } while(result!=0); do { gcf2++; } while(result2!=0); return num1; return den1; }
Java Code:Unreachable code://After first return
Are you suggesting that Cocunuts migrate?!! Monty Python
 01132010, 02:39 AM #8Senior Member
 Join Date
 Nov 2009
 Posts
 235
 Rep Power
 5
You cant return two objects from one method. You have to either make num1 and den1 public so you just call the method, or have two methods.
 01132010, 11:46 AM #9
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,679
 Blog Entries
 7
 Rep Power
 21
This thread (and the OP's code) is so cumbersome: change the type of the numerator and denominator to ints to make sense and use the folllowing gcd implementation for the reduction of both of them; reduce them in the constructor of the class:
Java Code:private int gcd(int a, int b) { for (int n= 0; b != 0; n= a%b, a= b, b= n); return a; }
kind regards,
JosLast edited by JosAH; 01132010 at 02:52 PM.
 01132010, 05:35 PM #10Member
 Join Date
 Dec 2009
 Posts
 68
 Rep Power
 0
i hope this will help you
Java Code:public class Fraction { /** numerator for this fraction */ private int numerator; /** denominator for this fraction */ private int denominator; /** * @param num numerator of this fraction * * @param denom denominator of this fraction */ public Fraction(int num, int denom) { this.setNumerator(num); this.setDenominator(denom); if (num == 0  denom == 0) { System.err.println("Mathematical Expression Error."); System.err.println("Unable To Perform Fration Arithmetic."); System.exit(2); } } /** * Creates a fraction number. * * @param number the numerator */ public Fraction(int number) { this(number, 1); } /** * Sets the numerator of this fraction * * @param numerator the numerator of this fraction */ public void setNumerator(int numerator) { this.numerator = numerator; } /** * Sets the denominator of this fraction * * @param denominator the denominator of this fraction */ public void setDenominator(int denominator) { this.denominator = denominator; } /** * @return returns the numerator of this fraction */ public int getNumerator() { return numerator; } /** * @return returns the denominator of this fraction */ public int getDenominator() { return denominator; } /** * * @param f1 the first fraction * @param f2 the second fraction * * @return returns true if the numerator and denominator of both fraction * equals */ public boolean equals(Fraction f1, Fraction f2) { if (f1.getNumerator() == f2.getNumerator() && f1.getDenominator() == f2.getDenominator()) { return true; } else { return false; } } /** * @return returns the simplified value of this fraction */ public Fraction simplify() { int num, denom, gcd; num = this.getNumerator(); denom = this.getDenominator(); gcd = gcd(this); Fraction simplified = new Fraction(num/gcd, denom/gcd); return simplified; } /** * Determines the greatest common divisor of this fraction using euclidean * algorithm. * * @param frac this fraction * * @return returns the greates common divisor of this fraction */ public static int gcd(Fraction frac) { int gcd = 0, remainder, num, denom; num = frac.getNumerator(); denom = frac.getDenominator(); remainder = num % denom; while (remainder != 0) { num = denom; denom = remainder; remainder = num % denom; gcd = denom; } return gcd; } /** * * @param num numerator of this fraction * * @param denom denominator of this fraction * * @return returns the smallest value between the two numbers in this * fraction */ public static int minNumber(int num, int denom) { int smaller; if (num <= denom) { smaller = num; return smaller; } else { smaller = denom; return smaller; } } /** * Converts a fraction into its decimal value. * * @return returns the decimal equivalent of this fraction */ public double toDecimal() { double decimalValue; decimalValue = (double) this.getNumerator() / (double) this.getDenominator(); return decimalValue; } /** * Compares two fraction and determines which of the two is smaller. * * @param f1 first fraction to compare * @param f2 second fraction to compare * * @return returns the smaller fraction between the two */ public static Fraction minFraction(Fraction f1, Fraction f2) { double frac1, frac2; frac1 = f1.toDecimal(); frac2 = f2.toDecimal(); if (frac1 <= frac2) { return f1; } else { return f2; } } /** * Adds a two fraction. * * @param frac fraction to be added to another fraction * * @return returns the sum of fraction */ public Fraction add(Fraction frac) { int a, b, c, d; a = this.getNumerator(); b = this.getDenominator(); c = frac.getNumerator(); d = frac.getDenominator(); Fraction sum = new Fraction(a + c, b + d); return sum; } /** * Adds a fraction into a simple integer value. * * @param number integer value to be added into this fraction * * @return returns the sum of fraction */ public Fraction add(int number) { Fraction sum, newFrac; newFrac = new Fraction(number, 1); sum = this.add(newFrac); return sum; } /** * Subtract this fraction into a given fraction argument. * * @param frac the fraction to subtract from this fraction * * @return returns the difference of two fraction */ public Fraction subtract(Fraction frac) { int a = this.getNumerator(), b = this.getDenominator(), c = frac.getNumerator(), d = frac.getDenominator(); Fraction difference = new Fraction((a * d)  (b * c), b * d); return difference; } /** * Subtract this fraction into a given number argument. * * @param number the integer value to subtract. * * @return returns the difference of this and number */ public Fraction subtract(int number) { Fraction newFrac, difference; newFrac = new Fraction(number, 1); difference = this.subtract(newFrac); return difference; } /** * Multiplies this fraction into a given fraction argument. * * @param frac the multiplier of the multiplication * * @return returns the product of two fraction */ public Fraction multiply(Fraction frac) { int a = this.getNumerator(), b = this.getDenominator(), c = frac.getNumerator(), d = frac.getDenominator(); Fraction product = new Fraction(a * c, b * d); return product; } /** * Multiplies this fraction into a given number argument * * @param number the mulitplier * * @return returns the product of this fration */ public Fraction multiply(int number) { Fraction newFrac, product; newFrac = new Fraction(number, 1); product = this.multiply(newFrac); return product; } /** * Divides this fraction into a given fraction argument. * * @param frac the divisor of the division * * @return returns quotient of this fraction divided by frac. */ public Fraction divide(Fraction frac) { int a = this.getNumerator(), b = this.getDenominator(), c = frac.getNumerator(), d = frac.getDenominator(); Fraction quotient = new Fraction(a * d, b * c); return quotient; } /** * Divides this fraction into a given integer argument. * * @param number the divisor * * @return returns the quotient of this fraction divided by number */ public Fraction divide(int number) { Fraction newFrac, quotient; newFrac = new Fraction(number, 1); quotient = this.divide(newFrac); return quotient; } /** * @return returns the String representation of this object */ @Override public String toString() { return this.getNumerator() + "/" + this.getDenominator(); } public static void main(String[] args) { Fraction sum, diff, product, quotient, smaller; Fraction fraction1 = new Fraction(24, 54); Fraction fraction2 = new Fraction(5, 10); sum = fraction1.add(fraction2).add(fraction1); diff = fraction1.subtract(fraction2); product = fraction1.multiply(fraction2).add(fraction1); quotient = fraction1.divide(fraction2).multiply(fraction2); smaller = Fraction.minFraction(fraction1, fraction2); System.out.println("The Sum Is: " + sum); System.out.println("The Difference Is: " + diff); System.out.println("The Product Is: " + product); System.out.println("The Quotient Is: " + quotient); System.out.print("\n"); System.out.format("Fraction1 Is Equivalent Into: %.3f \n", fraction1.toDecimal()); System.out.format("Fraction2 Is Equivalent Into: %.3f \n", fraction2.toDecimal()); System.out.print("\n"); System.out.println("The Smaller Between Two Fraction Is: " + smaller); System.out.print("\n"); System.out.println("The Greatest Commnon Divisor Of Fraction1 Is: " + Fraction.gcd(fraction1)); System.out.println("The Greatest Common Divisor Of Fraction2 Is: " + Fraction.gcd(fraction2)); } }
but i guess its not yet complete regarding with fraction arithmetic
 01132010, 05:51 PM #11
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,679
 Blog Entries
 7
 Rep Power
 21
Similar Threads

NumberFormat which leave fractions as they are
By itaipee in forum AWT / SwingReplies: 2Last Post: 12092009, 07:11 PM 
How do I reduce Fractions in this program?
By Popedreadlock in forum New To JavaReplies: 8Last Post: 12082008, 01:28 AM
Bookmarks