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

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)...
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...
C++ Question1. Create a class called Rantional for performing arithmatic with fractions. Then write a program...
C++ Question1. Create a class called Rantional for performing arithmatic with fractions. Then write a program to test your class. Use integer variables to represent the private data of the class, meaning 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 contain default values in case no initializers are provided and should store the fraction in reduced form. For example fraction 2/4 would be stored...
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...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer...
in JAVA Create a class called “MinMax” that satisfies the following requirements: a. create an integer array called nums that has 20 cells b. generate a random number between 5 and 30, and populate the array nums c. print the minimum and maximum number in the array nums d. print sum and average of numbers in the array nums Your output look like this: (Note: numbers shown below will be different in your program due to the random numbers) minimum...
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...
Create a Project and a Class called “FinalGrade” Write a Java program to compute your final...
Create a Project and a Class called “FinalGrade” Write a Java program to compute your final grade for the course. Assume the following (you can hard-code these values in your program). Assignments are worth 30% Labs are worth 40% Mid-term is worth 15% Final is worth 15% Ask for the grades in each category. Store them in a double variable. Print out the final percentage grade.           For example, Final Grade Calculation Enter percentage grades in the following order. Assignments,...
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...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT