- #1
CAF123
Gold Member
- 2,948
- 88
Homework Statement
Create a class which contains the following:
a)instance variables for the complex number. For x+yi, x and y are the variables.
b)constructor to initialize these variables.
c)methods to return the real and imaginary parts of the complex number
d)method to compute the magnitude of the complex number
e)method to convert complex number to a string (so it can be printed in form {x,y})
f)static method to take 2 complex numbers and return the sum as another complex number
g)static method to take 2 complex numbers, a and b, and return a*b as a third complex number.
The Attempt at a Solution
I have attempted steps a) - d) so far. My method for d) returns errors in the terminal so it is not correct. I don't really understand what e) means.
Code:
class Complex {
double x;
double y; //instance variables for the complex number
Complex() {
this.x = "";
this.y = "";
}
Complex(double x, double y) {
this.x = x;
this.y = y; //constructor to initialise these variables
}
double getX() {
return this.x; //getter method for x, the real component of the complex number
}
double getY() {
return this.y; //getter method for y, the imaginary part of the complex number.
}
double mag;
void magnitude(double mag) {
this.mag = this.Math.sqrt(Math.pow(double x,2) + Math.pow(double y,2)); //method to compute magnitude of number
}
}