Lab 4 - Maps
Instructions
- Create a java class named LabTwoFour and place it in the java112.labs2 package.
- Import the java.util package.
- Create a main method that instantiates an instance of the LabTwoFour class and calls the run() method.
- Add an instance variable of type Map.
-
In the run() method add code to accomplish the following:
- Instantiate an instance of HashMap and assign it to the Map variable.
- Put several entries into the map. Each entry should have a String for its key and a new Integer object for that value. Here are a few examples:
- “One”, an Integer object set to 1
- “Two”, an Integer object set to 2
-
Loop through the Map using a Set and the Map.Entry nested class and print out each key and value. Include a label with the key and value so the output makes sense. The output to the screen could look something like this (format not important):
Key: One Value: 1 Key: Two Value: 2
-
Next, just use
System.out.println()
to print the map to the command window. - Use the containsKey method in the Map interface and determine if the key of “Three” is in the Map. If it is then print the key and value to the command window. If not then print an error message.
- Create another instance variable of type Map.
- Instantiate an instance of TreeMap with the first Map as an argument in the TreeMap’s constructor.
- Display the output for this new Map and compare the output with the HashMap results.
Screenshots
To receive credit for this lab save a screenshot in the projects/screenshots/week7
directory.
lab4-happyPath.png
: The command line output when running your program's "happy path".
Rubric
All of the following must be satisfied to achieve a "Met" status
- Screenshot should clearly show expected output when running the program (happy path).
- Code and screenshots are properly named and saved in the correct directory.
- All lab steps have been accurately and appropriately implemented.
- Code adheres to the course coding standards.
- Commit messages are concise, atomic, and effectively describe the change(s) made.
- JavaDoc documentation is properly implemented, providing clarity and understanding of the code's functionality and usage.
- External sources (websites, classmates, AI tools, etc), if utilized, are referenced and documented within the code as comments.
Additional Considerations
- Instance variables are private.