Problem

Given the following class Complex, complete the following questions:class Complex {private...

Given the following class Complex, complete the following questions:

class Complex {

private int real;

private int imaginary;

public Complex(int r, int i) {

real = r;

imaginary = i;

} // end constructor

public String toString() {

return real + “ + ” + imaginary + “i”;

} // end toString

} //end class Complex

a. Write a statement that creates a complex number 3 + 2i called cl.


b. Write a statement that creates a complex number 4 - 5 i called c2.


c. Write a statement that prints a complex number called cl.


d. For the class Complex, modify the toStringmethod so that if the real or imaginary part is zero, it is not placed in the string. If both are zero, then just print zero. Finally, if the imaginary part is 1 or -1, simply print + i instead of + li and - i instead of - li.


e. For the class complex, add the following methods: public Complex add(Complex val)

// returns a Complex number whose value is (this + val) public Complex subtract(Complex val)

// returns a Complex number whose value is (this - val) public Complex multiply(Complex val)

// returns a Complex number whose value is (this * val)


f. Add a mainprogram with test code that demonstrates that the above methods are working properly.


Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 1
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT