Need some help on some code for a class (java)

Discussion in 'Mac Programming' started by Watabou, Sep 22, 2009.

  1. Watabou macrumors 68040

    Watabou

    Joined:
    Feb 10, 2008
    Location:
    United States
    #1
    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.
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    What control structures can you use? Is looping allowed?

    Which arithmetic operators are allowed? Division? Modulus?

    Those are hints as much as questions.
     
  3. Watabou thread starter macrumors 68040

    Watabou

    Joined:
    Feb 10, 2008
    Location:
    United States
    #3
    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
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    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
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    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.
     
  6. Watabou thread starter macrumors 68040

    Watabou

    Joined:
    Feb 10, 2008
    Location:
    United States
    #6
    Ah. Didn't think of using a modulo. Thanks lee and chown! :D
     

Share This Page