Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please read the notes provided below and fix the issues, Thank you Part 1: Sorting Arrays Develop a program that asks the user

Can you please read the notes provided below and fix the issues, Thank you

Part 1: Sorting Arrays

Develop a program that asks the user to enter a capital for a U.S. state. Upon receiving the user input, the program reports whether the user input is correct. For this application, the 50 states and their capitals are stored in a two-dimensional array in order by state name. Display the current contents of the array then use a bubble sort to sort the content by capital. Next, prompt the user to enter answers for all the state capitals and then display the total correct count. The user's answer is not case-sensitive.

Part 2: Sorting & Searching HashMap

Now revise the code to store the pairs of each state and its capital in a Map using the HashMap function. Display the content of the Map, then use the TreeMap class to sort the map while using a binary search tree for storage. Next, your program should prompt the user to enter a state and it should then display the capital for the state.

Part 1: Sorting Arrays Solution

import java.util.Scanner;

public class Test { /** Main method */ public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner(System.in);

// Store 50 states and their capitals in a two-dimensional array String[][] statesAndCapitals = getData();

int count = 0; // Correct answer // Repeatedly prompt the user to enter the capital of a state for (int i = 0; i

if (isEqual(statesAndCapitals[i][1], capital)) { System.out.println("Your answer is correct"); count++; } else { System.out.println("The correct answer should be " + statesAndCapitals[i][1]); } }

// Display the total correct count System.out.println(" The correct count is " + count); }

/** isEqual returns true if a is equal to c */ public static boolean isEqual(String c, String a) { if (c.length() != a.length()) return false;

for (int i = 0; i

return true; }

/** getData initializes the array with the 50 states and their capitals */ public static String[][] getData() { String[][] d = { {"Alabama", "Montgomery"}, {"Alaska", "Juneau"}, {"Arizona", "Phoenix"}, {"Arkansas", "Little Rock"}, {"California", "Sacramento"}, {"Colorado", "Denver"}, {"Connecticut", "Hartford"}, {"Delaware", "Dover"}, {"Florida", "Tallahassee"}, {"Georgia", "Atlanta"},{"Hawaii", "Honolulu"}, {"Idaho", "Boise"}, {"Illinois", "Springfield"}, {"Indiana", "Indianapolis"}, {"Iowa Des", "Moines"}, {"Kansas", "Topeka"}, {"Kentucky","Frankfort"}, {"Louisiana", "Baton Rouge"}, {"Maine", "Augusta"}, {"Maryland", "Annapolis"}, {"Massachusetts", "Boston"}, {"Michigan", "Lansing"}, {"Minnesota", "Saint Paul"}, {"Mississippi", "Jackson"}, {"Missouri", "Jefferson City"}, {"Montana", "Helena"}, {"Nebraska", "Lincoln"}, {"Nevada ", "Carson City"}, {"New Hampshire", "Concord"}, {"New Jersey", "Trenton"}, {"New Mexico", "Santa Fe"}, {"New York", "Albany"}, {"North Carolina", "Raleigh"}, {"North Dakota", "Bismarck"},{"Ohio", "Columbus"}, {"Oklahoma", "Oklahoma City"}, {"Oregon", "Salem"}, {"Pennsylvania", "Harrisburg"}, {"Rhode Island", "Providence"}, {"South Carolina", "Columbia"}, {"South Dakota", "Pierre"}, {"Tennessee", "Nashville"}, {"Texas", "Austin"}, {"Utah", "Salt Lake City"}, {"Vermont", "Montpelier"}, {"Virginia", "Richmond"}, {"Washington", "Olympia"}, {"West Virginia", "Charleston"}, {"Wisconsin", "Madison"}, {"Wyoming", "Cheyenne"}}; return d; } }

Part 2: Sorting & Searching HashMap Solution

import java.util.Scanner; import java.util.Map; import java.util.TreeMap; import java.util.HashMap;

