I'm trying to call angles from from the angle method down below in the Rotate90 method but I'm not sure of the syntax. What is the correct syntax?
import java.lang.Math;
public class CartesianPoint implements Point
{
private double xCoord;
private double yCoord;
public CartesianPoint (double xCoordinate,double yCoordinate)
{
xCoord = xCoordinate;
yCoord = yCoordinate;
}
public double xCoordinate ()
{
return xCoord;
}
public double yCoordinate ()
{
return yCoord;
}
public double angle ()
{
double angles;
angles = Math.cos( xCoord / Math.sqrt( xCoord*xCoord + yCoord*yCoord));
return angles;
}
public double radius ()
{
double radius;
radius = (yCoord*yCoord + xCoord*xCoord); //?
return radius;
}
public Point rotate90()
{
double rotated;
rotated = angles.angle + 90.0; //██████████ Error here ██████████
return rotated;
}
public double distanceFrom(Point other)
{
return 0;
}
}
-
3did you forget the 'homework' tag?Carsten– Carsten2010年01月21日 02:55:29 +00:00Commented Jan 21, 2010 at 2:55
-
2@Carsten funny comment. Still even if it is homework you could help the person out.Alfred– Alfred2010年01月21日 03:00:38 +00:00Commented Jan 21, 2010 at 3:00
-
4Unlike half the first timers who ask homework questions, at least Kevin has actually written some code. You gotta give him kudos for at least trying.Paul Tomblin– Paul Tomblin2010年01月21日 03:02:28 +00:00Commented Jan 21, 2010 at 3:02
-
1Please edit your question to have a more descriptive title and consistent code indentation.Jay Conrod– Jay Conrod2010年01月21日 03:03:50 +00:00Commented Jan 21, 2010 at 3:03
4 Answers 4
I think you mean
rotated = angle() + 90.0;
Except I think you'll find that Math.cos uses radians not degrees, so you're not going to get the result you think you are. And shouldn't that be arc cos, not cosine? Something like this might be more what you're looking for:
public double angle()
{
return Math.atan2(ycoord, xcoord) * 180 / Math.PI;
}
If you want rotate90 to return a new Point that is 90 degrees from the current point, then change it to the following:
public Point rotate90()
{
return new CartesianPoint(-yCoord, xCoord);
}
7 Comments
Method invocations in Java always have trailing parentheses even when they don't have any arguments:
rotated = angle() + 90.0;
Comments
Method call requires parenthesis even if there are no parameters needed. This is different from other languages, e.g.groovy.
Comments
To answer your question directly: If you want to be able to access the variable "angles" from outside the "angle" function, you must make it a class variable. That is, declare it outside of any function, like you have declared xCoord and yCoord. Then refer to it by its simple name without any reference to the function that created it.
Note that if rotate90 tries to reference angle before angles() is called, angle will contain zero (or whatever default value you set) and is unlikely to be useful.
It's not clear to me exactly what you're trying to accomplish. I think that your intent is that the angles function would take the arc cosine, not the cosine.