Write a Complex number class. It will have a default constructor, explicit const
ID: 3546712 • 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. Your class should have at least 2 constructors: a default constructor, and an explicit constructor with two arguments of type double. ( choose descriptive variable names: a,b,c and d are not acceptable variable names.)
here is the demo:
public class ComplexNumberDemo
{
public static void main (String[] args)
{
ComplexNumber cn1 = new ComplexNumber (4, 5);
ComplexNumber cn2 = new ComplexNumber (3, -2);
ComplexNumber cn3, cn4, cn5, cn6, cn7;
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);
}
}
/*
Here are some examples to test your program:
If c1 = 4 + 5i and c2 = 3 -2i then
the sum of c1 and c2 is 7.0 + 3.0i
subtracting c2 from c1 is 1.0 + 7.0i
multiplying c1 and c2 is 22.0 + 7.0i
dividing c1 by c2 is 0.1538461538 + 1.7692307692i
*/
Explanation / Answer
import java.util.*;
class ComplexNumber
{
private double real;
private double img;
public ComplexNumber()
{
real=0;
img =0;
}
public ComplexNumber(double r,double i)
{
real=r;
img =i;
}
public void set_real(double r)
{
real = r;
}
public void set_img(double i)
{
img=i;
}
public double get_real()
{
return real;
}
public double get_img()
{
return img;
}
public void read()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter real part of complex number :");
real = in.nextDouble();
System.out.println("Enter imaginary part of complex number :");
img = in.nextDouble();
}
public ComplexNumber add(ComplexNumber C1)
{
return new ComplexNumber(real+C1.real,img+C1.img);
}
public ComplexNumber subtract(ComplexNumber C1)
{
return new ComplexNumber(real-C1.real,img-C1.img);
}
public ComplexNumber multiply(ComplexNumber C1)
{
return new ComplexNumber(real*C1.real - img*C1.img,img*C1.real + real*C1.img);
}
public ComplexNumber divide(ComplexNumber C1)
{
double down = C1.real*C1.real+C1.img*C1.img;
return new ComplexNumber( (real*C1.real + img*C1.img)/down,(img*C1.real - real*C1.img)/down);
}
public boolean equals(ComplexNumber C1)
{
return (real==C1.real && img==C1.img);
}
public String toString()
{
if(img>0)
return real+ " + " + img + "i";
return real+ " " + img + "i";
}
}
public class ComplexNumberDemo
{
public static void main (String[] args)
{
ComplexNumber cn1 = new ComplexNumber (4, 5);
ComplexNumber cn2 = new ComplexNumber (3, -2);
ComplexNumber cn3, cn4, cn5, cn6, cn7;
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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.