In: Computer Science
In this program, you will write a simple program that holds all of the states and their corresponding capitals in a Map.
PROGRAM MUST BE DONE IN JAVA
Your program must have the following features:
·This program will be a Java Console Application called StateCapitals.
·Create a Map to hold the
names of all the states and their corresponding capital
names.
(State name is the key, capital name is the value.)
·Load the HashMap with each
state/capital pair.
(This should be hard-coded.)
·Print all of the state names
to the screen.
(Hint: Get the keys from the map and then print each state name one
by one.)
·Print all of the capital
names to the screen.
(Hint: Get the values from the map and then print each capital name
to the screen one by one.)
·Print each state along with
its capital to the screen.
(Hint: Use the key set to get each value from the map one by one,
printing both the key and value as you go.)
Sample output (order may vary):
STATES:
=======
Alabama
Alaska
Arizona
Arkansas
…
…
CAPITALS:
=========
Montgomery
Juneau
Phoenix
Little Rock
…
…
STATE/CAPITAL PAIRS:
====================
Alabama - Montgomery
Alaska - Juneau
Arizona - Phoenix
Arkansas - Little Rock
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class StateCapitals {
public static void main (String[] args) {
HashMap<String, String> map
= new HashMap<>();
map.put("Alabama","Montgomery");
map.put("Alaska","Juneau");
map.put("Arizona","Phoenix");
map.put("Arkansas","Little Rock");
//print map with each state capital
System.out.println("STATE/CAPITAL PAIRS:");
System.out.println("====================");
for (Map.Entry<String, String> e : map.entrySet())
System.out.println(e.getKey() + "---" + e.getValue());
System.out.println("...");
//Print all of the state names to the screen.
System.out.println("STATES:");
System.out.println("=======");
for (String i : map.keySet()) {
System.out.println(i);
}
System.out.println("...");
//print all the capitals
System.out.println("CAPITALS:");
System.out.println("=========");
for (String i : map.values()) {
System.out.println(i);
}
}
}
output: