Java Reference
In-Depth Information
6.12 Exercises
1. Write a class that is called
Complex
and that manipulates complex numbers.
The class should have the variables
realPart
and
imaginaryPart
,whichshould
be both of type
double
.
The class should support the
add
method. For example, if
c1
and
c2
are objects
of type
Complex
,then
c1.add(c2)
will return a
Complex
object that is the sum
of the two objects. The constructed object will have a real part that is the sum
of the real parts of the two objects, and an imaginary part that is the sum of the
imaginary parts of the two objects.
Similarly, write a
subtract
method. The constructed object will have a real part
that is the difference of the real parts of the two objects, and an imaginary part
that is the difference of the imaginary parts of the two objects.
Write static versions of the
add
and
subtract
methods. For example,
Complex.add(c1,c2)
will return the result of adding two complex numbers. Note
that now the hidden parameter is gone. Similarly,
Complex.subtract(c1,c2)
will return the difference of the two numbers.
Write an empty constructor and a constructor that takes in the real and imagi-
nary part of the number.
Write a
toString
method that returns the complex number as a
String
.For
example, it will return
3+2i
when the real part is 3 and the imaginary part is 2.
Add an
equals
method that compares two objects of type
Complex
for equality.
Create a second class with a
main
method and use it to test the methods of the
Complex
class.
2. Write a class that is called
Number
. An object of type
Number
represents an integer
with up to 100 digits. Recall that the type
int
only supports integers up to roughly
2
31
. To remove this restriction, the
Number
class will contain the following variable
declaration:
short[] digits = new short[100]
. In other words, the digits of the
number will be saved in an array. Create an additional Boolean variable to store the
sign of the number (i.e., positive or negative). Add static and non-static methods to
add and subtract numbers and the
toString
and
equals
methods as explained in
Exercise 1. The adding and subtracting should be performed similar to the way you
add and subtract numbers on paper. Write a
main
method that tests the class.
3. Write an
Employee
class. Every employee has
name
and
age
. Include empty and
non-empty constructors. Include meaningful
equals
and
toString
methods. Add
a
private static
variable that keeps track of the number of employees. Create a
public static
method that returns the number of created employees. Include a
main
method that tests all the features of your class.
4. Write the
FuelGauge
and
Odometer
classes. The
FuelGauge
class keeps track of the
amount of fuel in the car in gallons. It also contains methods for putting fuel in the
car and burning fuel. The
Odometer
class keeps track of the current mileage of the
car. It has a method for incrementing the car's mileage by one. Every object of type
Odometer
should contain an object of type
FuelGauge
. Every time the mileage on
the
Odometer
object goes up, the amount of fuel should go down by the appropriate