Question: Need help with changing this cose Assignment 9 (Chapter 7) 1) Create a Complex class. We will implement 5 functions: add, subtract, multiply, divide and
Need help with changing this cose
Assignment 9 (Chapter 7) 1) Create a Complex class. We will implement 5 functions: add, subtract, multiply, divide and a test for equality. We will go over the math in class. The class will handle double values for the real and imaginary parts of the complex numbers. Sample Session (1.0 - 0.5i) + 2.0i = (1.0 + 1.5i) (1.0 - 0.5i) - 2.0i = (1.0 - 2.5i) (1.0 - 0.5i) * 2.0i= (1.0 + 2.0i) (1.0 - 0.5i) / 2.0i = (-0.25 - 0.5i) (1.0 - 0.5i) != 2.0i Press any key to continue.. import java.util.Scanner; public class Complex { double real; double imaginary; public Complex(double real, double imaginary) { this.real = real; this.imaginary = imaginary; } public String toString(){ if(this.real == 0){ return this.imaginary + "i"; } else if (this.imaginary == 0){ return this.real + "" ; } else if(this.imaginary < 0){ return "(" + this.real + " - " + Math.abs(imaginary) + "i" + ")"; } return "(" + this.real + " + " + Math.abs(imaginary) + "i" + ")"; } public static void main(String[] args) { Scanner reader = new Scanner(System.in); double real; double imaginary; System.out.print("Enter first real part: "); real = Double.parseDouble(reader.nextLine()); System.out.print("Enter first imaginary part: "); imaginary = Double.parseDouble(reader.nextLine()); Complex num1 = new Complex(real,imaginary); System.out.print("Enter second real part: "); real = Double.parseDouble(reader.nextLine()); System.out.print("Enter second imaginary part: "); imaginary = Double.parseDouble(reader.nextLine()); Complex num2 = new Complex(real,imaginary); System.out.println(" Performing arithmetic operations .... "); System.out.println(num1 + " + " + num2 + " = " + add(num1,num2)); System.out.println(num1 + " - " + num2 + " = " + subtract(num1,num2)); System.out.println(num1 + " * " + num2 + " = " + multiply(num1,num2)); System.out.println(num1 + " / " + num2 + " = " + divide(num1,num2)); if(testForEquality(num1,num2)){ System.out.println(num1 + " = " + num2); } else { System.out.println(num1 + " != " + num2); } } public static Complex add(Complex num1 , Complex num2){ Complex temp = new Complex(0.0,0.0); temp.real = num1.real + num2.real ; temp.imaginary = num1.imaginary + num2.imaginary; return temp; } public static Complex subtract(Complex num1, Complex num2){ Complex temp = new Complex(0.0,0.0); temp.real = num1.real - num2.real; temp.imaginary = num1.imaginary - num2.imaginary ; return temp; } public static Complex multiply(Complex num1, Complex num2){ Complex temp = new Complex(0.0,0.0); // (a + bi) * ( c + di) = ( ac - bd ) + (ad + bc )i temp.real = (num1.real * num2.real ) - (num1.imaginary * num2.imaginary ); temp.imaginary = (num1.real * num2.imaginary) + (num2.real * num1.imaginary ); return temp; } public static Complex divide(Complex num1, Complex num2){ double temp = (num2.real * num2.real) + (num2.imaginary * num2.imaginary); if (temp == 0) return new Complex(Double.NaN, Double.NaN); return new Complex(((num1.real * num2.real) + (num1.imaginary * num2.imaginary)) / temp, ((num1.imaginary * num2.real) - (num1.real * num2.imaginary)) / temp); } public static boolean testForEquality(Complex num1, Complex num2){ if(num1.real == num2.real && num1.imaginary == num2.imaginary){ return true; } return false; } }
Reconfigure the class from assignment 9 to use the BigDecimal class. which is the given Code above ^.
Sample Session: Please enter the first real part>1
Please enter the first imaginary part> -0.5(1- 0.5i)
Please enter the second real part> 9
Please enter the second imaginary part>9
(9 + 9i) (1-0.5i) + (9+ 9i) = (10+8.5i)
(1-0.5i) -(9+9i) = (-8 - 9.5i)
(1-0.5i) * (9+9i)= (13.5 + 4.5i)
(1 - 0.5i) / (9+9i) = (0.0277777777777777778-0.08333333333333333i
(1-0.5i) != (9+9i)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
