7 Replies - 7230 Views - Last Post: 13 May 2009 - 04:23 PM Rate Topic: -----

#1 bottlepop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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);
		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


Is This A Good Question/Topic? 0
  • +

Replies To: Rational Java Program

#2 AbuJaFaR  Icon User is offline

  • D.I.C Regular

Reputation: 13
  • View blog
  • 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! :)
Was This Post Helpful? 0
  • +
  • -

#3 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

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

Re: Rational Java Program

Posted 12 May 2009 - 02:49 PM

:code:

Please post any errors you are getting, or expected input/output
Was This Post Helpful? 0
  • +
  • -

#4 bottlepop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Rational Java Program

Posted 12 May 2009 - 07:16 PM

View Postbottlepop, 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 :)
Was This Post Helpful? 0
  • +
  • -

#6 myork  Icon User is offline

  • D.I.C Head

Reputation: 41
  • View blog
  • 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;

Was This Post Helpful? 0
  • +
  • -

#7 bottlepop  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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? :P
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8347
  • View blog
  • Posts: 31,913
  • Joined: 06-March 08

Re: Rational Java Program

Posted 13 May 2009 - 04:23 PM

View Postbottlepop, 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? :P

If in a health related topic wouldn't want to be your patient :P
Was This Post Helpful? 1

Page 1 of 1