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); System.out.println("Please enter the numerator."); correctNum = keys.nextInt(); yourFraction.setNum(correctNum); System.out.println("Please enter the denominator."); correctDenom = keys.nextInt(); while (correctDenom == 0) { System.out.println("Error: Cannot divide by zero."); System.out.println("Please enter another number"); 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