Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Create a ComplexNo class for two imaginary numbers. Provide (in addition to add, subtract, multiply, and divide methods) a method for equals (which returns boolean),

Create a ComplexNo class for two imaginary numbers. Provide (in addition to add, subtract, multiply, and divide methods) a method for equals (which returns boolean), and toString( ) method (that returns a string representing the receiving object). Throw an exception if an attempt is made to divide by 0 (0 + 0i). Your implementation should return the results of each operation WITHOUT changing the operands.

Exxample:

Enter the first complex number's real part => 1.0

Enter the first complex number's imaginary part => -1.0

Enter the second complex number's real part => -1.0

Enter the second complex number's imaginary part => 1.0

complex number 1: 1.000000 - 1.000000i

complex number 2: -1.000000 + 1.000000i

the numbers are equal? false

add result: 0.000000

subtract result: 2.000000 - 2.000000i

multiply result: 2.000000i

divide result: -1.000000

Here is some of the code:

import java.util.Scanner; import java.math.*;

public class ComplexNo { public static void main(String[] args) { double real1, real2, img1, img2; Scanner scan = new Scanner(System.in); System.out.print("Enter the first complex number's real part =>"); real1 = scan.nextDouble(); System.out.print("Enter the first complex number's imaginary part =>"); img1 = scan.nextDouble(); System.out.print("Enter the seconds complex number's real part =>"); real2 = scan.nextDouble(); System.out.print("Enter the seconds complex number's imaginary part =>"); img2 = scan.nextDouble(); System.out.printf("%s %.6f %.6f%s", "complex number 1: ", real1, img1 ,"i "); System.out.printf("%s %.6f %.6f%s", "complex number 2: ", real2, img2 ,"i "); addition(real1, real2, img1, img2); subtraction(real1, real2, img1, img2); } public static double addition(double realOne, double realTwo, double imgOne, double imgTwo) { double real1 = realOne; double real2 = realTwo; double img1 = imgOne; double img2 = imgTwo; double sumReal = 0; double sumImg =0; sumReal = (real1 + real2); sumImg = (img1 + img2); return System.out.format("%s %.6f %.6f%s","add result: ", sumReal, sumImg, "i "); } public static double subtraction(double realOne, double realTwo, double imgOne, double imgTwo) { double real1 = realOne; double real2 = realTwo; double img1 = imgOne; double img2 = imgTwo; double sumReal = 0; double sumImg =0; sumReal = (real1 - real2); sumImg = (img1 - img2); return System.out.format("%s %.6f %.6f%s","subtract result: ", sumReal, sumImg, "i "); } }

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 Reliability Engineering Designing And Operating Resilient Database Systems

Authors: Laine Campbell, Charity Majors

1st Edition

978-1491925942

More Books

Students also viewed these Databases questions

Question

Compare and contrast verbal and nonverbal codes

Answered: 1 week ago

Question

Define and discuss the nature of ethnocentrism and racism

Answered: 1 week ago

Question

Define and discuss racial and ethnic stereotypes across cultures

Answered: 1 week ago