Question

Create a ComplexNo class for two imaginary numbers. Provide (in addition to add, subtract, multiply, and...

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\n");
System.out.printf("%s %.6f %.6f%s", "complex number 2: ", real2, img2 ,"i\n");
  
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\n");

}
  
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\n");

}
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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\n");
System.out.printf("%s %.6f %.6f%s", "complex number 2: ", real2, img2 ,"i\n");
  
addition(real1, real2, img1, img2);
subtraction(real1, real2, img1, img2);

multiply(real1,real2,img1,img2);

divide(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\n");

}
  
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\n");

}
  public static double multiply(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","multiply result: ", sumReal, sumImg, "i\n");

}

public static double divide(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","divide result: ", sumReal, sumImg, "i\n");

}
}

Add a comment
Know the answer?
Add Answer to:
Create a ComplexNo class for two imaginary numbers. Provide (in addition to add, subtract, multiply, and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form:...

    Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form: realPart + imaginaryPart * i where i is √-1 Use double variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should contain default values of (1,1) i.e. 1 for the real part and 1 for the imaginary part. Provide public member functions that perform the following...

  • C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the r...

    C++ Addition of Complex Numbers Background Knowledge A complex number can be written in the format of , where and are real numbers.   is the imaginary unit with the property of .   is called the real part of the complex number and   is called the imaginary part of the complex number. The addition of two complex numbers will generate a new complex number. The addition is done by adding the real parts together (the result's real part) and adding the...

  • Define a class named COMPLEX for complex numbers, which has two private data members of type...

    Define a class named COMPLEX for complex numbers, which has two private data members of type double (named real and imaginary) and the following public methods: 1- A default constructor which initializes the data members real and imaginary to zeros. 2- A constructor which takes two parameters of type double for initializing the data members real and imaginary. 3- A function "set" which takes two parameters of type double for changing the values of the data members real and imaginary....

  • The Calculator Class Create an application dlass called Colcustor with the following members Methods Retuns firtNumber...

    The Calculator Class Create an application dlass called Colcustor with the following members Methods Retuns firtNumber secondumber static void mainsering Asks for two integer undan uiet", ..",ЛТеп shows the result according to Note Use the attached example program and add required methods. Then une a switch tructure in the try black to call proper method according to the input operator Sample Output 1 : 2074 Sample Output 2 D1VLdeBy LerOWLthEkceptionHahaling-Jav / Handling ArithmeticExceptions and InputMismatchExceptions. import java.util.InputMismatchException; import java.util.Scanner; public...

  • C++ // Need to create a class which contains and operates on complex numbers. A complex...

    C++ // Need to create a class which contains and operates on complex numbers. A complex number is defined as “a number that can be expressed in the form a + bi, where a and b are real numbers, and i is imaginary” a is called the real part and b is called the imaginary part. The class MyComplexClass will store the complex number as separate real part and imaginary part values. MyComplexClass Private Member Variables *One double for the...

  • A complex number is a number in the form a + bi, where a and b...

    A complex number is a number in the form a + bi, where a and b are real numbers and i is sqrt( -1). The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas: a + bi + c + di = (a + c) + (b + d)i a + bi - (c + di)...

  • I have Majority of the code written but I just need to implement the <,>,and =...

    I have Majority of the code written but I just need to implement the <,>,and = sign in the code. I have posted the instructions for the whole code. I will add what I have at the end. Objectives: Implement basic class concepts in Java Implement inheritance with super and sub-classes Implement basic polymorphism concepts Problem: The TARDIS has been infected by a virus which means it is up to Doctor Who to manually enter calculations into the TARDIS interface....

  • LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR...

    LANGUAGE IS JAVA CALCULATOR USING SCIENTIFIC/STANDARD MODE ERRORS: ADDITION AND DIVISION NOT WORKING FOR EITHER CALCULATOR CODE: import java.util.Scanner; public class CalculatorProject { public static void main (String[] args) { Scanner yurr = new Scanner(System.in);    String mode; double i; String choice; String answer = "Y"; while (answer.equals("Y")) { System.out.println("Enter the calculator mode: Standard/Scientific?"); mode = yurr.next(); if(mode.equals("Scientific")) { System.out.print("Enter '+' for addition, '-' for subtractions, '*' for multiplication, '/' for division, 'sin' for sin x, 'cos' for cos x,...

  • Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the...

    Assignment: Write a program that prompts the user to enter 10 numbers, and then displays the mean and standard deviation of these numbers I already have the source code all done. I just need this translated into a flowchart. Again I just need the flowchart, not any source code. I have already provided the source code below. Need Flowchart version of my code Here is the source code: public class Mean_Standard_Deviation {    public static void main(String[] args) {   ...

  • The main method of your calculator program has started to get a little messy. In this...

    The main method of your calculator program has started to get a little messy. In this assignment, you will clean it up some by moving some of your code into new methods. Methods allow you to organize your code, avoid repetition, and make aspects of your code easier to modify. While the calculator program is very simple, this assignment attempts to show you how larger, real world programs are structured. As a new programmer in a job, you will likely...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT