1. Every state has a unique, two character state code. This program creates a HashMap and loads it with data for a few states. See if you can can fill-in the blanks to retrieve and display the data for one of the states based upon the value of the two character state code. If no such state code exists, the error message should be displayed.
import java.util.*; public class Lab41A { public static void main(String[] args) { Map states = new HashMap(); states.put("CT", "Connecticut"); states.put("AZ", "Arizona"); states.put("IA", "Iowa"); states.put("NM", "New Mexico"); String stateCode = _______; if (states._____________________(stateCode)) { String stateName = __________________________________________; System.out.println(stateCode + " - " + stateName); } else { System.out.println("Invalid state code: " + stateCode); } } }
The theme of this forum is stated as "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."
I'm happy to help, but what is your question? This appears to be a homework assignment.
If I were to guess at a the point of the assignment, you are supposed to learn how to use the Java API to fill-in the blanks. You will probably find what you need by looking up the HashMap class at the Java SE5 API
Mike [ February 14, 2007: Message edited by: Mike Mc Afee ]
You'll find we don't give out a lot of answers without making you do the leg work first. Trust me, figuring it out yourself is much more valuable. And fun.
Have you spent some time with the Map interface in the JavaDoc? See if you can pick out the methods you need for a couple of those.
You could get StateCode from a couple different places. Let's say you put it on the command line when you start the program, like this:
Do you know where those arguments wind up so you can get one?
Welcome to the Ranch. We saw Lab40A a couple of days ago.
You know we don't give people answers to their homework assignments, but we do give them hints which might help them work it out.
We presume you are familiar with the API specifications website. You should get three frames; click HashMap in the lower left frame. Now go through the methods which HashMap has and you should be able to work out which of the methods goes into which of the blanks. Then try it with stateCode = "CT"; and stateCode = "AK"; See what happens.
You will notice I have sent you to the J2SE1.4.2 documentation. You have been specified an old version of HashMap, so J2SE1.4.2 is more appropriate. I am afraid I can't find the older versions of the tutorial to refer you to; the later editions are here.
I am not looking for answer. I am trying to understand what is going on in the program. I am not sure what is being asked ? We are encouraged to get assitance from what ever sources are available. As many of you know... programming is built with trail and error. So far with this I am getting error. I dont understand the "string statecode =" line. I think I have figured the rest out . But I may be wrong. [ February 14, 2007: Message edited by: Nick Hogue ]
Your program will now print something like this: CT - CT
That's not right. It should print something like this: CT - Connecticut
Instead of making stateName equal to stateCode, you're supposed to look up the state name in the map. Now, what should you change in that line to look up the name of the state?
import java.util.*; public class testMap { public static void main(String[] args) { Map states = new HashMap(); states.put("CT", "Connecticut"); states.put("AZ", "Arizona"); states.put("IA", "Iowa"); states.put("NM", "New Mexico");