tags:

views:

168

answers:

4

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;
}

}

+3  A: 

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);
   }
Paul Tomblin
This is the correct syntax you need.
Logikal
Thanks, I'll use that. But I need to call the value returned from angle() (which is the value stored in the variable angles) in my rotate90 method so I can add 90 to the angles variable. I tried what you suggested but it did not work
Kevin Duke
@Kevin, there isn't a an "angle" stored anywhere. You calculate a new one every time you call angle() from the X and Y coord. If you want to rotate the X and Y coord, you will need to modify them, not just add 90 to a calculated angle. That's a very different problem.
Paul Tomblin
but isn't there a value stored in the variable "angles" in the method angle()? I can't call that in another method?
Kevin Duke
No, it's not storing "angles" anywhere. When angle() returns, angles goes out of scope and disappears.
Paul Tomblin
Ahh, I see. It's just like C where variables are not visible outside of functions.The only problem I have now is that my function is returning type double but it expects type Point. How can I make this work?
Kevin Duke
Doing what was suggested above ( public Point rotate90() { return new Point(-yCoord, xCoord); })Yields the error message: Point is abstract; cannot be instantiated. Why is that?
Kevin Duke
Change that to new CartesianPoint.
Paul Tomblin
+1  A: 

Method invocations in Java always have trailing parentheses even when they don't have any arguments:

rotated = angle() + 90.0;
x4u
A: 

Method call requires parenthesis even if there are no parameters needed. This is different from other languages, e.g.groovy.

fastcodejava
A: 

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.

Jay