Question: A rational number is a number that can be represented by a pair of integers a numerator and a denominator. Write a class named Rational

A rational number is a number that can be represented by a pair of integers

a

numerator and a denominator.

Write a class

named

Rational

that represents

rational numbers. The class should support the following

state/

behavior:

?

A pair of integers

,

num

and

den

,

representin

g the numerator and denominator

?

A constructor that accepts a

pair of integer and uses them to initialize the

instance

variables

.

An exception should be throw if the denominator is 0

.

?

A

second

constructor that accepts a single integer representing a whole number

(i.e., with a denominator of 1).

For full credit, you

must

invoke the two

-

parameter

constructor

(i.e., use

this

)

?

A

toString

method that returns the string representation

num

/

den

(e.g., 3/5)

, if

the denominator is not equal to 1, and

num

(e.g., 7) otherwise.

?

A

Rational

-

returning

method named

multiply

that

accepts a

Rational

argument

and

returns a

new

Rational

consisting of the product of

the receiver

and parameter (both

Rational

s

)

.

(The product of 2 rational numbers is a rational

whose numerator is the product of the two numerators, and whose denominator is

the product of the tw

o

denominators).

?

A

Rational

-

returning

method named

inverse

that

accepts no arguments

returns a

new

Rational

representing the inverse (reciprocal) of its (

Rational

)

parameter

(i.e., the numerator becomes the new denominator and vice versa)

.

?

A

boolean

-

r

e

turning

method named

equals

that

that accepts a

Rational

argument and

returns whether

the receiver and parameter (both

Rational

s

) are

equal

.

(For the purpose of this question, assume two ration

a

l numbers are equal if

their numerator

s

and denominators are equal, i.e., a

ssume 2/

4 is

not

equal to 1/

2).

?

A

main

method that declares and creates

two

Rational

s, using the t

wo

constructors, and illustr

ates the use of the

multiply

,

inverse

, and

equals

methods (one call to each is sufficient).

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!