In: Computer Science
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:
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.
// 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: