Question

In: Computer Science

In this program, you will write a simple program that holds all of the states and...

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

Solutions

Expert Solution

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:


Related Solutions

Using python Write a program that displays all of states in the U.S. and display each...
Using python Write a program that displays all of states in the U.S. and display each state that begins with the letter A.
For this assignment you will develop pseudocode and write a C++ program for a simple calculator....
For this assignment you will develop pseudocode and write a C++ program for a simple calculator. You will create both files in Codio. Put your pseudocode and C++ code in the files below. PSEUDOCODE FILE NAME: Calculator.txt C++ SOURCE CODE FILE NAME : Calculator.cpp DESCRIPTION: Write a menu-driven program to perform arithmetic operations and computations on a list of integer input values. Present the user with the following menu. The user will choose a menu option. The program will prompt...
#1    Write  a simple array program that:    Des not use an “external class & demo      program"    [If you wish, you...
#1    Write  a simple array program that:    Des not use an “external class & demo      program"    [If you wish, you may break it down into methods, but that is NOT         required.]    a.     Set up 4 arrays which each hold 6 employee’s data:              int[ ] empid              int[ ] hours              double[ ] rate              double[ ] wages          b.     Set up loops to load the empid, hours and rate arrays    c.     Set up a loop to calculate values for the wages array. TAKE OVERTIME [hours > 40],  INTO CONSIDERATION, thru the use  of...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program...
Write a program in C++ that efficiently implements a skip list that holds integers. Your program should: 1. Accurately implement the skip list ADT using a random number generator and nodes that contain an integer as well as the addresses of adjacent nodes to the left, right, up, and down. 2. Correctly implement the Insert, Search, and Delete operations. 3. Read a series of unique, newline-delineated integers from a file and insert them (one at a time in the order...
I want to write this program in java. Write a simple airline ticket reservation program in...
I want to write this program in java. Write a simple airline ticket reservation program in java.The program should display a menu with the following options: reserve a ticket, cancel a reservation, check whether a ticket is reserved for a particular person, and display the passengers. The information is maintained on an alphabetized linked list of names. In a simpler version of the program, assume that tickets are reserved for only one flight. In a fuller version, place no limit...
You are asked to write a simple C++ phonebook application program. Here are the requirements for...
You are asked to write a simple C++ phonebook application program. Here are the requirements for the application. read the contact information from a given input file (phonebook.txt) into a dynamically created array of Contact objects. Each line of the input line includes name and phone information of a contact. Assume that each name has a single part Allow to perform operations on array of data such as search for a person, create a new contact or delete an existing...
In this question, you are asked to write a simple java program to understand natural language....
In this question, you are asked to write a simple java program to understand natural language. The user will enter the input following the format: Name came to City, Country in Year. For example: Robin came to Montreal, Canada in 2009. Assume a perfect user will follow the exactly above formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay...
You want to write a simple python program for a food delivery company that asks the...
You want to write a simple python program for a food delivery company that asks the office worker how many drivers they require to deliver their packages to different parts of town. When the user enters the number of drivers, the program will ask how many packages each driver will carry to its destination. It will then calculate the total number of packages and display the result on the screen as below. Sample run: How many drivers do you need?...
Write a Java program for a simple bank account. You shall define a Customer class: A...
Write a Java program for a simple bank account. You shall define a Customer class: A customer has a first name, last name, and social security number. The social security number is a String variable and must comply with this format: xxx-xx-xxxx where 'x' is a digit between 0-9. If a customer is supplied with an invalid SSN, a message must be printed stating SSN of the customer is invalid; however, the account still is created. You shall define a...
Write a simple javascript program using express and node.js to create a simple webpage that can...
Write a simple javascript program using express and node.js to create a simple webpage that can lead to other pages within the program. if possible, Comment everything out so I could understand how every things works.  But basically, server should be hosted on localhost:8000 and should have a simple starting page. Maybe a welcome message, with at least two "links" that goes to a "homepage" and then a "exit" page.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT