Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Rational - GCD issue

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Rational - GCD issue

    Hello, I am having trouble trying to build the "gcd(Greatest common divisor". It's where if I input "1/2 - 3/4" output would be "-2/8", where I would want "-1/4". Here is my code. I finished all my arithmetic's, and now all I need to do is simplify my answers. After that, I should be good with this code. Any help is appreciated.

    import java.util.Scanner;
     
     
    public class Rational {
     
     
      // TODO Auto-generated 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;
     }
     private static int gcd(int m, int n)
        {
            int r;
            while(n!= 0)
            {
                r = m % n;
                m = n;
                n = r;     
                }
            return m;
            }
     
     //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;
      }
     }

    Here's my tester.
    public class TestRational {
     
     public static void main(String[] args) {
      // TODO Auto-generated 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());
     }
     
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Rational - GCD issue

    This is a time when posting an example run would be helpful to explain what you're asking for help with. So when I run your code for the fractions you suggest, I get:
    Enter numerator
    1
    Enter Denominator
    2
    Enter numerator
    3
    Enter Denominator
    4
    1/2 + 3/4 = 10/8
    1/2 - 3/4 = -2/8
    1/2 * 3/4 = 3/8
    1/2 / 3/4 = 4/6
    0.0
    Numerator: 2
    Denominator: 5
    Are you asking for help to simplify all of the resulting fractions, 10/8, -2/8, 3/8, and 4/6?

    What are the results 0.0, Numerator: 2 and Denominator: 5 supposed to be? Are those right?

    I notice that you never use the method gcd(). Why is that?

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Rational - GCD issue

    Quote Originally Posted by GregBrannon View Post
    This is a time when posting an example run would be helpful to explain what you're asking for help with. So when I run your code for the fractions you suggest, I get:
    Enter numerator
    1
    Enter Denominator
    2
    Enter numerator
    3
    Enter Denominator
    4
    1/2 + 3/4 = 10/8
    1/2 - 3/4 = -2/8
    1/2 * 3/4 = 3/8
    1/2 / 3/4 = 4/6
    0.0
    Numerator: 2
    Denominator: 5
    Are you asking for help to simplify all of the resulting fractions, 10/8, -2/8, 3/8, and 4/6?

    What are the results 0.0, Numerator: 2 and Denominator: 5 supposed to be? Are those right?

    I notice that you never use the method gcd(). Why is that?
    Thanks for the reply. Yes, I was missing the method gcd. Here is what i've done.

    int divisor = gcd(numerator, denominator);
    numerator = numerator/divisor;
    denominator = denominator/divisor;

    ^this should be posted right after the add/sub/mul/div parts. It works for Sub/Div.

    if (numerator == 0){
    return "0";
    }
    else if (numerator == denominator){
    return "1";

    ^I put this in the ToString method. So, yeah, just figuring out whats wrong with simplifying the addition & Multiplication, and i'm finish with this work.

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Rational - GCD issue

    Hello.
    You are still fine.
    Suppose your output is n/m. You can find the common factors for n and m one by one. If a factor say 'f' is found then reduce both n and m by f. With this you can avoid GCD.

    Syed.

Similar Threads

  1. need help with lehmer's gcd algorithm code
    By mirjamkolar in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 17th, 2013, 04:05 AM
  2. Rational program calling up from different file but in same package
    By my21 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 11th, 2013, 12:00 PM
  3. Euclid Calculator GCD and LCM using BigInteger
    By Exiled in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 18th, 2012, 11:00 PM
  4. The Rational Class Program [HOMEWORK HELP]
    By FCarlton24 in forum Object Oriented Programming
    Replies: 1
    Last Post: October 18th, 2011, 12:31 PM
  5. [Homework] Rational Class
    By burger king in forum Object Oriented Programming
    Replies: 2
    Last Post: January 13th, 2011, 09:15 PM