Results 1 to 8 of 8
 09032013, 08:44 PM #1Member
 Join Date
 May 2013
 Posts
 11
 Rep Power
 0
Rational  greatest common devisor help.
Hello, I am doing a rational class where I do arithmetic operations with fractions. Everything works correctly, but the problem I am having is simplifying the fraction(starts line 54 to 67). So if I input 1/2  1/2, it would display "0/4" & 1/2 + 1/2 = 2/2. So here is my code and test code:
Java Code:import java.util.Scanner; public class Rational { // TODO Autogenerated method stub //Data members private int numerator; private int denominator; //Constructors Rational() { numerator = 0; denominator = 1; } Rational(int num, int den) { numerator = num; denominator = den; } //Accessors public int getNumerator() { return numerator; } public int getDenominator() { return denominator; } // Modifiers public void setNumerator(int num) { numerator = num; } public void setDenominator(int den) { denominator = den; } public Rational inputRational(){ Scanner input = new Scanner(System.in); System.out.println("Enter numerator"); numerator = input.nextInt(); System.out.println("Enter Denominator"); denominator = input.nextInt(); return new Rational(numerator, denominator); } public String toString() { return numerator + "/" + denominator; } [B]private int gcd(int numerator, int denominator) { int r; while(denominator!= 0) { r = numerator % denominator; numerator = denominator; denominator = r; } return numerator; }[/B] //Adding public Rational add(Rational f) { int num; int den; num = (numerator * f.denominator) + (f.numerator * denominator); den = denominator * f.denominator; return new Rational(num, den); } public void sub(Rational f1, Rational f2) { numerator = (f1.numerator * f2.denominator)  (f2.numerator * f1.denominator); denominator = f1.denominator * f2.denominator; } public Rational mul(Rational f) { int num; int den; num = numerator * f.numerator; den = denominator * f.denominator; return new Rational(num, den); } public void div(Rational f1, Rational f2) { this.numerator = f1.numerator * f2.denominator; this.denominator = f1.denominator * f2.numerator; } public static double divided(Rational f1, Rational f2) { double value; value = (f1.numerator/f2.denominator)*(f2.denominator/f2.numerator); return value; } }
Java Code:public class TestRational { public static void main(String[] args) { // TODO Autogenerated method stub Rational f1 = new Rational(); Rational f2 = new Rational(); Rational f3 = new Rational(); f1.inputRational(); f2.inputRational(); f3 = f1.add(f2); System.out.println(f1.toString() + " + " + f2.toString() + " = " + f3.toString()); f3.sub(f1,f2); System.out.println(f1.toString() + "  " + f2.toString() + " = " + f3.toString()); f3 = f1.mul(f2); System.out.println(f1.toString() + " * " + f2.toString() + " = " + f3.toString()); f3.div(f1, f2); System.out.println(f1.toString() + " / " + f2.toString() + " = " + f3.toString()); System.out.println(Rational.divided(f1,f2)); f1.setNumerator(2); f2.setDenominator(5); System.out.println("Numerator: " +f1.getNumerator()); System.out.println("Denominator: "+f2.getDenominator()); } }
 09032013, 09:20 PM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,847
 Rep Power
 10
Re: Rational  greatest common devisor help.
You need to reverse the roles of denominator and numerator. To calculate GCD it is r = largest % smallest.
Then numerator = r and denominator = smallest.
For proper fractions, the denominator is the largest. For improper fractions, the numerator is larger. If you want to include both,
you need to swap their roles.
take 28/35 or 35/28
35 % 28 = 7
28 % 7 = 0
So 7 is the GCD and the fraction reduced to 4/5 or 5/4.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 09032013, 10:00 PM #3Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,847
 Rep Power
 10
Re: Rational  greatest common devisor help.
Please ignore my previous (and ill thought out) previous post. I would edit it but I can't. Grumble.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 09032013, 10:06 PM #4Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,847
 Rep Power
 10
Re: Rational  greatest common devisor help.
I am not certain what the problem is with your code. To simplify fractions, you need to divide the numerator and denominator by the gcd. Do you do that? Also, do you want to leave 0/4 as 0/4 or 0? Do you want to leave 99/99 as 99/99 or 1?
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 09032013, 10:19 PM #5Member
 Join Date
 May 2013
 Posts
 11
 Rep Power
 0
Re: Rational  greatest common devisor help.
Hey thanks for the replies. I want to make sure if the answer is 0/4, the output would be 0, and if 99/99, the output to be 1. Still working on the code.
 09032013, 10:23 PM #6Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 5,847
 Rep Power
 10
Re: Rational  greatest common devisor help.
Well, I presume a couple of if statements would do the trick.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 09052013, 06:18 AM #7Member
 Join Date
 May 2013
 Posts
 11
 Rep Power
 0
Re: Rational  greatest common devisor help.
Yes, I have worked on it and manage to simplify the division and subtraction well.
int divisor = gcd(numerator, denominator);
numerator = numerator/divisor;
denominator = denominator/divisor;
Added on each of the arithmetic.
but it does not seem to work with add/mult. Anywho, still working on this part, and once I finish and find whats wrong with add/mult simplifying, I should be all finished.
 09052013, 09:28 AM #8
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,395
 Blog Entries
 7
 Rep Power
 25
Re: Rational  greatest common devisor help.
If you follow a few simple rules, all will be easy:
1) make your Fractions immutable;
2) do the gcd( ... ) trick in the constructor;
3) for negative numbers make the numerator negative (the denominator will be positive);
4) don't print the part "/denominator" if the denominator == 1.
kind regards,
JosBuild a wall around Donald Trump; I'll pay for it.
Similar Threads

IBM Rational
By takugian in forum Forum LobbyReplies: 0Last Post: 10012012, 09:13 PM 
Rational Clearcase
By Rajavardhan in forum New To JavaReplies: 1Last Post: 07282012, 08:23 AM 
Greatest Common Factors (Version 2) Storing and Comparing Factors
By skaterboy987 in forum New To JavaReplies: 11Last Post: 11012011, 03:47 AM 
Rational Rose
By garrym in forum Forum LobbyReplies: 3Last Post: 05172011, 02:02 PM 
greatest prime factor
By java_prgr in forum New To JavaReplies: 2Last Post: 07232010, 08:28 PM
Bookmarks