Created
April 30, 2021 03:58
-
-
Save mikeymop/92b2c2ba8a1518c410b6d6edcda23d52 to your computer and use it in GitHub Desktop.
Assignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sortingArrays; | |
import java.util.Random; | |
import java.util.Scanner; | |
/** | |
<b>Part 1: Sorting Arrays</b> | |
<p>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. | |
<p>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. | |
<p>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. | |
*/ | |
public class Part1 { | |
public Part1() { | |
final String[][] data = getData(); | |
quizUser(getRandomState(data), true); | |
System.out.println("\n\nHere are the answers in state order:"); | |
sleep(2000); | |
printArray(data); | |
System.out.println("\n\nHere are the answers in capital order:"); | |
sleep(2000); | |
printArray(bubbleSort(data)); | |
sleep(1000); | |
System.out.println("\nTime for a quiz, can you guess each capital?"); | |
sleep(1000); | |
System.out.printf("It looks like you got %d / 50 correct.", runGame(data)); | |
} | |
/** Runs the actual game where the user guesses each state's capital. | |
* @return int - score Number of correct guesses. | |
*/ | |
public static int runGame(String[][] arr) { | |
int score = 0; | |
for(String[] state: arr) { | |
boolean correct = quizUser(state); | |
if(correct) { score++; } | |
} | |
return score; | |
} | |
/** Iterate through each array element and prints | |
* the state and capital in the array. | |
* @param arr - The array containing the states and their capitals. | |
*/ | |
public static void printArray(String[][] arr) { | |
System.out.printf("%-15s %-15s%n", "State:", "Capital:"); | |
for(String[] state: arr ) { | |
System.out.printf("%-15s %s%n", state[0], state[1]); | |
} | |
} | |
/** | |
* In bubble sort, we traverse the array from first to array_length - 1 | |
* and compare the element with the next one. | |
* Element is swapped with next element if the next element is greater. | |
* | |
* Bubble sort steps are as follows. | |
* 1. Compare array[n] & array[n+1] | |
* 2. If array[n] > array [n+1] swap it. | |
* ... | |
* 5. Compare array[n-1] & array[n] | |
* 6. if [n-1] > array[n] then swap it. | |
* | |
* After this step largest element will be the last index. | |
* Repeat the same steps for array[1] to array[n-1] | |
* @param String[][] - unsortedArray | |
* @return String[][] - sortedArray | |
*/ | |
public static String[][] bubbleSort(String[][] arr) { | |
for(int i = 0; i < arr.length; i++) { | |
for(int j = i+1; j < arr.length; j++) { // you need to iterate twice in bubblesort | |
if(arr[i][1].compareTo(arr[j][1]) > 0) { // i is alphabetically after j | |
String[] temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
} | |
} | |
} | |
return arr; | |
} | |
/** Given a state obj from the state array | |
* ask the user to guess the capital of the state. | |
* @param String[] - state The state we are quizzing the user on. | |
* @optional boolean - feedback If true, print feedback on the users correctness. | |
*/ | |
public static boolean quizUser(String[] state) { | |
Scanner scnr = new Scanner(System.in); // tracks user input | |
System.out.printf("What is the capital of %s?%n", state[0]); | |
String answer = null; | |
try { | |
answer = scnr.next(); // get the user's input | |
} catch(IllegalStateException e) { | |
System.out.println("Error in scanner"); | |
} | |
if(answer.toLowerCase().equals(state[1].toLowerCase())) { | |
return true; // The user guessed right | |
} | |
return false; // The user guessed wrong | |
} | |
/** This overloads the quizUser function | |
* When feedback is true, print the response based on the user's input. | |
* @param state | |
* @param feedback | |
* @return | |
*/ | |
public static boolean quizUser(String[] state, boolean feedback) { | |
if(quizUser(state)) { | |
System.out.println("You're right!"); | |
return true; | |
} | |
System.out.printf("Nope! It's actually %s.%n", state[1]); | |
return false; | |
} | |
/** Gets a random state from the state array */ | |
public static String[] getRandomState(String[][] arr) { | |
int rnd = new Random().nextInt(arr.length); | |
return arr[rnd]; // returns a random number no greater than arr.length | |
} | |
/** I really just have this for readability */ | |
private static void sleep(long milli) { | |
try { | |
Thread.sleep(milli); | |
} catch (InterruptedException e) { // Must handle the Thread exception | |
System.out.println("I'm having trouble sleeping."); | |
Thread.currentThread().interrupt(); | |
} | |
} | |
/** Initialize the 2dm array with all of the states and their capitals. */ | |
public static String[][] getData() { | |
String[][] data = { | |
{"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 data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment