Write a Complex number class. It will have a default constructor, explicit const
ID: 3634358 • Letter: W
Question
Write a Complex number class. It will have a default constructor, explicit constructor, and the following methods: read () public Complex add(Complex), public Complex subtract(Complex), public Complex multiply(Complex), public Complex divide(Complex), public boolean equals(complex) and a toString() method. Include get and set methods as well. I will provide a main demo program.This is the demo :
public class ComplexNumberDemo
{
public static final int ARRAY_SIZE = 5;
public static void main (String[] args)
{
ComplexNumber cn1 = new ComplexNumber (1, 2);
ComplexNumber cn2 = new ComplexNumber (2, 3);
ComplexNumber cn3, cn4, cn5, cn6, cn7;
ComplexNumber []ComplexNumberArray = new ComplexNumber [ARRAY_SIZE];
System.out.println ("First ComplexNumber number: " + cn1);
System.out.println ("Second ComplexNumber number: " + cn2);
if (cn1.equals(cn2))
System.out.println ("cn1 and cn2 are equal.");
else
System.out.println ("cn1 and cn2 are NOT equal.");
cn4 = cn1.add(cn2);
cn5 = cn1.subtract(cn2);
cn6 = cn1.multiply(cn2);
cn7 = cn1.divide(cn2);
System.out.println ("cn1 + cn2: " + cn4);
System.out.println ("cn1 - cn2: " + cn5);
System.out.println ("cn1 * cn2: " + cn6);
System.out.println ("cn1 / cn2: " + cn7);
//The next two methods must be written by you.
cn7.readComplexNumberArray(ComplexNumberArray);
cn7 = cn7.getAverage(ComplexNumberArray);
System.out.println("the average of the Complex Number array is " + cn7);
}
}
Explanation / Answer
please rate
class ComplexNumber{
double real;
double imaginary;
ComplexNumber(double real,double imaginary){
this.real = real;
this.imaginary = imaginary;
}
public ComplexNumber add(ComplexNumber n){
ComplexNumber result = new ComplexNumber(0, 0);
result.real = real + n.real;
result.imaginary = imaginary + n.imaginary;
return result;
}
public ComplexNumber subtract(ComplexNumber n){
ComplexNumber result = new ComplexNumber(0, 0);
result.real = real - n.real;
result.imaginary = imaginary - n.imaginary;
return result;
}
public ComplexNumber multiply(ComplexNumber n){
ComplexNumber result = new ComplexNumber(0, 0);
result.real = real * n.real - imaginary * n.imaginary;
result.imaginary = real * n.imaginary + imaginary * n.real;
return result;
}
public ComplexNumber divide(ComplexNumber n){
ComplexNumber result = null;
ComplexNumber denominatorConjugate = new ComplexNumber(n.real,-n.imaginary);
ComplexNumber denominator = n.multiply(denominatorConjugate);
result = new ComplexNumber((this.real/denominator.real),(this.imaginary/denominator.real));
result = result.multiply(denominatorConjugate);
return result;
}
}
/**
* The Class ComplexNumberDemo.
*/
public class ComplexNumberDemo {
/**
* The main method.
*
* @param args the arguments
*/
public static void main(String[] args) {
ComplexNumber cn1 = new ComplexNumber(4, 5);
ComplexNumber cn2 = new ComplexNumber(3, -2);
ComplexNumber cn4, cn5, cn6, cn7;
System.out.println("First ComplexNumber number: " + cn1.real + " + "+cn1.imaginary+"i");
System.out.println("Second ComplexNumber number: " + cn2.real + " + "+cn2.imaginary+"i");
if (cn1.equals(cn2))
System.out.println("cn1 and cn2 are equal.");
else
System.out.println("cn1 and cn2 are NOT equal.");
cn4 = cn1.add(cn2);
cn5 = cn1.subtract(cn2);
cn6 = cn1.multiply(cn2);
cn7 = cn1.divide(cn2);
System.out.println("cn1 + cn2: " + cn4.real + " + "+cn4.imaginary+"i");
System.out.println("cn1 - cn2: " + cn5.real+ " + "+cn5.imaginary+"i");
System.out.println("cn1 * cn2: " + cn6.real+" + "+cn6.imaginary+"i");
System.out.println("cn1 / cn2: " + cn7.real+" + "+cn7.imaginary+"i");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.