This is a COP 3538 Data Structures project that i need help writing a code using
ID: 3884491 • Letter: T
Question
This is a COP 3538 Data Structures project that i need help writing a code using Java Language and that reads from a file(states1.csv) and output should be exactly the same as requested in the project. Would apreciate the help.
Requirements:
1. Create a class named State that will store information about a US state and provide methods to get, and set the data, and compare the states by several fields.
a. Fields: State Name, Capital City, State Abbreviation, State Population, Region, US House Seats
b. Constructor
c. Get and set methods for each field
d. Compare method to compare based on State Name (expects a State object as a parameter)
e. Method to print a state
2. Create a class named Project1 that will:
a. Read the file (csv) of states and create an array of state objects containing that data.
b. Offer the user the following options:
1) Print a state report
2) Sort by state name (using Bubble sort)
3) Sort by population (using Selection sort)
4) Sort by Region (using Insertion sort)
5) Find and print a given state (using binary search if the data is sorted by state name, sequential search if not)
6) Quit
c. Implement the given option, then prompt again. (deal with invalid choice)
Project1 should have main and methods for each options 1-5.
d. The State report in option 1 should be in this form:
State Name Capital City State Abbr State Population Region US House Seats
------------------------------------------------------------------------------------------------------------------------------------
Florida Tallahassee FL 19,552,860 South 27
Pennsylvania Harrisburg PA 12,773,801 Middle Atlantic 18
Massachusetts Boston MA 6,692,824 New England 9
e. The State report in option 5 should be in this form:
State Name: Florida
Capital City: Tallahassee
State Abbr: FL
State Population: 19,552,860
Region: South
US House Seats: 27
Provide comments in this form for the State class:
Comments for the class:
/**
* Detailed description of the class.
*
* @author <your name>
* @version <date you last changed the class>
*/
Public method comments:
/**
* Description of the purpose of the method, the meaning of the
* input parameters (if any) and the meaning of the return values
* (if any).
*
* @param parameter description of the parameter (one for each)
* @return description of the return value
*/
Output Example:
COP3538 Project 1
Instructor: name
Array Searches and Sorts
Enter the file name: States1.csv
There were 50 state records read.
1. Print a state report
2. Sort by State name
3. Sort by Population
4. Sort by Region
5. Find and print a given state
6. Quit
Enter your choice: 1
State Name Capital City State Abbr State Population Region US House Seats
Explanation / Answer
Project1.java
-------------------------------------------------
import java.io.*;
import java.nio.file.Files;
import java.text.NumberFormat;
import java.util.*;
public class Project1 {
/**
* The array of states that will be loaded
* from a file when the program is run.
*/
private static State[] states;
/**
* An instance of a Scanner to read in
* user input from the console.
*/
private static final Scanner SCANNER = new Scanner(System.in);
/**
* The main method of our program.
*
* @param args
* Arguments entered on the command
* line (unused).
* @throws IOException
* If an error occurs when reading
* a file.
*/
public static void main(String[] args) throws IOException {
printWelcomeMessages();
readFile();
while (true) {
System.out.println();
try {
switch (printMenu()) {
case 1:
printStateReport();
break;
case 2:
sortByStateName();
break;
case 3:
sortByPopulation();
break;
case 4:
sortByRegion();
break;
case 5:
findAndPrintGivenState();
break;
case 6:
quit();
break;
default:
throw new Exception();
}
} catch (Exception e) {
System.out.println();
System.out.println("Please enter a number from 1 to 6!");
}
}
}
/**
* Prints the welcome messages to the
* console when the program starts.
*/
private static void printWelcomeMessages() {
System.out.println("Project 1");
System.out.println("Array Searches and Sorts");
}
/**
* Reads the file and stores it in the
* states array.
*
* @throws IOException
* If an error occurs when reading
* the file.
*/
private static void readFile() throws IOException {
File file;
while (true) {
System.out.print("Enter the file name: ");
file = new File(SCANNER.nextLine());
if (!file.exists()) {
System.out.println();
System.out.println("That file doesn't seem to exist!");
System.out.println();
continue;
}
break;
}
List<String> lines = Files.readAllLines(file.toPath());
states = new State[lines.size() - 1];
for (int i = 1; i < lines.size(); i++) {
String line = lines.get(i);
String[] information = line.split(",");
String name = information[0];
String capitalCity = information[1];
String abbreviation = information[2];
int population = Integer.parseInt(information[3]);
String region = information[4];
int houseSeats = Integer.parseInt(information[5]);
State state = new State(name, capitalCity, abbreviation, population, region, houseSeats);
states[i - 1] = state;
}
System.out.println();
System.out.println("There were " + states.length + " state records read.");
}
/**
* Prints the menu to the console and
* prompts the user to enter an ID.
*
* @return
* The ID that the user selects.
*/
private static int printMenu() {
System.out.println("1. Print A State Report");
System.out.println("2. Sort By State Name");
System.out.println("3. Sort By Population");
System.out.println("4. Sort By Region");
System.out.println("5. Find And Print A Given State");
System.out.println("6. Quit");
System.out.print("Enter your choice: ");
return Integer.parseInt(SCANNER.nextLine());
}
/**
* Prints the information for every
* state.
*/
private static void printStateReport() {
System.out.println();
System.out.printf("%-19s %-16s %-11s %-12s %-16s %-12s", "State Name", "Capital City", "State Abbr", "State Population", "Region", "US House Seats");
System.out.println();
System.out.println("-------------------------------------------------------------------------------------------------");
for (State state : states) {
System.out.println(state);
}
}
/**
* Sorts the states array by name.
*/
private static void sortByStateName() {
for (int outer = 0; outer < states.length - 1; outer++) {
for (int inner = states.length -1; inner > outer; inner--) {
if (states[inner].compareTo(states[inner - 1]) > 0) {
State temp = states[inner];
states[inner] = states[inner - 1];
states[inner - 1] = temp;
}
}
}
System.out.println();
System.out.println("States sorted by name.");
}
/**
* Sorts the states array by population.
*/
private static void sortByPopulation() {
for (int outer = 0; outer < states.length - 1; outer++) {
int lowest = outer;
for (int inner = outer + 1; inner < states.length; inner++) {
if (states[inner].getPopulation() > states[lowest].getPopulation()) {
lowest = inner;
}
}
if(lowest != outer) {
State temp = states[lowest];
states[lowest] = states[outer];
states[outer] = temp;
}
}
System.out.println();
System.out.println("States sorted by population.");
}
/**
* Sorts the states array by region.
*/
private static void sortByRegion() {
int inner, outer;
for (outer = 1; outer < states.length; outer++) {
State temp = states[outer];
inner = outer;
while (inner > 0 && states[inner - 1].getRegion().compareTo(temp.getRegion()) > 0) {
states[inner] = states[inner - 1];
inner--;
}
states[inner] = temp;
}
System.out.println();
System.out.println("States sorted by region.");
}
/**
* Prompts the user to enter the name
* of a state and display its information,
* if it exists.
*/
private static void findAndPrintGivenState() {
System.out.println();
System.out.print("Enter the state name: ");
String stateName = SCANNER.nextLine();
State state = null;
for (State s : states) {
if (s.getName().equalsIgnoreCase(stateName)) {
state = s;
break;
}
}
System.out.println();
if (state == null) {
System.out.println("Error: " + stateName + " not found!");
return;
}
System.out.println(String.format("%1$-19s", "State Name:") + state.getName());
System.out.println(String.format("%1$-19s", "Capital City:") + state.getCapitalCity());
System.out.println(String.format("%1$-19s", "State Abbr:") + state.getAbbreviation());
System.out.println(String.format("%1$-19s", "State Population:") + NumberFormat.getInstance().format(state.getPopulation()));
System.out.println(String.format("%1$-19s", "Region:") + state.getRegion());
System.out.println(String.format("%1$-19s", "US House Seats:") + state.getHouseSeats());
}
/**
* Quits the program.
*/
private static void quit() {
System.out.println();
System.out.println("Have a good day!");
System.exit(0);
}
}
----------------------------------------------------
State.java
-------------------------
import java.text.NumberFormat;
public class State implements Comparable<State> {
private String name;
private String capitalCity;
private String abbreviation;
private int population;
private String region;
private int houseSeats;
public State(String name, String capitalCity, String abbreviation, int population, String region, int houseSeats) {
this.name = name;
this.capitalCity = capitalCity;
this.abbreviation = abbreviation;
this.population = population;
this.region = region;
this.houseSeats = houseSeats;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getCapitalCity() {
return this.capitalCity;
}
public void setCapitalCity(String capitalCity) {
this.capitalCity = capitalCity;
}
public String getAbbreviation() {
return this.abbreviation;
}
public void setAbbreviation(String abbreviation) {
this.abbreviation = abbreviation;
}
public int getPopulation() {
return this.population;
}
public void setPopulation(int population) {
this.population = population;
}
public String getRegion() {
return this.region;
}
public void setRegion(String region) {
this.region = region;
}
public int getHouseSeats() {
return this.houseSeats;
}
public void setHouseSeats(int houseSeats) {
this.houseSeats = houseSeats;
}
@Override
public int compareTo(State state) {
return state.name.compareTo(this.name);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(String.format("%1$-20s", name));
sb.append(String.format("%1$-20s", capitalCity));
sb.append(String.format("%1$-10s", abbreviation));
sb.append(String.format("%1$-16s", NumberFormat.getInstance().format(population)));
sb.append(String.format("%1$-20s", region));
sb.append(String.format("%1$-20d", houseSeats));
return sb.toString();
}
}
-----------------------------------------------------------------
State.csv
----------------------------
State,Capital,Abbreviation,Population,Region,US House Seats
Mississippi,Jackson,MS,2991207,South,4
New Hampshire,Concord,NH,1323459,New England,2
Pennsylvania,Harrisburg,PA,12773801,Middle Atlantic,18
North Dakota,Bismarck,ND,723393,Midwest,1
Louisiana,Baton Rouge,LA,4625470,South,6
Maine,Augusta,ME,1328302,New England,2
Vermont,Montpelier,VT,626630,New England,1
Delaware,Dover,DE,925749,Middle Atlantic,1
Montana,Helena,MT,1015165,West,1
Ohio,Columbus,OH,11570808,Midwest,16
Iowa,Des Moines,IA,3090416,Midwest,4
Missouri,Jefferson City,MO,6044171,Midwest,8
Kansas,Topeka,KS,2893957,Midwest,4
South Dakota,Pierre,SD,844877,Midwest,1
Texas,Austin,TX,26448193,Southwest,36
Washington,Olympia,WA,6971406,West,10
New Jersey,Trenton,NJ,8899339,Middle Atlantic,12
Alabama,Montgomery,AL,4833722,South,7
South Carolina,Columbia,SC,4774839,South,7
New Mexico,Santa Fe,NM,2085287,Southwest,3
Indiana,Indianapolis,IN,6570902,Midwest,9
Arkansas,Little Rock,AR,2959373,South,4
Wyoming,Cheyenne,WY,582658,West,1
Colorado,Denver,CO,5268367,West,7
Tennessee,Nashville,TN,6495978,South,9
Oregon,Salem,OR,3930065,West,5
Michigan,Lansing,MI,9895622,Midwest,14
Idaho,Boise,ID,1612136,West,2
Nebraska,Lincoln,NE,1868516,Midwest,3
Oklahoma,Oklahoma City,OK,3850568,Southwest,5
Wisconsin,Madison,WI,5742713,Midwest,8
Rhode Island,Providence,RI,1051511,New England,2
New York,Albany,NY,19651127,Middle Atlantic,27
Nevada,Carson City,NV,2790136,West,4
Arizona,Phoenix,AZ,6626624,Southwest,9
California,Sacramento,CA,38332521,West,53
Georgia,Atlanta,GA,9992167,South,14
Florida,Tallahassee,FL,19552860,South,27
West Virginia,Charleston,WV,1854304,Middle Atlantic,3
Hawaii,Honolulu,HI,1404054,West,2
Kentucky,Frankfort,KY,4395295,South,6
Minnesota,St Paul,MN,5420380,Midwest,8
Massachusetts,Boston,MA,6692824,New England,9
Maryland,Annapolis,MD,5928814,Middle Atlantic,8
Connecticut,Hartford,CT,3596080,New England,5
Utah,Salt Lake City,UT,2900872,West,4
Illinois,Springfield,IL,12882135,Midwest,18
North Carolina,Raleigh,NC,9848060,South,13
Alaska,Juno,AK,735132,West,1
Virginia,Richmond,VA,8260405,Middle Atlantic,11
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.