Question

In: Computer Science

n Java, Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have...

n Java,

Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form

realPart + imaginaryPart * i

where i is square root of -1

Use floating-point variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared.

Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform the following operations:

a) Add two Complex numbers: The real parts are added together and the imaginary parts are added together.   So, if we have (a + bi) + (c + di)), the result should be (a + c) + (b + d) i.

b) Subtract two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand. So, if we have (a + bi) - (c + di)), the result should be (a - c) + (b - d) i.

c) Multiply two Complex numbers: The real part of the result is the real part of the right operand multiplies the real part of the left operand minus the imaginary part of the right operand multiply the imaginary part of the left operand. The imaginary part of the result is the real part of the left operand multiply the imaginary part of the left operand plus the imaginary part of the left operand multiply the real part of the right operand. So, if we have (a + bi) * (c + di)), the result should be (ac - bd) + (ad + bc) i.

d) Division two Complex numbers: We set the value of square real part of the denominator plus square imaginary part of the denominator is A. The real part of the result is the real part of the numerator multiplies the real part of the denominator plus the imaginary part of the numerator multiply the imaginary part of the denominator and divided by A. The imaginary part of the result is the real part of the left operand multiply the imaginary part of the left operand plus the imaginary part of the left operand multiply the real part of the right operand. So, if we have (a + bi) / (c + di)), the result should be (ac+bd)+i(bc-ad))/(c2+d2).

e) Print Complex numbers in the form (a, b), where a is the real part and b is the imaginary part.

A public class (Complex as in this assignment’s demo programs) should be implemented to do complex number calculation.

In the main function (ComplexTest as in the demo) you will instantiate two Complex class objects, call Complex class’s Addition, Subtraction, Multiply, Division function and print out the result of the calculations.

The following are the specifications for this assignment.

Complex.java

There are two private double data members (real and imaginary). The real stores the real part of a complex number and the imaginary stores the imaginary part of a complex number.

There are seven public functions (two Complex, add, subtract, multiply, division, and toString) in this classes.

  1. The first Complex function does not take any data. (or call no argument constructor) It initializes 0s to the private data members.
  2. The second Complex function takes two double numbers. (or call two-argument constructor) It assigns the two numbers to the private data members.
  3. The add function takes one Complex object and returns one Complex object. The function does two complex numbers addition.
  4. The subtract function takes one Complex object and returns one Complex object. The function does two complex numbers subtraction.
  5. The multiply function takes one Complex object and returns one Complex object. The function does two complex numbers multiplication.
  6. The division function takes one Complex object and returns one Complex object. The function does two complex numbers division.
  7. The toString function returns a String object but does not take any data. The function returns a parenthesis string which represents complex number. The numbers are in floating point format with only one digit after decimal point.

ComplexTest.java

It contains main function (driver) for this assignment. In the main function you have to do

  1. Create two Complex objects with value of (9.5, 7.7) and (1.2, 3.1).
  2. Print a proper formatted headline “A complex number in the form (x, y) is equal to x + yi, where i is square root of -1.”
  3. Print a second headline "*-Complex numbers calculations-*".
  4. If an object contains (9.5, 7.7 ) and b object contains ( 1.2, 3.1 ). The program will use System.out.printf function to display information by calling a.toString(), b.toString(), a.add( b ).toString(), a.subtract( b ).toString(),a.multiply( b ).toString(), and a.division( b ).toString() as arguments.

Solutions

Expert Solution

I have implemented the Complex.java and ComplexTest.java  per the given description.

Please find the following Code Screenshot, Output, and Code.

ANY CLARIFICATIONS REQUIRED LEAVE A COMMENT

1.CODE SCREENSHOT :

2.OUTPUT :

3.CODE :

public class Complex {
    private double realPart,imaginaryPart;
    /*The first Complex function does not take any data. 
    (or call no argument constructor)
    It initializes 0s to the private data members.*/
    public Complex() {
        this.realPart = 0;
        this.imaginaryPart = 0;
    }
    /*The second Complex function takes two double numbers. 
    (or call two-argument constructor)
    It assigns the two numbers to the private data members.*/
    public Complex(double realPart, double imaginaryPart) {
        this.realPart = realPart;
        this.imaginaryPart = imaginaryPart;
    }
    /*The add function takes one Complex object and 
    returns one Complex object. 
    The function does two complex numbers addition.
    */
    public Complex add(Complex c1){
        double a=this.realPart,b=this.imaginaryPart,c=c1.realPart,d=c1.imaginaryPart;
        return new Complex(a+c,b+d);
    }
    /*The subtract function takes one Complex object and 
    returns one Complex object.
    The function does two complex numbers subtraction.*/
    public Complex subtract(Complex c1){
         double a=this.realPart,b=this.imaginaryPart,c=c1.realPart,d=c1.imaginaryPart;
        return new Complex(a-c,b-d);
    }
    /*The multiply function takes one Complex object and 
    returns one Complex object. The function does two 
    complex numbers multiplication.*/
    public Complex multiply(Complex c1){
         double a=this.realPart,b=this.imaginaryPart,c=c1.realPart,d=c1.imaginaryPart;
        return new Complex(a*c-b*d,a*d+b*c);
    }
    /*The division function takes one Complex object and 
    returns one Complex object.
    The function does two complex numbers division.*/
     public Complex division(Complex c1){
         double a=this.realPart,b=this.imaginaryPart,c=c1.realPart,d=c1.imaginaryPart;
         double den=c*c+d*d;
        return new Complex((a*c+b*d)/den,(b*c-a*d)/den);
    }
     /*The toString function returns a String object but does not take any data.
     The function returns a parenthesis string which represents complex number.
     The numbers are in floating point format with only one digit after decimal point.*/
    @Override
    public String toString(){
        String s="("+String.format("%.1f",this.realPart)+" , "+String.format("%.1f", this.imaginaryPart)+")";
        return s;
    }
}

ComplexTest.java

public class ComplexTest {
    public static void main(String[] args) {
       /*Create two Complex objects with value of 
        (9.5, 7.7) and (1.2, 3.1).*/
       Complex a=new Complex(9.5, 7.7);
       Complex b=new Complex(1.2, 3.1);
       System.out.println("\"A complex number in the form (x, y) is equal to x + yi, where i is square root of -1\"");
       System.out.println("*-Complex numbers calculations-*");
       /*The program will use System.out.printf function to display information by calling 
       a.toString(), b.toString(), 
       a.add( b ).toString(), a.subtract( b ).toString(),
       a.multiply( b ).toString(), and 
       a.division( b ).toString() as arguments.*/
       System.out.printf("The First Complex Number           : %s\n",a.toString());
       System.out.printf("The Second Complex Number          : %s\n",b.toString());
       System.out.printf("The Sum of Complex Numbers         : %s\n",a.add( b ).toString());
       System.out.printf("The Difference of  Complex Numbers : %s\n",a.subtract( b ).toString());
       System.out.printf("The Product of Complex Number s    : %s\n",a.multiply( b ).toString());
       System.out.printf("The Division of Complex Numbers    : %s\n",a.division( b ).toString());
       
       
    }
    
}

Related Solutions

The following program will be written in JAVA. Create a class called complex performing arithmetic with...
The following program will be written in JAVA. Create a class called complex performing arithmetic with complex numbers. Write a program to test your class.                         Complex numbers have the form:                         realPart + imaginaryPart * i                                               ___                         Where i is sqrt(-1)                                                 Use double variables to represent data of the class. Provide a constructor that enables an object of this class to be initialized when it is declared. The constructor should contain default values in...
Must be coded in C#. 10.8 (Rational Numbers) Create a class called Rational for performing arithmetic...
Must be coded in C#. 10.8 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write an app to test your class. Use integer variables to represent the private instance variables of the class—the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it’s declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 1/2 and would be stored in the object...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Create a new Java project called lab1 and a class named Lab1 Create a second class...
Create a new Java project called lab1 and a class named Lab1 Create a second class called VolumeCalculator. Add a static field named PI which = 1415 Add the following static methods: double static method named sphere that receives 1 double parameter (radius) and returns the volume of a sphere. double static method named cylinder that receives 2 double parameters (radius & height) and returns the volume of a cylinder. double static method named cube that receives 1 double parameter...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called...
android studio -Starting with a basic activity, create a new Java class (use File->New->Java class) called DataBaseManager as in Lecture 5 and create a database table in SQLite, called StudentInfo. The fields for the StudentInfo table include StudentID, FirstName, LastName, YearOfBirth and Gender. Include functions for adding a row to the table and for retrieving all rows, similar to that shown in lecture 5. No user interface is required for this question, t -Continuing from , follow the example in...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside...
JAVA program Create a class called Array Outside of the class, import the Scanner library Outside of main declare two static final variables and integer for number of days in the week and a double for the revenue per pizza (which is $8.50). Create a method called main Inside main: Declare an integer array that can hold the number of pizzas purchased each day for one week. Declare two additional variables one to hold the total sum of pizzas sold...
Create a class called triangle_area. The initializing inputs are numbers b and h. In addition to...
Create a class called triangle_area. The initializing inputs are numbers b and h. In addition to the initialization method, the class must have the area method, which calculates the area of ​​a triangle (A = b x h / 2) and creates an attribute called Area with that value. Use python sintaxis
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT