Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Help please: Design a class named Complex for representing complex numbers and the methods add, subtract, multiply, divide, abs for performing complex-number operations, and override

Help please:

Design a class named Complex for representing complex numbers and the methods add, subtract, multiply, divide, abs for performing complex-number operations, and override toString method for returning a string representation for a complex number. The toString method returns a + bi as a string. If b is 0, it simply returns a.

Provide three constructors Complex(a, b), Complex(a), and Complex(). Complex() creates a Complex object for number 0 and Complex(a) creates a Complex object with 0 for b. Also provide the getRealPart() and getImaginaryPart() methods for returning the real and imaginary part of the complex number, respectively. Your Complex class should also implement the Cloneable interface.

Write a test program that prompts the user to enter two complex numbers and display the result of their addition, subtraction, multiplication, and division.

Here is the main class. It cannot be changed.

import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter the first complex number: "); double a = input.nextDouble(); double b = input.nextDouble(); Complex c1 = new Complex(a, b); System.out.print("Enter the second complex number: "); double c = input.nextDouble(); double d = input.nextDouble(); Complex c2 = new Complex(c, d); System.out.println("(" + c1 + ")" + " + " + "(" + c2 + ")" + " = " + c1.add(c2)); System.out.println("(" + c1 + ")" + " - " + "(" + c2 + ")" + " = " + c1.subtract(c2)); System.out.println("(" + c1 + ")" + " * " + "(" + c2 + ")" + " = " + c1.multiply(c2)); System.out.println("(" + c1 + ")" + " / " + "(" + c2 + ")" + " = " + c1.divide(c2)); System.out.println("|" + c1 + "| = " + c1.abs()); Complex c3 = (Complex)c1.clone(); System.out.println(c1 == c3); System.out.println(c3.getRealPart()); System.out.println(c3.getImaginaryPart()); } }

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

Visual Basic 4 Ole Database And Controls Superbible

Authors: Michael Hatmaker, C. Woody Butler, Ibrahim Malluf, Bill Potter

1st Edition

1571690077, 978-1571690074

More Books

Students also viewed these Databases questions

Question

Estimate the weight of the air in your physics classroom?

Answered: 1 week ago