public class StateCapitals {

public static void main(String[] args) { // create a HashMap to store the state names and capitals Map state_capitals = new HashMap(); // populate the hashmap state_capitals.put("Alabama", "Montgomery"); state_capitals.put("Alaska", "Juneau"); state_capitals.put("Arizona", "Phoenix"); state_capitals.put("Arkansas", "Little Rock"); state_capitals.put("California", "Sacramento"); state_capitals.put("Colorado", "Denver"); state_capitals.put("Connecticut", "Hartford"); state_capitals.put("Delaware", "Dover"); state_capitals.put("Florida", "Tallahassee"); state_capitals.put("Georgia", "Atlanta"); state_capitals.put("Hawaii", "Honolulu"); state_capitals.put("Idaho", "Boise"); state_capitals.put("Illinois", "Springfield"); state_capitals.put("Indiana", "Indianapolis"); state_capitals.put("Iowa", "Des Moines"); state_capitals.put("Kansas", "Topeka"); state_capitals.put("Kentucky", "Frankfort"); state_capitals.put("Louisiana", "Baton Rouge"); state_capitals.put("Maine", "Augusta"); state_capitals.put("Maryland", "Annapolis"); state_capitals.put("Massachusetts", "Boston"); state_capitals.put("Michigan", "Lansing"); state_capitals.put("Minessota", "Saint Paul"); state_capitals.put("Mississippi", "Jackson"); state_capitals.put("Missouri", "Jefferson City"); state_capitals.put("Montana", "Helena"); state_capitals.put("Nebraska", "Lincoln"); state_capitals.put("Nevada", "Carson City"); state_capitals.put("New Hampshire", "Concord"); state_capitals.put("New Jersey", "Trenton"); state_capitals.put("New Mexico", "Santa Fe"); state_capitals.put("New York", "Albany"); state_capitals.put("North Carolina", "Raleigh"); state_capitals.put("North Dakota", "Bismarck"); state_capitals.put("Ohio", "Columbus"); state_capitals.put("Oklahoma", "Oklahoma City"); state_capitals.put("Oregon", "Salem"); state_capitals.put("Pennsylvania", "Harrisburg"); state_capitals.put("Rhode Island", "Providence"); state_capitals.put("South Carolina", "Columbia"); state_capitals.put("South Dakota", "Pierre"); state_capitals.put("Tennessee", "Nashville"); state_capitals.put("Texas", "Austin"); state_capitals.put("Utah", "Salt Lake City"); state_capitals.put("Vermont", "Montpelier"); state_capitals.put("Virginia", "Richmond"); state_capitals.put("Washington", "Olympia"); state_capitals.put("West Virginia", "Charleston"); state_capitals.put("Wisconsin", "Madison"); state_capitals.put("Wyoming", "Cheyenne"); // display the contents of HashMap System.out.printf("%-30s%30s ","State","Capital"); for(String state : state_capitals.keySet()) System.out.printf("%-30s%30s ",state,state_capitals.get(state)); // create a new map using tree map to sort the entries of hash mao Map state_capitals_sorted = new TreeMap(); // loop to populate the tree map for(String state : state_capitals.keySet()) state_capitals_sorted.put(state, state_capitals.get(state)); String state; Scanner console = new Scanner(System.in); // input the state to search System.out.print(" Enter state to search: "); state = console.nextLine(); // check that tree map contains the state, then display the capital of the state else display error message if(state_capitals_sorted.containsKey(state)) System.out.println("Capital of "+state+" is "+state_capitals_sorted.get(state)); else System.out.println("Invalid state: "+state); } }

image text in transcribed

Category Unacceptable (0-2) Open with Needs Improvement Good (7-8) (3-6) Excellent (9-10) Points Correctness (x3) Multiple major errors, or an entirely incorrect response. At least one At least one component component contains a major contains a minor error. All components are completely correct 9/30 error. Standards (x3) poorly written Program is Several Few Program is inappropriate inappropriate stylistically well design choices design choices designed. Variables are (i.e. poor variable (i.e. poor variable names, improper names, improper and the code is named clearly, indentation) indentation) easily readable. 30/30 The code is brute The code is fairly The code is Code Efficiency(x2) The code is huge and appears to be patched together extremely force an efficient without efficient without sacrificing 20/20 unnecessarily readability and sacrificing long understanding readability and understanding, The The documentation is documentation is The tersebut documentation is vague and/or does not give a nonetheless well written and gives a brief brief overview of clearly explains 20/20 what the code is overview of what what the code is the code is accomplishing accomplishing and how accomplishing and how and how Documentation No (x2) documentation exists Total Points: 79/100 Grader Notes: Great work on this program! I like how you use a single structure for both parts, and then fill it in Part 2 The Map functions work as per requirements and there are no errors. However, the bubble sort functionality is missing from this code The code is well-styled and efficient, documentation is solid. As an additional note, you could also expand the program to perform a BST search (https://www.geaksforgeeks.org/binary-search-tree-set-1-search-and-insertion/). This Category Unacceptable (0-2) Open with Needs Improvement Good (7-8) (3-6) Excellent (9-10) Points Correctness (x3) Multiple major errors, or an entirely incorrect response. At least one At least one component component contains a major contains a minor error. All components are completely correct 9/30 error. Standards (x3) poorly written Program is Several Few Program is inappropriate inappropriate stylistically well design choices design choices designed. Variables are (i.e. poor variable (i.e. poor variable names, improper names, improper and the code is named clearly, indentation) indentation) easily readable. 30/30 The code is brute The code is fairly The code is Code Efficiency(x2) The code is huge and appears to be patched together extremely force an efficient without efficient without sacrificing 20/20 unnecessarily readability and sacrificing long understanding readability and understanding, The The documentation is documentation is The tersebut documentation is vague and/or does not give a nonetheless well written and gives a brief brief overview of clearly explains 20/20 what the code is overview of what what the code is the code is accomplishing accomplishing and how accomplishing and how and how Documentation No (x2) documentation exists Total Points: 79/100 Grader Notes: Great work on this program! I like how you use a single structure for both parts, and then fill it in Part 2 The Map functions work as per requirements and there are no errors. However, the bubble sort functionality is missing from this code The code is well-styled and efficient, documentation is solid. As an additional note, you could also expand the program to perform a BST search (https://www.geaksforgeeks.org/binary-search-tree-set-1-search-and-insertion/). This

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Building The Data Warehouse

Authors: W. H. Inmon

4th Edition

0764599445, 978-0764599446

More Books

Students also viewed these Databases questions