Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a class called Complex for performing arithmetic with complex numbers. Complex numbers consist of a realPart and imaginaryPart Requirements: The Complex class must

 

Create a class called Complex for performing arithmetic with complex numbers. Complex numbers consist of a realPart and imaginaryPart Requirements: The Complex class must have 2 float fields: one for the realPart and one for the imaginaryPart. Create a Complex constructor that takes no arguments and initializes the fields to 0. Create a Complex constructor that takes a float for the realPart and a float for the imaginaryPart. Create a Complex method called print() that prints out the two fields (see example output below). Create a Complex method: public Complex add(Complex c1, Complex c2) The add method should add the two given complex numbers. The real parts are added together and the imaginary parts are added together. The add method should create a new instance of complex. The realPart should the realPart from c1 added to the realPart from c2. The imaginaryPart should the imaginaryPart from c1 added to the imaginaryPart from c2. The new instance should be returned. Create a Complex method: public Complex subtract(Complex c1, Complex c2) Similar to add, except the realParts and imaginaryParts are subtracted. Write a main that tests your classes. See the example below. Sample Output: a = (9.5, 7.7) b = (1.2, 3.1) a+b= (10.7, 10.8) a-b = (8.3, 4.6) Problem 3: Create a class Rectangle. The class has attributes length and width, each of which defaults to 1. Provide methods that calculate the perimeter and the area of the rectangle. Provide set and get methods for both length and width. The set methods should verify that length and width are each floating-point numbers greater than or equal to 0.0 and less than 20.0. Write a program to test class Rectangle.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Java How To Program Late Objects Version

Authors: Paul Deitel, Deitel & Associates

8th Edition

0136123716, 9780136123712

More Books

Students also viewed these Programming questions

Question

What is DFX? What all is included in DFX?

Answered: 1 week ago

Question

Draw a picture consisting parts of monocot leaf

Answered: 1 week ago

Question

What extraneous variables might in flu ence the response?

Answered: 1 week ago