Results 1 to 5 of 5
  1. #1
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default limiting the value allowed in a set method

    I have an exercise for university and I can do the rest of it, except the part that wants us the limit the setBearing method of and airplane class to only take (n, nw ,ne , s, etc.)

    Part of the question:

    This exercise could be amended with error-checking in all the set methods, for instance no negative
    altitude, bearing should be n, nw, s, sw, etc. If you try to use a set method with a value that is wrong
    you should print an error-message.

    I tried to use a switch statement, but eclipse wont recognise the cases . eg

    public void setBearing(char newBearing){
    switch(newBearing){
    case n : bearing = newBearing;

    but it says case expressions must be constant expressions.

    How else can one set the allowed arguments for a set method?

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    as long as we dont allow the user to enter any bearing other than n,nw,ne,s,sw,se,e,w,

    and if they do, we should print an error message, using println,.

    how would I do this if a user enters a string for example instead of a double?

    or is that part of exception handling?

    havent done enums yet, but I would like to learn everything about java, preferably before we cover it in university,

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    6

    Default

    enums are the more practical and also easier way to go with this.

    also, your current setup would not work. you take in a char, but how would you represent 'ne', 'nw' and such?

    also, it says they need to be constant expressions because your 'n' is not in single quotes to indicate that it's a char, so it's being interpreted as a variable

  5. #5
    Implode is offline Member
    Join Date
    Aug 2009
    Posts
    50
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    enums are the more practical and also easier way to go with this.

    also, your current setup would not work. you take in a char, but how would you represent 'ne', 'nw' and such?

    also, it says they need to be constant expressions because your 'n' is not in single quotes to indicate that it's a char, so it's being interpreted as a variable
    Thanks.

    So could i do declare enum Bearing { N, NE, NW, S, SE, SW, E, W}

    then in the setBearing method,

    if( (arg) != Bearing)
    System.out.println("Error - enter correct bearing e.g. N) ;
    else
    Bearing = (arg;)

Similar Threads

  1. Int Array Allowed in main() Method?
    By Starclopsofish in forum New To Java
    Replies: 19
    Last Post: 10-03-2008, 04:22 PM
  2. Limiting the Dragging area
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-30-2008, 03:51 PM
  3. Limiting the capacity of a cell of JTable
    By rameshraj in forum Advanced Java
    Replies: 0
    Last Post: 03-24-2008, 02:20 PM
  4. Limiting size of ArrayList
    By ravian in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 06:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •