Question

In: Computer Science

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 case no initializers are provided. Also, a Method to assign data to data fields. Provide public member Methods for each of the following:

  1. Adding two complex numbers. The real parts are added together and the imaginary parts are added together.
  2. Subtracting 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.
  3. Multiplying two Complex numbers.
  4. Dividing two Complex numbers.
  5. Printing complex numbers in the form (a, b), where a is the real part and b is the imaginary part.

Result:            Display the result on Message Dialog Box as shown below:

Result

               a = (9.5 , 7.7)

               b = (1.2 , 3.1)

          a + b = (10.7, 10.8)

          a – b = (8.3, 4.9)

                                OK

             

Lastly, Create a source file for your program, compile it, execute it, and paste the result on the source file.

Solutions

Expert Solution

// Complex.java

public class Complex {
  
   // fields
   private double realPart ;
   private double imaginaryPart ;
  
   // default constructor
   public Complex()
   {
       this(0,0);
   }
  
   // parameterized constructor
   public Complex(double real, double imaginary)
   {
       this.realPart = real;
       this.imaginaryPart = imaginary;
   }
  
   // method to set the real part
   public void setReal(double value)
   {
       realPart = value;
   }
  
   // method to set the imaginary part
   public void setImaginary(double value)
   {
       imaginaryPart = value;
   }
  
   // method to return real part
   public double getReal()
   {
       return realPart;
   }
  
   // method to return imaginary part
   public double getImaginary()
   {
       return imaginaryPart;
   }
  
   // method that returns a new Complex object
// which is the sum of the calling object and the passed object.
public Complex addition(Complex complex) {
return new Complex(this.realPart + complex.realPart, this.imaginaryPart + complex.imaginaryPart);
}

// method that returns a new Complex object
// which is the subtraction of the calling object and the passed object.
public Complex subtraction(Complex complex) {
return new Complex(this.realPart - complex.realPart, this.imaginaryPart - complex.imaginaryPart);
}

  
// method that returns a new Complex
// object which is the product of two passed complex numbers.
public Complex multiplication(Complex c2) {

double r = this.realPart * c2.realPart - this.imaginaryPart * c2.imaginaryPart;
double i = this.realPart * c2.imaginaryPart + this.imaginaryPart * c2.realPart;

return new Complex(r, i);
}
  
// method that returns a new Complex
// object which is the this Complex number divided by passed object
public Complex division(Complex c2) {

   double denominator = (Math.pow(c2.realPart,2) + Math.pow(c2.imaginaryPart, 2));
double r = (this.realPart * c2.realPart + this.imaginaryPart * c2.imaginaryPart) / denominator;
double i = (this.imaginaryPart * c2.realPart - this.realPart * c2.imaginaryPart)/ denominator;

return new Complex(r, i);
}

// method to return string representation of the Complex number
public String toString()
{
   return String.format("(%.1f, %.1f)",realPart,imaginaryPart);
}
}

//end of Complex.java

// ComplexTester.java

import javax.swing.JOptionPane;

public class ComplexTester {
  
public static void main(String[] args) {

       // test the class
       Complex a = new Complex(9.5, 7.7);
       Complex b = new Complex(1.2, 3.1);
       Complex add = a.addition(b);
       Complex sub = a.subtraction(b);
       Complex mul = a.multiplication(b);
       Complex div = a.division(b);
       JOptionPane.showMessageDialog(null, "a = "+a.toString()+"\nb = "+b.toString()+"\na + b = "+add.toString()
               +"\na - b = "+sub.toString()+"\na * b = "+mul.toString()+"\na / b = "+div.toString());
   }

}

//end of ComplexTester.java

Output:


Related Solutions

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...
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 java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
This program is written in Java and should be modularized in methods in one class. This...
This program is written in Java and should be modularized in methods in one class. This program will calculate the Gross Earnings, FICA tax (Medicare and Social Security taxes), Federal Tax Withheld, and Net Amount of the payroll check for each employee of a company. The output must contain numbers with 2 decimal places. The user input must be validated – if incorrect data is entered, send an error message to the user. INPUT The application must be able to...
Write a program in java that does the following: Create a StudentRecord class that keeps the...
Write a program in java that does the following: Create a StudentRecord class that keeps the following information for a student: first name (String), last name (String), and balance (integer). Provide proper constructor, setter and getter methods. Read the student information (one student per line) from the input file “csc272input.txt”. The information in the file is listed below. You can use it to generate the input file yourself, or use the original input file that is available alone with this...
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...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT