should be

if (this.magnitude()>op2.magn

Solved

Posted on 2004-11-04

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;

}

Anyone have any idea?

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.magnit

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;

}

Anyone have any idea?

21 Comments

Make the class implement Comparable

public int compareTo(Object other) {

Complex otherComplex = (Complex)other;

return (int)(magnitude - otherComplex.magnitude);

}

}

For a simple square, it's probably an unnecessary expense to call these functions

u can also make a function to simulate ^ like this:

public double getToThePower(double d, int power)

{

int inc = power;

double powered = 1;

while (inc > 0)

{

powered = powered * d;

inc--;

}

return powered

}

Cheers!

Right.

But now the author at least knows the existence of Math.pow() ;°)

^ 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

Cheers!

boolean greaterThan = (c1.compareTo(c2) > 0);

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.

Title | # Comments | Views | Activity |
---|---|---|---|

Conflicting setter definitions for property Exception | 5 | 38 | |

abstract class with all non abstract mentods | 6 | 58 | |

wordlen challenge | 3 | 30 | |

Running Jira on Raspberry PI 2? | 3 | 45 |

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**19** Experts available now in Live!