Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please code in java,thanks . . . Exercise 2 A complex number has two parts, a real part a and an imaginary part b, and

Please code in java,thanks

image text in transcribedimage text in transcribedimage text in transcribed

. . . Exercise 2 A complex number has two parts, a real part a and an imaginary part b, and is written as a + bi. For example, 1.0+ 3.0i is a complex number with the real part being 1.0 and the imaginary part being 3.0. In this exercise, you will write a class to model complex numbers and manipulate them. Design a class called Complex with the following properties: Two instance variables real and imag - both double. A constructor that creates an instance of a Complex object with the given real and imag values. A default constructor that creates an instance of a Complex object 0.0 +0.0i. Get and set methods A toString method that returns the String (a + bi) where a and b are the real and imag parts, respectively. A method is Real that returns true if the complex number is real (that is, the imag value is 0.0). A method isImaginary that returns true if the complex number is imaginary (that is, the real value is 0.0). A method equals(Complex other) that returns true if this instance of the Complex number is equal to the given Complex number (other). Note: Two complex numbers are equal if their real parts are equal and their imaginary parts are equal. A method magnitude that returns the magnitude of this complex number. Note: magnitude(a+bi) = Math.sqrt(a*a + b*b) A method complex_add(Complex other) that adds this instance of the Complex number to the given instance (other) and returns the result as a Complex number. Note: (a+bi)+(c+di) = (a+c) + (b+d)i A method complex_sub(Complex other) that subtracts the given instance of the Complex number (other) from this instance and returns the result as a Complex number. Note: (a+bi)-(c+di)=(a-c)+(b-d)i A method complex_multiply(Complex other) that multiplies this instance of the Complex number with the given instance (other) and returns the result as a Complex number Note: (a+bi)*(c+di)= (ac-bd)+ad+bc)i . . Write a client program called Demo4 with a main method to test all the methods in the Complex class. A sample dialog and output is given below. Enter complex number 1: 3.0 5.6 Enter complex number 2: 0.0 4.5 Number 1 is (3.0 + 5.6i) (3.0 + 5.6i) is NOT a pure real number (3.0 + 5.6i) is NOT a pure imaginary number Number 2 is (0.0 + 4.5i) (0.0 + 4.5i) is NOT a pure real number (0.0 + 4.5i) is a pure imaginary number The magnitude of (3.0+5.6i) is .... The magnitude of (0.0+4.5i) is (3.0+5.6i) + (0.0+4.5i) is .. (3.0+5.6i) - (0.0+4.5i) is (3.0+5.6i)*(0.0+4.5i) is Important Note about comparing Doubles: In both the exercises, you will be comparing doubles. If you use the == to compare two doubles, it may produce unexpected outcome. For example, (1.1+2.2)==3.3 returns false. It is common to define a small threshold value EPSILON (usually set to 10^-8 for comparing doubles. So you would write your equals method to return true if the result is + or - EPSILON. Marking rubric In total, you need to test eight methods as listed below. 0.25 mark is deducted for failure to test a method. otoStringo o is Realo) o is Imaginaryo o equals(Complex other) maginitude complex_add(Complex other) complex_sub(Complex other) o complex_multiply(Complex other) o O

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

Database Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions