Question: Write this program in java Your task is to create a class that will model a complex number. In much the way that our example

Write this program in java

Your task is to create a class that will model a complex number. In much the way that our example class in chapter 7 was a class that modeled a Fraction.

A complex number is a number of the form a + bi, where a is the real part and b is the imaginary part. It is based on the mathematical premise that i2 = -1. So like the fraction class, you will define two double data members for real and imaginary.

Your Complex class will contain the 13 methods described below. NOTE: All methods except for the constructors and the one labeled static are instance methods (called from an object) and immutable (do not change the calling object).

General Methods:

A constructor that takes no arguments and assigns both values to 0.

A constructor that takes one double and assigns it to the real portion, 0 to the other.

A constructor that takes two doubles, and assigns them to real, then imaginary.

2 accessors (get methods)

getReal( )

getImaginary( )

2 mutators (set methods)

setReal(double)

setImaginary(double)

toString( ) -outputting a String neatly in the form a + bi

equals( Complex ) - takes a complex number as a parameter type and returns a boolean of true if the calling object is equal to the parameter.

Mathematical Methods

2 different add methods

add( Complex )

add( double )

Each of these add methods will return a new Complex object, so that we can call the methods by:

Complex anObject = complexObject1.add(complexObject2); OR

Complex anObject = complexObject1.add(doubleValue);

These are mathematically equivalent to anObject = complexObject1 + complexObject2 and anObject = complexObject1 + doubleValue.

The rules for adding two Complex objects or a Complex object and a real value(double) is on the following pages.

Static method -that can be run without an object.

add(Complex, Complex)

This method can be called only by using the class name:

Complex newObject = Complex.add(ComplexObject1,ComplexObject2);

But the mathematical result is the same as the first add method.

Use of a Static Constant and one more method

Add a third data member at the top of the class, this one will be a private static constant called LIMIT. Set LIMIT to equal 10.0 for all objects. This data member will represent a set limit for how large my complex number should be. The size of a complex number is represented by the distance from the origin of a 2D real/imaginary number line, in other words the formula: size =( Write this program in java Your task is to create a classreal^2 + imaginary^2) ^that will model a complex number. In much the way that our. Note: Your complex object can be larger than this limit, we are creating this constant simply to be able to determine if it is large or not. For this you need to create the following method.

isBig( )

This method is an instance method that returns a Boolean based on if the object is larger than the limit variable.

Testing

I have made it easy for you to test your created class. I have created a Driver file called ComplexTester.java that is in your lab folder. You MUST set up your template class as directed. (Use exact method names.) Your class will need to be able to run with this driver with no errors. Remember to make a copy on your computer in the same location that you are creating your class and attempt to get it to run. This file tests all of the methods that you have just created. It also tests them in order, so that as you finish some you can test only that portion, simply comment the rest of the code out. Also your output should look like the text on the last page of this lab.

class ComplexTester { public static void main(String args[] ) { //create complex numbers testing all three constructors Complex one = new Complex(); Complex two = new Complex(5); Complex three = new Complex(2.1,3.5); // test toString System.out.println("First Complex number : " + one.toString()); System.out.println("Second Complex number : " + two.toString()); System.out.println("Third Complex number : " + three); //test accessors System.out.println("Real for third number : "+ three.getReal()); System.out.println("Imaginary for third number : " + three.getImaginary()); //test mutators System.out.println("Using mutators, change first number to 1.7 + 3.4i"); one.setReal(1.7); one.setImaginary(3.4); System.out.println("First Complex number : " + one); System.out.println(); //test two instance add methods Complex four = one.add(three); System.out.println(one + " plus " + three + " = " + four); Complex five = two.add(3); System.out.println("Adding 3.0 to Second complex number = " + five); System.out.println(); //test equals method System.out.println("Third Complex number : " + three); Complex six = new Complex(2.1,3.5); System.out.println("Sixth Complex number : " + six); System.out.println("Third equals sixth : " + three.equals(six)); System.out.println("Third equals first : " + three.equals(one)); System.out.println(); //test static add method System.out.println("Fourth Complex number : " + four); System.out.println("Fifth Complex number : " + five); Complex seven = Complex.add(four,five);

System.out.println("Fourth plus Fifth = " + seven); //testing toBig method System.out.println(); System.out.println("Seventh Complex number : " + seven); System.out.println("Is Fourth to Big : " + four.isBig()); System.out.println("Is Seventh to Big : " + seven.isBig()); } }

Complex Math:

A + Bi refers to A = real portion; B = imaginary portion

Adding: resulting complex number

A + Bi plus a real number C => (A + C) + (B)i

A + Bi plus C + Di => (A + C) + (B + D)i

Equals:

Means real portions of each must be the same and imaginary portions must be the same as each other.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!