Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Nested if's

 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am supposed tochange this from a nested if to something else but I don't know what else to changed it to any help would be appricated.

 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could implement it usning enums.
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think he wanted me to get rid of the if's all together...doesn't emun's use ifs
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a HashMap with the timezones as keys and the minutes as Integer values.
[ March 31, 2006: Message edited by: Joanne Neal ]
 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whats a hashmap
 
Paul Clapham
Sheriff
Posts: 21319
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or if this is a real-life question and not just a beginner's programming exercise, you could replace the whole thing by calls to various methods in the TimeZone class. No "ifs" would be required there.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You dont have to use if's if you use enums. You could use the valueOf() method.

 
Victoria Preston
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I am in intermidate programming but its the beginner stuff I don't understand.....I am having trouble with the basic concepts...thats why I post here
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Map approach is something I use all the time. Look at the JavaDoc for all the details. Here's a method that might be similar to yours:

See if you can read up on HashMap enough to do somethin glike that.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like the perfect fit for the Strategy Design Pattern. Some proponents of language theory proclaim that a single if/else is a broken construct and does not belong in a complete language. While I haven't bought that argument just yet, I certainly agree with the more wider (though still small relative to the critical mass) understanding that a if/else/else (3 or more statement bodies) should always be avoided. I even know a few who claim that for loops are flawed constructs!! I haven't bought that argument, simply because I don't understand it in completeness yet, but I suspect that they are ultimately right. For the same reason - appropriate abstraction/accurate requirement expression - switch/case constructs should always be avoided.

The point being, I suspect that your teacher (a knowledagable academic?) is probably trying to teach you something related to this and I encourage you to pursue it further.

The Strategy Design Pattern - one of few legitimate design patterns - is implemented in Java almost always using a java.util.Map. If you wish to go for further purity (but falling infinitely short implied by Java), you might want to consider net.tmorris.adt.map.Map - or just write the interface yourself. Minimalism is paramount even though it is often unachievable under a given context. So often I observe suffering as a result of excessive expression of requirement/contract, and worse, I observe the resulting "problem" attributed to a consequence, instead of the underlying flaws.

Good to hear of a tertiary institution on the right path ...if indeed that is what is happening.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:
Good to hear of a tertiary institution on the right path ...if indeed that is what is happening.


For all we know the instructor is trying to push them towards a type-safe enum and a switch/case so I wouldn't get my hopes up too much.

As for the OP's question, I think using a Map is your best bet and someone already gave you sample code for that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic