# Reducing fractions

• 01-09-2010, 02:08 AM
SwEeTAcTioN
Reducing fractions
Can any one show me an example of a method that reduces a fraction. I have made several desperate attempts that have all failed.:mad:

All help is appreciated:)
• 01-09-2010, 02:16 AM
Lil_Aziz1
May we see those several attempts?
• 01-09-2010, 09:53 AM
JosAH
Quote:

Originally Posted by SwEeTAcTioN
Can any one show me an example of a method that reduces a fraction. I have made several desperate attempts that have all failed.:mad:

All help is appreciated:)

If you have a greatest common divisor method available you're 90% in business. Hint: Google has loads of those methods available.

kind regards,

Jos
• 01-10-2010, 07:28 AM
bigj
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
• 01-12-2010, 04:42 AM
SwEeTAcTioN
so this is what i wrote so far

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;   }```
num1 and den1 are declared in this program as well but this is just the reducing part im pretty stuck and it gives me two errors

Code:

```result cannot be resolved result2 cannot be resolved```
Any help would be greatly appreciated
• 01-12-2010, 06:16 AM
pbrockway2
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.)
• 01-13-2010, 02:34 AM
SwEeTAcTioN
so i almost fixed it only one error but i have a feeling its not an easy fix

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; }```
and my error is
Code:

`Unreachable code://After first return`
• 01-13-2010, 02:39 AM
collin389
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.
• 01-13-2010, 11:46 AM
JosAH
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:

Code:

```        private int gcd(int a, int b) {                                 for (int n= 0; b != 0; n= a%b, a= b, b= n);                 return a;         }```
... and then post your changes/new iimplementation.

kind regards,

Jos
• 01-13-2010, 05:35 PM
bigj

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));     } }```
i dont have time to write another code to make it more understandable, i just shared the complete program that i have recently studied....
but i guess its not yet complete regarding with fraction arithmetic
• 01-13-2010, 05:51 PM
JosAH
Quote:

Originally Posted by bigj