Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Watabou

macrumors 68040
Original poster
Feb 10, 2008
3,426
759
United States
Since I don't know any programming forum out there, I thought I would just post here:

Basically, what this program does is normalize angles between 0 and 360 that you provide. For example, if you provide 380, then the program normalizes it to 20. If you input -30, then the program normalizes it to 330. I have one problem though. Let's say the input is 360010. How would I go about solving that? I know it should output 10 but I can't figure out how to do this. Here is my current code by the way:


public class Angle {

/**
* Normalizes an angle.
* @param angle an integer angle (may be negative)
* @return the equivalent angle in the range 0 - 359
*/

public int normalize(int angle)
{
int normalizedAngle;

if (angle >= 0 && angle <= 360)
normalizedAngle = angle;
else if (angle < 0)
normalizedAngle = 360 + angle;
else
normalizedAngle = angle - 360;


return normalizedAngle;
}
}

Any help or hints on how to work this out would be greatly appreciated. I'm just so frustrated right now because I've been staring at this for quite a while and haven't figured anything out.

P.S.: Nothing too complicated though. We only learned primitive data types, instance fields, classes and that sorts so that should tell you guys how far we are in the class.
 
What control structures can you use? Is looping allowed?

Which arithmetic operators are allowed? Division? Modulus?

Those are hints as much as questions.
 
I can use ifs and else statements. We haven't been taught looping yet but all arithmetic operations are allowed.

I was thinking of division too but I don't know how I would execute that.

Thanks in advance
 
Input modulo x
if result less than y, add x

the whole assignment seems to be coming up with this, though... But you asked, and there's no real way to give advice on this that doesn't give it away. I took out the two constants and replaced them with x and y so there is something left for you to do.

-Lee
 
Look up what the % operator does.

Also think about integer division, by running a few examples, such as 750 / 360, or 360010 / 360. Then think about multiples of a full circle, and what normalization means for more than one full circle.

edit:
lee1210 beat me by one full circle of the second hand.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.