-
-
Save bytecodeman/916e5cc99009f42857be2f315749251e to your computer and use it in GitHub Desktop.
CSC-111 State Data Retrieval Systems
This file contains 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 library; | |
public class LinearSearch { | |
public static int search(String[] array, String key) { | |
for (int i = 0; i < array.length; i++) | |
if (array[i].equalsIgnoreCase(key)) | |
return i; | |
return -1; | |
} | |
public static int search(int[] array, int key) { | |
for (int i = 0; i < array.length; i++) | |
if (array[i] == key) | |
return i; | |
return -1; | |
} | |
} |
This file contains 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 statedatav3; | |
public class StateData { | |
public String name; | |
public String capital; | |
public double area; | |
public int year; | |
public int order; | |
public String bird; | |
} |
This file contains 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
New Hampshire | |
Concord | |
9304 1788 9 | |
Massachusetts | |
Boston | |
8257 1788 6 | |
Vermont | |
Montpelier | |
9609 1791 14 | |
Rhode Island | |
Providence | |
1214 1790 13 | |
Connecticut | |
Hartford | |
5009 1788 5 | |
Maine | |
Augusta | |
33215 1820 23 | |
New York | |
Albany | |
49576 1788 11 | |
Pennsylvania | |
Harrisburg | |
45333 1787 2 | |
Delaware | |
Dover | |
2057 1787 1 | |
Maryland | |
Annapolis | |
10577 1788 7 | |
Washington | |
Olympia | |
68192 1889 42 | |
Oregon | |
Salem | |
96981 1859 33 | |
Virginia | |
Richmond | |
40815 1788 10 | |
North Carolina | |
Raleigh | |
52586 1789 12 | |
South Carolina | |
Columbia | |
31055 1788 8 | |
Georgia | |
Atlanta | |
58876 1788 4 | |
New Jersey | |
Trenton | |
7836 1787 3 | |
Hawaii | |
Honolulu | |
6450 1959 50 | |
Iowa | |
Des Moines | |
56290 1846 29 | |
Florida | |
Tallahassee | |
58560 1845 27 | |
Tennessee | |
Nashville | |
42244 1796 16 | |
Michigan | |
Lansing | |
58216 1837 26 | |
Ohio | |
Columbus | |
41222 1803 17 | |
Wyoming | |
Cheyenne | |
97914 1890 44 | |
Colorado | |
Denver | |
104247 1876 38 | |
Nebraska | |
Lincoln | |
77227 1867 37 | |
Wisconsin | |
Madison | |
56154 1848 30 | |
South Dakota | |
Pierre | |
77047 1889 39 | |
Alabama | |
Montgomery | |
51609 1819 22 | |
Mississippi | |
Jackson | |
47716 1817 20 | |
Louisiana | |
Baton Rouge | |
48523 1812 18 | |
Arkansas | |
Little Rock | |
53104 1836 25 | |
Missouri | |
Jefferson City | |
69686 1821 24 | |
Kentuckey | |
Frankfort | |
40395 1792 15 | |
Minnesota | |
Saint Paul | |
84068 1858 32 | |
North Dakota | |
Bismark | |
70665 1889 41 | |
Kansas | |
Topeka | |
82264 1861 34 | |
Oklahoma | |
Oklahoma City | |
69919 1907 46 | |
Texas | |
Austin | |
267338 1845 28 | |
New Mexico | |
Santa Fe | |
121666 1912 47 | |
Arizona | |
Phoenix | |
113909 1912 48 | |
Utah | |
Salt Lake City | |
84916 1896 45 | |
Nevada | |
Carson City | |
110540 1864 36 | |
Idaho | |
Boise | |
83557 1890 43 | |
West Virginia | |
Charleston | |
24181 1863 35 | |
California | |
Sacramento | |
158706 1850 31 | |
Alaska | |
Juneau | |
586412 1959 49 | |
Indiana | |
Indianapolis | |
36291 1816 19 | |
Illinois | |
Springfield | |
56400 1818 21 | |
Montana | |
Helena | |
147138 1889 41 |
This file contains 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 statedatav4; | |
public class StateData2 { | |
public String name; | |
public String capital; | |
public double area; | |
public int year; | |
public int order; | |
public StateData2(String sn, String sc, double sa, int sy, int so) { | |
this.name = sn; | |
this.capital = sc; | |
this.area = sa; | |
this.year = sy; | |
this.order = so; | |
} | |
public String toString() { | |
return String.format("%20s%20s", this.capital, this.name); | |
} | |
} |
This file contains 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
/* | |
* Name: Prof. Tony Silvestri | |
* Date: 11/22/2020 | |
* Course Number: CSC-111 | |
* Course Name: Intro to Java Programming | |
* Problem Number: State Data Processing V1 | |
* Email: silvestri@stcc.edu | |
*/ | |
package statedatav1; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.util.Scanner; | |
import library.LinearSearch; | |
public class StateDataSearch1 { | |
private final static String TITLE = "State Data Search App V1.0"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
private final static int STATECOUNT = 50; | |
private final static String FILENAME = "statedata.txt"; | |
// ********************************************** | |
// Put as many methods you need here | |
private static String getFirstCharacter(String str) { | |
str = str.trim().toUpperCase(); | |
return str.isEmpty() ? "" : str.substring(0, 1); | |
} | |
private static void initializeDatabase(String[] stateNames, String[] stateCapitals, double[] stateArea, | |
int[] stateYear, int[] stateOrder) throws FileNotFoundException { | |
Scanner in = new Scanner(new File(FILENAME)); | |
for (int i = 0; i < STATECOUNT; i++) { | |
String sn = in.nextLine(); | |
String sc = in.nextLine(); | |
double sa = in.nextDouble(); | |
int sy = in.nextInt(); | |
int so = in.nextInt(); | |
in.nextLine(); | |
stateNames[i] = sn; | |
stateCapitals[i] = sc; | |
stateArea[i] = sa; | |
stateYear[i] = sy; | |
stateOrder[i] = so; | |
} | |
in.close(); | |
} | |
@SuppressWarnings("unused") | |
private static void outputData(String[] stateNames, String[] stateCapitals) { | |
for (int i = 0; i < stateNames.length; i++) | |
System.out.printf("%15s%15s\n", stateNames[i], stateCapitals[i]); | |
System.out.println(); | |
} | |
private static void searchByStateName(Scanner sc, String[] stateNames, String[] stateCapitals) { | |
System.out.println("Enter a State Name to get its Capital . . ."); | |
do { | |
System.out.print("Enter State Name (EXIT to Quit): "); | |
String name = sc.nextLine(); | |
if (name.equals("EXIT")) | |
break; | |
int index = LinearSearch.search(stateNames, name); | |
if (index != -1) | |
System.out.println(stateCapitals[index] + " is the capital of " + name); | |
else | |
System.out.println("No such state " + name); | |
} while (true); | |
} | |
private static void searchByStateOrder(Scanner sc, String[] stateNames, int[] stateOrder) { | |
System.out.println("Entering Searching By State Order Mode . . ."); | |
do { | |
System.out.print("Enter State Order [1-50] (0 to Quit): "); | |
int order = sc.nextInt(); | |
sc.nextLine(); // Consume Line | |
if (order == 0) | |
break; | |
int index = LinearSearch.search(stateOrder, order); | |
if (index != -1) | |
System.out.println(stateNames[index] + " is state no. " + order); | |
else | |
System.out.println("No state matches order no. = " + order); | |
} while (true); | |
} | |
// ********************************************** | |
// Start your logic coding in the process method | |
private static void process(Scanner input, String args[]) throws Exception { | |
// Parallel Arrays | |
String stateNames[] = new String[STATECOUNT]; | |
String stateCapitals[] = new String[STATECOUNT]; | |
double stateArea[] = new double[STATECOUNT]; | |
int stateYear[] = new int[STATECOUNT]; | |
int stateOrder[] = new int[STATECOUNT]; | |
initializeDatabase(stateNames, stateCapitals, stateArea, stateYear, stateOrder); | |
//outputData(stateNames, stateCapitals); | |
String searchType; | |
do { | |
System.out.print("Enter Search Type Name to [C]apital, [O]rder to Name, E[X]it: "); | |
searchType = getFirstCharacter(input.nextLine()); | |
switch (searchType) { | |
case "C": | |
searchByStateName(input, stateNames, stateCapitals); | |
break; | |
case "O": | |
searchByStateOrder(input, stateNames, stateOrder); | |
break; | |
case "X": | |
break; | |
default: | |
System.out.println("Bad Search Type Specified."); | |
break; | |
} | |
} while (!searchType.equals("X")); | |
} | |
// ********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner input, String prompt) { | |
System.out.print(prompt); | |
String doOver = input.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
// ********************************************** | |
// Do not change the main method | |
public static void main(String args[]) throws Exception { | |
System.out.println("Welcome to " + TITLE); | |
Scanner input = new Scanner(System.in); | |
do { | |
process(input, args); | |
} while (doThisAgain(input, CONTINUE_PROMPT)); | |
input.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
This file contains 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
/* | |
* Name: Prof. Tony Silvestri | |
* Date: 11/22/2020 | |
* Course Number: CSC-111 | |
* Course Name: Intro to Java Programming | |
* Problem Number: State Data Processing V2 - Processing from file on webserver | |
* Email: silvestri@stcc.edu | |
*/ | |
package statedatav2; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.util.Scanner; | |
import library.LinearSearch; | |
public class StateDataSearch2 { | |
private final static String TITLE = "State Data Search App V2.0"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
private final static String DATABASEFILE = "statedata.txt"; | |
private final static int STATECOUNT = 50; | |
// ********************************************** | |
// Put as many methods you need here | |
private static String getFirstCharacter(String str) { | |
str = str.trim().toUpperCase(); | |
return str.isEmpty() ? "" : str.substring(0, 1); | |
} | |
private static void initializeDatabase(String[] stateNames, String[] stateCapitals, double[] stateArea, | |
int[] stateYear, int[] stateOrder) throws IOException { | |
URL url = new URL("https://cs.stcc.edu/~silvestri/csc111/" + DATABASEFILE); | |
Scanner in = new Scanner(url.openStream()); | |
for (int i = 0; i < STATECOUNT; i++) { | |
String sn = in.nextLine(); | |
String sc = in.nextLine(); | |
double sa = in.nextDouble(); | |
int sy = in.nextInt(); | |
int so = in.nextInt(); | |
in.nextLine(); | |
stateNames[i] = sn; | |
stateCapitals[i] = sc; | |
stateArea[i] = sa; | |
stateYear[i] = sy; | |
stateOrder[i] = so; | |
} | |
in.close(); | |
} | |
@SuppressWarnings("unused") | |
private static void outputData(String[] stateNames, String[] stateCapitals) { | |
for (int i = 0; i < stateNames.length; i++) | |
System.out.printf("%15s%15s\n", stateNames[i], stateCapitals[i]); | |
System.out.println(); | |
} | |
private static void searchByStateName(Scanner sc, String[] stateNames, String[] stateCapitals) { | |
System.out.println("Enter a State Name to get its Capital . . ."); | |
do { | |
System.out.print("Enter State Name (EXIT to Quit): "); | |
String name = sc.nextLine(); | |
if (name.equals("EXIT")) | |
break; | |
int index = LinearSearch.search(stateNames, name); | |
if (index != -1) | |
System.out.println(stateCapitals[index] + " is the capital of " + name); | |
else | |
System.out.println("No such state " + name); | |
} while (true); | |
} | |
private static void searchByStateOrder(Scanner sc, String[] stateNames, int[] stateOrder) { | |
System.out.println("Entering Searching By State Order Mode . . ."); | |
do { | |
System.out.print("Enter State Order [1-50] (0 to Quit): "); | |
int order = sc.nextInt(); | |
sc.nextLine(); // Consume Line | |
if (order == 0) | |
break; | |
int index = LinearSearch.search(stateOrder, order); | |
if (index != -1) | |
System.out.println(stateNames[index] + " is state no. " + order); | |
else | |
System.out.println("No state matches order no. = " + order); | |
} while (true); | |
} | |
// ********************************************** | |
// Start your logic coding in the process method | |
private static void process(Scanner input, String args[]) throws Exception { | |
String stateNames[] = new String[STATECOUNT]; | |
String stateCapitals[] = new String[STATECOUNT]; | |
double stateArea[] = new double[STATECOUNT]; | |
int stateYear[] = new int[STATECOUNT]; | |
int stateOrder[] = new int[STATECOUNT]; | |
initializeDatabase(stateNames, stateCapitals, stateArea, stateYear, stateOrder); | |
//outputData(stateNames, stateCapitals); | |
String searchType; | |
do { | |
System.out.print("Enter Search Type Name to [C]apital, [O]rder to Name, E[X]it: "); | |
searchType = getFirstCharacter(input.nextLine()); | |
switch (searchType) { | |
case "C": | |
searchByStateName(input, stateNames, stateCapitals); | |
break; | |
case "O": | |
searchByStateOrder(input, stateNames, stateOrder); | |
break; | |
case "X": | |
break; | |
default: | |
System.out.println("Bad Search Type Specified."); | |
break; | |
} | |
} while (!searchType.equals("X")); | |
} | |
// ********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner input, String prompt) { | |
System.out.print(prompt); | |
String doOver = input.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
// ********************************************** | |
// Do not change the main method | |
public static void main(String args[]) throws Exception { | |
System.out.println("Welcome to " + TITLE); | |
Scanner input = new Scanner(System.in); | |
do { | |
process(input, args); | |
} while (doThisAgain(input, CONTINUE_PROMPT)); | |
input.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
This file contains 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
/* | |
* Name: Prof. Tony Silvestri | |
* Date: 11/22/2020 | |
* Course Number: CSC-111 | |
* Course Name: Intro to Java Programming | |
* Problem Number: State Data Processing V3 | |
* Email: silvestri@stcc.edu | |
*/ | |
package statedatav3; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.util.Scanner; | |
import library.LinearSearch; | |
public class StateDataSearch3 { | |
private final static String TITLE = "State Data Search App V3.0"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
private final static String DATABASEFILE = "statedata.txt"; | |
private final static int STATECOUNT = 50; | |
// ********************************************** | |
// Put as many methods you need here | |
private static String getFirstCharacter(String str) { | |
str = str.trim().toUpperCase(); | |
return str.isEmpty() ? "" : str.substring(0, 1); | |
} | |
private static void initializeDatabase(StateData[] states) throws IOException { | |
URL url = new URL("https://cs.stcc.edu/~silvestri/csc111/" + DATABASEFILE); | |
Scanner in = new Scanner(url.openStream()); | |
for (int i = 0; i < STATECOUNT; i++) { | |
states[i] = new StateData(); | |
states[i].name = in.nextLine(); | |
states[i].capital = in.nextLine(); | |
states[i].area = in.nextDouble(); | |
states[i].year = in.nextInt(); | |
states[i].order = in.nextInt(); | |
in.nextLine(); // Consume Line | |
} | |
in.close(); | |
} | |
private static void outputData(StateData[] states) { | |
for (int i = 0; i < states.length; i++) | |
System.out.printf("%15s%15s\n", states[i].name, states[i].capital); | |
System.out.println(); | |
} | |
private static void searchByStateName(Scanner sc, StateData[] states) { | |
System.out.println("Enter a State Name to get its Capital . . ."); | |
do { | |
System.out.print("Enter State Name (EXIT to Quit): "); | |
String name = sc.nextLine(); | |
if (name.equals("EXIT")) | |
break; | |
String stateNames[] = new String[STATECOUNT]; | |
for (int i = 0; i < STATECOUNT; i++) { | |
stateNames[i] = states[i].name; | |
} | |
int index = LinearSearch.search(stateNames, name); | |
if (index != -1) | |
System.out.println(states[index].capital + " is the capital of " + name); | |
else | |
System.out.println("No such state " + name); | |
} while (true); | |
} | |
private static void searchByStateOrder(Scanner sc, StateData[] states) { | |
System.out.println("Entering Searching By State Order Mode . . ."); | |
do { | |
System.out.print("Enter State Order [1-50] (0 to Quit): "); | |
int order = sc.nextInt(); | |
sc.nextLine(); // Consume Line | |
if (order == 0) | |
break; | |
int stateOrders[] = new int[STATECOUNT]; | |
for (int i = 0; i < STATECOUNT; i++) { | |
stateOrders[i] = states[i].order; | |
} | |
int index = LinearSearch.search(stateOrders, order); | |
if (index != -1) | |
System.out.println(states[index].name + " is state no. " + order); | |
else | |
System.out.println("No state matches order no. = " + order); | |
} while (true); | |
} | |
// ********************************************** | |
// Start your logic coding in the process method | |
private static void process(Scanner input, String args[]) throws Exception { | |
StateData states[] = new StateData[STATECOUNT]; | |
initializeDatabase(states); | |
outputData(states); | |
String searchType; | |
do { | |
System.out.print("Enter Search Type Name to [C]apital, [O]rder to Name, E[X]it: "); | |
searchType = getFirstCharacter(input.nextLine()); | |
switch (searchType) { | |
case "C": | |
searchByStateName(input, states); | |
break; | |
case "O": | |
searchByStateOrder(input, states); | |
break; | |
case "X": | |
break; | |
default: | |
System.out.println("Bad Search Type Specified."); | |
break; | |
} | |
} while (!searchType.equals("X")); | |
} | |
// ********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner input, String prompt) { | |
System.out.print(prompt); | |
String doOver = input.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
// ********************************************** | |
// Do not change the main method | |
public static void main(String args[]) throws Exception { | |
System.out.println("Welcome to " + TITLE); | |
Scanner input = new Scanner(System.in); | |
do { | |
process(input, args); | |
} while (doThisAgain(input, CONTINUE_PROMPT)); | |
input.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
This file contains 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
/* | |
* Name: Prof. Tony Silvestri | |
* Date: 11/22/2020 | |
* Course Number: CSC-111 | |
* Course Name: Intro to Java Programming | |
* Problem Number: State Data Processing V4 | |
* Email: silvestri@stcc.edu | |
*/ | |
package statedatav4; | |
import java.io.IOException; | |
import java.net.URL; | |
import java.util.Scanner; | |
import library.LinearSearch; | |
public class StateDataSearch4 { | |
private final static String TITLE = "State Data Search App V4.0"; | |
private final static String CONTINUE_PROMPT = "Do this again? [y/N] "; | |
private static final String DATABASEFILE = "statedata.txt"; | |
private static final int STATECOUNT = 50; | |
// ********************************************** | |
// Put as many methods you need here | |
private static String getFirstCharacter(String str) { | |
str = str.trim().toUpperCase(); | |
return str.isEmpty() ? "" : str.substring(0, 1); | |
} | |
private static void initializeDatabase(StateData2[] states) throws IOException { | |
URL url = new URL("https://cs.stcc.edu/~silvestri/csc111/" + DATABASEFILE); | |
Scanner in = new Scanner(url.openStream()); | |
for (int i = 0; i < STATECOUNT; i++) { | |
String sn = in.nextLine(); | |
String sc = in.nextLine(); | |
double sa = in.nextDouble(); | |
int sy = in.nextInt(); | |
int so = in.nextInt(); | |
in.nextLine(); | |
states[i] = new StateData2(sn, sc, sa, sy, so); | |
} | |
in.close(); | |
} | |
private static void outputData(StateData2[] states) { | |
for (int i = 0; i < states.length; i++) | |
System.out.println(states[i]); | |
System.out.println(); | |
} | |
private static void searchByStateName(Scanner sc, StateData2[] states) { | |
System.out.println("Enter a State Name to get its Capital . . ."); | |
do { | |
System.out.print("Enter State Name (EXIT to Quit): "); | |
String name = sc.nextLine(); | |
if (name.equals("EXIT")) | |
break; | |
String stateNames[] = new String[STATECOUNT]; | |
for (int i = 0; i < STATECOUNT; i++) { | |
stateNames[i] = states[i].name; | |
} | |
int index = LinearSearch.search(stateNames, name); | |
if (index != -1) | |
System.out.println(states[index].capital + " is the capital of " + name); | |
else | |
System.out.println("No such state " + name); | |
} while (true); | |
} | |
private static void searchByStateOrder(Scanner sc, StateData2[] states) { | |
System.out.println("Entering Searching By State Order Mode . . ."); | |
do { | |
System.out.print("Enter State Order [1-50] (0 to Quit): "); | |
int order = sc.nextInt(); | |
sc.nextLine(); // Consume Line | |
if (order == 0) | |
break; | |
int stateOrders[] = new int[STATECOUNT]; | |
for (int i = 0; i < STATECOUNT; i++) { | |
stateOrders[i] = states[i].order; | |
} | |
int index = LinearSearch.search(stateOrders, order); | |
if (index != -1) | |
System.out.println(states[index].name + " is state no. " + order); | |
else | |
System.out.println("No state matches order no. = " + order); | |
} while (true); | |
} | |
// ********************************************** | |
// Start your logic coding in the process method | |
private static void process(Scanner input, String args[]) throws Exception { | |
StateData2 states[] = new StateData2[STATECOUNT]; | |
initializeDatabase(states); | |
outputData(states); | |
String searchType; | |
do { | |
System.out.print("Enter Search Type Name to [C]apital, [O]rder to Name, E[X]it: "); | |
searchType = getFirstCharacter(input.nextLine()); | |
switch (searchType) { | |
case "C": | |
searchByStateName(input, states); | |
break; | |
case "O": | |
searchByStateOrder(input, states); | |
break; | |
case "X": | |
break; | |
default: | |
System.out.println("Bad Search Type Specified."); | |
break; | |
} | |
} while (!searchType.equals("X")); | |
} | |
// ********************************************** | |
// Do not change the doThisAgain method | |
private static boolean doThisAgain(Scanner input, String prompt) { | |
System.out.print(prompt); | |
String doOver = input.nextLine(); | |
return doOver.trim().equalsIgnoreCase("Y"); | |
} | |
// ********************************************** | |
// Do not change the main method | |
public static void main(String args[]) throws Exception { | |
System.out.println("Welcome to " + TITLE); | |
Scanner input = new Scanner(System.in); | |
do { | |
process(input, args); | |
} while (doThisAgain(input, CONTINUE_PROMPT)); | |
input.close(); | |
System.out.println("Thank you for using " + TITLE); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment