1

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

}

asked Jan 21, 2010 at 2:52
4
  • 3
    did you forget the 'homework' tag? Commented Jan 21, 2010 at 2:55
  • 2
    @Carsten funny comment. Still even if it is homework you could help the person out. Commented Jan 21, 2010 at 3:00
  • 4
    Unlike 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. Commented Jan 21, 2010 at 3:02
  • 1
    Please edit your question to have a more descriptive title and consistent code indentation. Commented Jan 21, 2010 at 3:03

4 Answers 4

3

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);
 }
answered Jan 21, 2010 at 2:55
Sign up to request clarification or add additional context in comments.

7 Comments

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, 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.
but isn't there a value stored in the variable "angles" in the method angle()? I can't call that in another method?
No, it's not storing "angles" anywhere. When angle() returns, angles goes out of scope and disappears.
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?
|
1

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

rotated = angle() + 90.0;
Jim Ferrans
31.1k12 gold badges59 silver badges83 bronze badges
answered Jan 21, 2010 at 2:55

Comments

0

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

answered Jan 21, 2010 at 3:25

Comments

0

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.

answered Jan 21, 2010 at 6:43

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.