Java Reference
In-Depth Information
and define the constructor so that the object will be initialized to realPart + 0* i .
Also include a no-argument constructor that initializes an object to 0 (that is, to
0 + 0* i ). Define accessor and mutator methods as well as the methods equals
and toString . Define static methods for addition, subtraction, and multiplication
of objects of your class Complex . These methods should be static and should each
have two parameters of type Complex and return a value of type Complex . For
example, Complex.add(c1, c2) will return the result of adding the two complex
numbers (two objects of the class Complex ) c1 and c2 . Also write a test program
to test your class.
Hints : To add or subtract two complex numbers, you add or subtract the two
instance variables of type double . The product of two complex numbers is given
by the following formula:
(a + b *i)*(c + d *i) = (a *c − b *d) + (a *d + b *c)*i
Part Two: Add a second version of the methods for addition, subtraction, and mul-
tiplication. 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 c1.add(c2)
returns the result of adding the complex numbers c1 and c2 . Note that your class
should have all these methods; for example, there should be two methods named add .
Alternate Part Two: Add a second version of the methods for addition, subtrac-
tion, and multiplication. These methods should have the same names as the static
version but should use a calling object and a single argument. The methods will 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,
c1.add(c2);
changes the values of the instance variables of c1 so they represent the result of
adding c2 to the original version of c1 . Note that your class should have all these
methods; for example, there should be two methods named add .
(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?)
8. Programming Project 4.12 asked you to create a PizzaOrder class that stores an
order consisting of up to three pizzas. Extend this class with the following methods
and constructor:
public int getNumPizzas() —returns the number of pizzas in the order.
public Pizza getPizza1() —returns the first pizza in the order or null if
pizza1 is not set.
public Pizza getPizza2() —returns the second pizza in the order or null
if pizza2 is not set.
public Pizza getPizza3() —returns the third pizza in the order or null if
pizza3 is not set.
Search WWH ::




Custom Search