Java Reference
In-Depth Information
arguments that can be used to set the instance variables of an object to any val-
ues. 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 should be static methods that each have two parame-
ters of type
Rational
and return a value of type
Rational
. For example,
Rational.add(r1, r2)
will return 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 nor-
malization, 4/-8 would be represented the same as -4/8. Also write a test pro-
gram to test your class.
Hints:
Two rational numbers
a/b
and
c/d
are equal if
a*d
equals
c*b.
Part Two: Add a second version of the methods for addition, subtraction, multi-
plication, and division. These methods should have the same names as the static
version but should use a calling object and a single argument. For example, this
version of the
add
method (for addition) has a calling object and one argument.
So
r1.add(r2)
returns the result of adding the rationals
r1
and
r2
. Note that
your class should have all these methods; for example, there should be two meth-
ods named
add
.
Alternate Part Two (If you want to do both Part Two and Alternate Part Two, they
must be two classes. You cannot include the methods from both Part Two and
Alternate Part Two in a single class. Do you know why?): Add a second version of
the methods for addition, subtraction, multiplication, and division. These
methods should have the same names as the static version but should use a calling
object and a single argument. The methods should be
void
methods. The result is
given as the changed value of the calling object. For example, this version of the
add
method (for addition) has a calling object and one argument. Therefore,
r1.add(r2);
changes the values of the instance variables of
r1
so they represent the result of
adding
r2
to the original version of
r1
. Note that your class should have all these
methods; for example, there should be two methods named
add
.
7.
Define a class for complex numbers. A complex number is a number of the form
a + b*i
where, for our purposes,
a
and
b
are numbers of type
double
, and
i
is a number
that represents the quantity
-
1
. Represent a complex number as two values of