Results 1 to 11 of 11
  1. #1
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default 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:)
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  2. #2
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    May we see those several attempts?

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by SwEeTAcTioN View Post
    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
    Last edited by JosAH; 01-09-2010 at 01:20 PM.

  4. #4
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    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

  5. #5
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    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;
      }
    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

    Java Code:
    result cannot be resolved
    result2 cannot be resolved
    Any help would be greatly appreciated
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    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.)

  7. #7
    SwEeTAcTioN's Avatar
    SwEeTAcTioN is offline Member
    Join Date
    Oct 2009
    Posts
    69
    Rep Power
    0

    Default

    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;
    }
    and my error is
    Java Code:
    Unreachable code://After first return
    Are you suggesting that Cocunuts migrate?!! -Monty Python

  8. #8
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    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.

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    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;
    	}
    ... and then post your changes/new iimplementation.

    kind regards,

    Jos
    Last edited by JosAH; 01-13-2010 at 02:52 PM.

  10. #10
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default

    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));
        }
    }
    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

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,733
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by bigj View Post
    i hope this will help you
    I don't think so because there are quite some logic errors in your code.

    kind regards,

    Jos

    ps. spoonfeeding is not helping the OP.

Similar Threads

  1. NumberFormat which leave fractions as they are
    By itaipee in forum AWT / Swing
    Replies: 2
    Last Post: 12-09-2009, 07:11 PM
  2. How do I reduce Fractions in this program?
    By Popedreadlock in forum New To Java
    Replies: 8
    Last Post: 12-08-2008, 01:28 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •