# Rational Numbers

Page 1 of 1

## 5 Replies - 2294 Views - Last Post: 13 May 2010 - 05:35 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=90821&amp;s=cda8944715fa87ed473eea246063db00&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 izthrower

Reputation: 13
• Posts: 233
• Joined: 11-February 09

# Rational Numbers

Posted 05 March 2009 - 11:43 AM

Define a class for rational numbers. A rational number is a number that can be represented as the quotient of two integers. For example, 1/2, 3/4, 64/2, and so forth are all rational numbers. (By 1/2 etc. we mean the everyday meaning of the fraction, not the integer division this expression would produce in a Java program.) Represent rational numbers as two values of type int, one for the numerator and one for the denominator So your class will have two instance variables of type int. Call the class Rational. Include a constructor with two arguments that can be used to set the instance variables of an object to any values. Also, include a constructor that has only a single parameter of type int; call this single parameter wholeNumber and define the constructor so that the object will be initialized to the rational number wholeNumber/1. Also, include a no-argument constructor that initializes an object to 0 (that is, to 0/1). Note that the numerator, the denominator, or both may contain a minus sign. Define methods for addition, subtraction, multiplication, and division of objects of your class Rational. These methods will be static methods that each have two parameters of type Rational and returns a value of type Rational. For example, Rational.add(r1, r2) will returns the result of adding the two rational numbers (two objects of the class Rational) r1 and r2. Define accessor and mutator methods as well as the methods equals and toString. You should include a method to normalize the sign of the rational number so that the denominator is positive and the numerator is either positive or negative. For example, after normalization 4/-8 would be represented the same as -4/8. Also write a test program to test your class.
Hints: Two rational numbers a/b and c/d are equal if a*d equals c*b.

You should define the following methods:

public Rational(int numerator, int denominator)
public Rational(int wholeNumber)
public Rational()
public void normalize()
public void setNumerator(int numerator)
public int getNumerator()
public void setDenominator(int denominator)
public int getDenominator()
public boolean equals(Rational obj)
public static Rational add(Rational r1, Rational r2)
public static Rational subtract(Rational r1, Rational r2)
public static Rational multiply(Rational r1, Rational r2)
public static Rational divide(Rational r1, Rational r2)

To aid in testing, you may also wich to implement a method that will return the value of the Rational as a double:
public double getDoubleValue()

/**
Class representing rational numbers
*/
class Rational {
/**
The number's numerator
*/
private int numerator;

/**
The number's denominator
*/
private int denominator;

/**
Returns a String representing the number
*/
public String toString() {
return numerator + "/" + denominator;
}

}

/**
Test class that exercises the methods of Rational
*/
public class RationalDemo {

public static void main(String[] args) {
// Test constructor, setters, toString, and normalize
Rational oneHalf = new Rational();
oneHalf.setNumerator(1);
oneHalf.setDenominator(2);
System.out.println("One half: " + oneHalf);

Rational twoQuarters = new Rational();
twoQuarters.setNumerator(-2);
twoQuarters.setDenominator(-4);
System.out.println("Before normalization: " + twoQuarters);
twoQuarters.normalize();
System.out.println("After normalization: " + twoQuarters);

Rational negativeThree = new Rational(-3);
System.out.println("Negative three: " + negativeThree);
Rational twoThirds = new Rational(2, 3);
System.out.println("Two thirds: " + twoThirds);

// Test the getter methods
System.out.println(
"Numerator of " + twoThirds + ": " + twoThirds.getNumerator());
System.out.println(
"Denominator of " + twoThirds + ": " + twoThirds.getDenominator());
System.out.println(
"Value of " + twoThirds + ": " + twoThirds.getDoubleValue());

// Test equality
System.out.println(
oneHalf + " = " + twoThirds + " ? " + oneHalf.equals(twoThirds));
System.out.println(
oneHalf +" = " + twoQuarters + " ? " + oneHalf.equals(twoQuarters));

// Test the arithmetic operations
System.out.println(oneHalf + " + " + twoThirds + " = " +
System.out.println(oneHalf + " - " + twoThirds + " = " +
Rational.subtract(oneHalf, twoThirds));
System.out.println(oneHalf + " * " + twoThirds + " = " +
Rational.multiply(oneHalf, twoThirds));
System.out.println(oneHalf + " / " + twoThirds + " = " +
Rational.divide(oneHalf, twoThirds));
}

}

Any Help Plz
Thanks Sooo much

Is This A Good Question/Topic? 0

## Replies To: Rational Numbers

### #2 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: Rational Numbers

Posted 05 March 2009 - 11:50 AM

so what do you need help with?, can you elaborate more please

### #3 BigAnt

• May Your Swords Stay Sharp

Reputation: 102
• Posts: 2,392
• Joined: 16-August 08

## Re: Rational Numbers

Posted 05 March 2009 - 11:51 AM

He just wants the assingment done for him, he has 2 other threads with two other assignments already

### #4 mostyfriedman

• The Algorithmi

Reputation: 727
• Posts: 4,473
• Joined: 24-October 08

## Re: Rational Numbers

Posted 05 March 2009 - 12:01 PM

BigAnt, on 5 Mar, 2009 - 10:51 AM, said:

He just wants the assingment done for him, he has 2 other threads with two other assignments already

yea i saw his other threads

Reputation:

## Re: Rational Numbers

Posted 12 May 2010 - 10:34 PM

halu...hahaha so this is where our instructor gets his activity in the laboratory huh..he told us that he made a program out of every activity......shame on himm..pweeeehhhh

### #6 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11236
• Posts: 42,274
• Joined: 27-December 08

## Re: Rational Numbers

Posted 13 May 2010 - 05:35 AM