I need this written in Java!! Create a ComplexNumber class to represent a comple
ID: 3707064 • Letter: I
Question
I need this written in Java!!
Create a ComplexNumber class to represent a complex number. A complex number is a number that's of the form a+bi, where a and b are real numbers (a,b E R), but i V-1, also called the imaginary n if you need a refresher Check Wikipedia The fields for a ComplexNumber should be a and b. Create a constructor that initializes values for both a and b. Create instance methods add, subtract, multiply, and divide. Each of these methods will take in one other ComplexNumber and performs the stated operation to produce a new ComplexNumber For example, the add method should look like this: public ComplexNumber add(ComplexNumber other) t Demo by creating some complex numbers and testing them outExplanation / Answer
program:
class ComplexNumber
{
double a,b;
ComplexNumber(double a,double b)
{
this.a=a;
this.b=b;
}
public ComplexNumber add(ComplexNumber C1)
{
ComplexNumber Sum=new ComplexNumber();
Sum.a=a+C1.a;
Sum.b=b+C1.b;
System.out.println("SUM:" + C2.a + "+i" +C2.b);
}
public ComplexNumber sub(ComplexNumber C1)
{
ComplexNumber Diff=new ComplexNumber();
Diff.a=a-C1.a;
Diff.b=b-C1.b;
System.out.println("DIFF:" + C2.a + "+i" +C2.b);
}
public ComplexNumber product(ComplexNumber C1)
{
ComplexNumber Mul=new ComplexNumber();
Mul.a=a*(C1.a);
Mul.b=b*(C1.b);
System.out.println("MUL:" + C2.a + "+i" +C2.b);
}
public ComplexNumber div(ComplexNumber C1)
{
ComplexNumber Div=new ComplexNumber();
Div.a=a/(C1.a);
Div.b=b/(C1.b);
System.out.println("DIV:" + C2.a + "+i" +C2.b);
}
Class ComplexNumbermain
{
public static void main(String[] s)
{
ComplexNumber C=new ComplexNumber(10,20);
ComplexNumber C1=new ComplexNumber(20,40);
ComplexNumber C2=new ComplexNumber();
C2=C2.add(C1);
C2=C2.sub(C1);
C2=C2.product(C1);
C2=C2.div(C1);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.