I am trying to create a class to represent complex numbers and then use it to perform various operations (adding, subtracting, multiplying etc). I am having trouble with the magnitude function.
The code I have for magnitude is as follows:
public double magnitude(){
Math.sqrt(real^2 + imaginary^2);
}
**Upon compile the above gives the error: operator ^ cannot be applied to double,double**
Then I want to test if the *calling* object has a greater magnitude than the object specified in the parameter (in this case Complex op2) using this:
public boolean greaterThan (Complex op2){
if (this.magnitude>op2.magnitude)
return true;
else
return false;
}
**Upon compile the above gives the errors: Cannot resolve symbol (^ point to this), Cannot resolve symbol (^ pointing to the . after op2)**
I am extremely hesitant about the code in the second half of the question. I am not sure if I can use the this property that way or not or if I have it backwards.
Some of the initial setup of my class in case it is applicable.
public class Complex {
public double real, imaginary;
public complex(double r, double i){
real=r; imaginary=i;
}
public double magnitude(){
Math.sqrt(Math.pow(real, 2.0) + Marth.pow(imaginary, 2.0) );
}
0
It’s our mission to create a product that solves the huge challenges you face at work every day. In case you missed it, here are 7 delightful things we've added recently to monday to make it even more awesome.
>> For a simple square, it's probably an unnecessary expense to call these functions
Right.
But now the author at least knows the existence of Math.pow() ;°)
Hi Wraith10,
^ operator can not be applied to double, that's right.
This operator means
1) Logical XOR if applied to boolean operands
x ^ y returns true if and only if one operand is true; otherwise, false.
2) Bitwise XOR if applied to integer operands.
The binary bitwise operators perform bitwise operations between corresponding individual bit values in the operands. The result is a new integer value of the promoted type, which can only be either int or long.
A ^ B returns 1 if and only if one of the bits is 1; otherwise, 0.
just replace it with real*real + imaginary*imaginary
compareTo would be useful for using your class in collections. If you just want a simple 'greater than', you can do
boolean greaterThan = (c1.compareTo(c2) > 0);
0
Wraith10Author Commented:
Using your feedback I've changed my real^2 and imaginary^2 into real*real and imaginary*imaginary. I can't believe I didnt think of doing that I just panicked when it didnt work. I also implemented the if (this.magnitude()>op2.magnitude()) change that was suggested.
I appreciate the other suggestions that were made but for now those appear to be the simplest corrections. No worries I will PAQ this question once I get the test harness written in no more than a couple of days but I want to leave it open for now in case I encounter unexpected errors.
Do you really only want to award my comment? Or did you also want to award one of CEHJ's?
If you made a mistake, you (or we) can always ask a moderator to reopen this question.
Once reopened you can reaccept by splitting the points among the two of us.
Let us know.
With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.
should be
if (this.magnitude()>op2.magn