Results 1 to 8 of 8
  1. #1
    kay19 is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default 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 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;
    	}
    	[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 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());
     }
    
    }
    Therefore, any help on that issue is greatly appreciated.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default 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,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default 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,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default 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,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    kay19 is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

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

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: Rational - greatest common devisor help.

    Well, I presume a couple of if statements would do the trick.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    kay19 is offline Member
    Join Date
    May 2013
    Posts
    11
    Rep Power
    0

    Default Re: Rational - greatest common devisor help.

    Quote Originally Posted by jim829 View Post
    Well, I presume a couple of if statements would do the trick.

    Regards,
    Jim
    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.

  8. #8
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,435
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. IBM Rational
    By takugian in forum Forum Lobby
    Replies: 0
    Last Post: 10-01-2012, 09:13 PM
  2. Rational Clearcase
    By Rajavardhan in forum New To Java
    Replies: 1
    Last Post: 07-28-2012, 08:23 AM
  3. Replies: 11
    Last Post: 11-01-2011, 02:47 AM
  4. Rational Rose
    By garrym in forum Forum Lobby
    Replies: 3
    Last Post: 05-17-2011, 02:02 PM
  5. greatest prime factor
    By java_prgr in forum New To Java
    Replies: 2
    Last Post: 07-23-2010, 08:28 PM

Posting Permissions

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