Results 1 to 5 of 5
- 09-17-2009, 04:42 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 50
- Rep Power
- 0
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
-
- 09-17-2009, 05:16 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 50
- Rep Power
- 0
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,
- 09-17-2009, 05:35 PM #4
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
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
- 09-17-2009, 05:51 PM #5
Member
- Join Date
- Aug 2009
- Posts
- 50
- Rep Power
- 0
Similar Threads
-
Int Array Allowed in main() Method?
By Starclopsofish in forum New To JavaReplies: 19Last Post: 10-03-2008, 04:22 PM -
Limiting the Dragging area
By Preethi in forum New To JavaReplies: 1Last Post: 07-30-2008, 03:51 PM -
Limiting the capacity of a cell of JTable
By rameshraj in forum Advanced JavaReplies: 0Last Post: 03-24-2008, 02:20 PM -
Limiting size of ArrayList
By ravian in forum New To JavaReplies: 3Last Post: 01-29-2008, 06:37 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks