I have the code included that i used to intialize the complex number but i cant
ID: 3887050 • Letter: I
Question
I have the code included that i used to intialize the complex number but i cant figure out what im doing with the adding or subtracting. I have tried as you can see but it still doesnt seem like any of it is right to me.
public class ComplexNumber
{
private double imaginary;
private double real;
public ComplexNumber()
{
imaginary = 0.0;
real = 0.0;
}
/**
* Used to create a ComplexNumber with passed real and imaginary values.
* @param realIn Initial real value
* @param imaginaryIn Initial imaginary value
*/
public ComplexNumber(double realIn, double imaginaryIn)
{
real = realIn;
imaginary = imaginaryIn;
}
/**
* Adds this ComplexNumber to a passed ComplexNumber, creating a new ComplexNumber.
* This is done by creating a new ComplexNumber with the two operands' real and imaginary parts summed together.
* @param operand2 ComplexNumber to be added to calling instance of ComplexNumber
* @return ComplexNumber that is the summation of calling and passed instance
*/
public ComplexNumber add(ComplexNumber operand2)
{
ComplexNumber abd = new ComplexNumber(this.real, this.imaginary);
// *** ComplexNumber *** this;
double zzz = abd.real + operand2.real;
double xxx = cde.imaginary + operand2.imaginary;
return new ComplexNumber(zzz, xxx);
}
/**
* Subtracts this ComplexNumber with a passed ComplexNumber, creating a new ComplexNumber.
* This is done by creating a new ComplexNumber with the two operands'
* real and imaginary parts subtracted from each other.
* @param operand2 ComplexNumber to be subtracted from calling instance of ComplexNumber
* @return ComplexNumber that is the difference of calling and passed instance
*/
public ComplexNumber subtract(ComplexNumber operand2)
{
}
Explanation / Answer
------------------------------------------------------ComplexNumber.java-------------------------------
public class ComplexNumber {
private double imaginary;
private double real;
public ComplexNumber() {
imaginary = 0.0;
real = 0.0;
}
/**
* Used to create a ComplexNumber with passed real and imaginary values.
* @param realIn Initial real value
* @param imaginaryIn Initial imaginary value
*/
public ComplexNumber(double realIn, double imaginaryIn){
real = realIn;
imaginary = imaginaryIn;
}
/**
* Adds this ComplexNumber to a passed ComplexNumber, creating a new ComplexNumber.
* This is done by creating a new ComplexNumber with the two operands' real and imaginary parts summed together.
* @param operand2 ComplexNumber to be added to calling instance of ComplexNumber
* @return ComplexNumber that is the summation of calling and passed instance
*/
public ComplexNumber add(ComplexNumber operand2){
double r = this.real + operand2.real;
double i = this.imaginary + operand2.imaginary;
return new ComplexNumber(r, i);
}
/**
* Subtracts this ComplexNumber with a passed ComplexNumber, creating a new ComplexNumber.
* This is done by creating a new ComplexNumber with the two operands'
* real and imaginary parts subtracted from each other.
* @param operand2 ComplexNumber to be subtracted from calling instance of ComplexNumber
* @return ComplexNumber that is the difference of calling and passed instance
*/
public ComplexNumber subtract(ComplexNumber operand2){
double r = this.real - operand2.real;
double i = this.imaginary - operand2.imaginary;
return new ComplexNumber(r,i);
}
}
----------------------------------------Test.java-----------------------------------------------------------------
public class Test {
public static void main(String[] srgs){
ComplexNumber c1 = new ComplexNumber(8,3);
ComplexNumber c2 = new ComplexNumber(4,6);
System.out.println("c1+c2= "+c1.add(c2));
System.out.println("c1+c2= "+c1.subtract(c2));
}
}
------------------------------------------------------OUTPUT-------------------------------------------
c1+c2= 12.0 + 9.0i
c1+c2= 4.0 - 3.0i