Here is the question I need help with Write a class definition for a complex num
ID: 3613588 • Letter: H
Question
Here is the question I need help with Write a class definition for a complex number.The class definition should include variables that represent thereal and imaginary part of the number.
You should include a constructor that allows you to specify thereal and imaginary parts and a default constructor.
You should also include functions that allow you to add, substract,multiply and divide two complex numbers. The functions return typeshould be complex number.
Also include a function called toString which displays the complexnumber. HERE IS MY CODE # include<iostream> using namespace std class complexNumber { public: complexNumber(); complexNumber(doubleinputreal, double inputimaginary); complexNumber add(complexNumber); complexNumbersubtract(complexNumber); complexNumber multiply(complexNumber); complexNumber divide(complexNumber); void toString(); private: double real; double imaginary; }; int main() { complexNumber number1=complexNumberinput; complexNumber number2=complexNumberinput; complexNumber sum=number1.add(number2); complexNumber difference=number1.subtract(number2); complexNumber product=number1.multiply(number2); complexNumber quotient=number1.divide(number2); sum.toSring(); difference.toString(); product.toString(); quotient.toString(); } //Addi˜tion complexNumber::add(complexNumber input) { complexNumber temp; HOW DO I CONTINUE FROM HERE??
Here is the question I need help with Write a class definition for a complex number.
The class definition should include variables that represent thereal and imaginary part of the number.
You should include a constructor that allows you to specify thereal and imaginary parts and a default constructor.
You should also include functions that allow you to add, substract,multiply and divide two complex numbers. The functions return typeshould be complex number.
Also include a function called toString which displays the complexnumber. HERE IS MY CODE # include<iostream> using namespace std class complexNumber { public: complexNumber(); complexNumber(doubleinputreal, double inputimaginary); complexNumber add(complexNumber); complexNumbersubtract(complexNumber); complexNumber multiply(complexNumber); complexNumber divide(complexNumber); void toString(); private: double real; double imaginary; }; int main() { complexNumber number1=complexNumberinput; complexNumber number2=complexNumberinput; complexNumber sum=number1.add(number2); complexNumber difference=number1.subtract(number2); complexNumber product=number1.multiply(number2); complexNumber quotient=number1.divide(number2); sum.toSring(); difference.toString(); product.toString(); quotient.toString(); } //Addi˜tion complexNumber::add(complexNumber input) { complexNumber temp; HOW DO I CONTINUE FROM HERE??
Explanation / Answer
Dear..Am givingyou this logic in java. Try to implement this in yourown.
Code: public class ComplexDemo { public static void main(String[] args) { public static void main(String[] args) { Complex c = new Complex(3, 5); Complex d = new Complex(2, -2); System.out.println(c); System.out.println(c + ".getReal() = " + c.getReal()); System.out.println(c + " + " + d + " = " + c.add(d)); System.out.println(c + " + " + d + " = " + Complex.add(c, d)); System.out.println(c + " * " + d + " = " + c.multiply(d)); System.out.println(Complex.divide(c, d)); }}class Complex { private double r; private double i; Complex(double rr, double ii) { r = rr; i = ii; } public String toString() { StringBuffer sb = new StringBuffer().append(r); if (i>0) sb.append('+'); return sb.append(i).append('i').toString(); } public double getReal() { return r; } public double getImaginary() { return r; } public double getImaginary() { return i; } public double magnitude()
{ return Math.sqrt(r*r + i*i); } public Complex add(Complex other) { return add(this, other); } public static Complex add(Complex c1, Complex c2) { return new Complex(c1.r+c2.r, c1.i+c2.i); } public Complex subtract(Complex other) { return subtract(this, other);} public static Complex subtract(Complex c1, Complex c2) { return new Complex(c1.r-c2.r, c1.i-c2.i); } public Complex multiply(Complex other) { return multiply(this, other); } public static Complex multiply(Complex c1, Complex c2) { return new Complex(c1.r*c2.r - c1.i*c2.i, c1.r*c2.i + c1.i*c2.r); } public static Complex divide(Complex c1, Complex c2) { return new Complex( (c1.r*c2.r+c1.i*c2.i)/(c2.r*c2.r+c2.i*c2.i), (c1.i*c2.r-c1.r*c2.i)/(c2.r*c2.r+c2.i*c2.i)); } public boolean equals(Object o) {
if (!(o instanceof Complex)) throw new IllegalArgumentException("Complex.equals argument must be a Complex"); Complex other = (Complex)o; return r == other.r && i == other.i; } public int hashCode()
{ return (int)(r)|(int)i; }}