Question
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
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started