Rational Java Program

Page 1 of 1

7 Replies - 11597 Views - Last Post: 13 May 2009 - 04:23 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=104808&amp;s=a525676c67768cc65ed5ccf1a126430f&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 bottlepop

Reputation: 0
• Posts: 5
• Joined: 12-May 09

Rational Java Program

Posted 12 May 2009 - 02:38 PM

well this was my assignment
Write a class that represents rational numbers. The class name is Rational. A rational number can be represented as the ratio of two integer values, a and b, where b is not zero. The class has attributes for the numerator and denominator of this ratio. The ratio should always be stored in its simplest form. That is, any common factor of a and b should be removed. For example, the rational number 40/12 should be stored as 10/3.
Create the following attributes:
• numerator
• denominator
The class has the following constructors and methods:
• A default constructor that sets the rational number to 0/1.
• A constructor that has parameters for the numerator and denominator, and converts the resulting ratio to simplified form.
• simplify - a private method that converts the rational number to simplified form.
• getGCD(x, y) - a private static method that returns the largest common factor of the two positive integers x and y, that is, their greatest common divisor. For example, the greatest common divisor of 40 and 12 is 4.
• value - public method that returns the rational number as a double value.
• toString - public method that returns the rational number as a string in the form a/b.
The main program should ask for rational number, from the user, as two numbers (numerator, denominator). Note that methods 'simplify' and 'getGCD(x,y)' are private methods. The second constructor will call the private method simplify and the method 'simplify' will call the method 'getGCD(x,y)' . The only way to run the program (get it to simplify the rational number) is to create an object, and pass in the values of numerator and denominator. The second constructor will have to check for any Illegal entries and request new numbers, from the user, if required. There are many ways to find a GCD of two numbers. Do some searching! Call the relevant methods to display the final result. Your main program must have instructions to display the final result using both the methods.

i've read some of the topics and i found t his program but it doesn't seem to reduce! can anyone help?

here is the main.java
``` /*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication4;
import java.util.*;
/**
*
* @author Owner
*/
public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int correctNum,correctDenom;

Fraction yourFraction = new Fraction();
yourFraction.writeOutput();

Scanner keys = new Scanner(System.in);
correctNum = keys.nextInt();
yourFraction.setNum(correctNum);

correctDenom = keys.nextInt();
while (correctDenom == 0)
{
System.out.println("Error: Cannot divide by zero.");
correctDenom = keys.nextInt();
}
yourFraction.setDenom(correctDenom);

yourFraction.writeOutput();
}

// TODO code application logic here

}

```

and this is rational.java
```/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication4;

/**
*
* @author Owner
*/
public class Fraction {
public   int num;
public  int denom;
public  int GCD;

public Fraction()
{
num = 0;
denom = 1;
}
public void setNum(int newNum)
{
simplfy(newNum, denom);
}
public void setDenom(int newDenom)
{
simplfy(num, newDenom);
}
public void simplfy(int newNum, int newDenom)
{
while(GCD >= 1)
{
num /= GCD;
denom /= GCD;
GCD = getGCD();
}
num = newNum;
denom = newDenom;
}
private int getGCD()
{
GCD = Math.min(Math.abs(num), Math.abs(denom));
System.out.println(GCD);
while((num % GCD !=0) ||(denom % GCD != 0))
{
GCD--;
System.out.println(GCD);
}
return GCD;
}
public void writeOutput()
{
System.out.println(Integer.toString(num) + "/"
+ Integer.toString(denom));
}

public void simplify() {
int largest = denom;
if(num < denom)
largest = num;

GCD = 0;
for(int i = largest; i > 0; i--) {
if(num % i == 0 && denom % i == 0) {
GCD = i;
break;
}
}

// if GDC != 0 we found one
if(GCD != 0) {
num /= GCD;	  // reduce
denom /= GCD;
simplify();		   // and call simplify() again
}
}

}
```

This post has been edited by bottlepop: 12 May 2009 - 02:52 PM

Is This A Good Question/Topic? 0

Replies To: Rational Java Program

#2 AbuJaFaR

• D.I.C Regular

Reputation: 13
• Posts: 330
• Joined: 13-December 07

Re: Rational Java Program

Posted 12 May 2009 - 02:48 PM

Can you edit your post and place the *code* tags between your code??
It'll be a lot easier to understand it!

#3 Fuzzyness

• Comp Sci Student

Reputation: 669
• Posts: 2,438
• Joined: 06-March 09

Re: Rational Java Program

Posted 12 May 2009 - 02:49 PM

Please post any errors you are getting, or expected input/output

#4 bottlepop

Reputation: 0
• Posts: 5
• Joined: 12-May 09

Re: Rational Java Program

Posted 12 May 2009 - 02:53 PM

well it asks for a num and denom, and if i put 8 for num and 10 for denom... it comes out 8/10. how do i simplify it?

#5 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Rational Java Program

Posted 12 May 2009 - 07:16 PM

bottlepop, on 12 May, 2009 - 01:38 PM, said:

well this was my assignment
..

i've read some of the topics and i found t his program but it doesn't seem to reduce! can anyone help?

So you assignment is to cut & paste cude from the net ? Not to actually write the program ?
Wonder if I can do a PhD thesis like that

#6 myork

Reputation: 41
• Posts: 129
• Joined: 04-March 09

Re: Rational Java Program

Posted 12 May 2009 - 07:32 PM

If you're getGCD() works, then you don't need to re-calculate the GCD in the simplify method. That said, your getGCD is supposed to be private static, and is supposed to take 2 numbers. Essentially, once you fix that, you'll end up being able to use
```int gcd = Fraction.getGCD(num, denom);
simpNum = num/gcd;
simpDenom = num/gcd;
```

#7 bottlepop

Reputation: 0
• Posts: 5
• Joined: 12-May 09

Re: Rational Java Program

Posted 13 May 2009 - 01:46 PM

Quote

Wonder if I can do a PhD thesis like that

hey if it works why not?

#8 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

Re: Rational Java Program

Posted 13 May 2009 - 04:23 PM

bottlepop, on 13 May, 2009 - 12:46 PM, said:

Quote

Wonder if I can do a PhD thesis like that

hey if it works why not?

If in a health related topic wouldn't want to be your patient