Using Data Structures in jave please help with this project: Requirements: 1. Cr
ID: 3869829 • Letter: U
Question
Using Data Structures in jave please help with this project:
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:
COP3538 Project 1 – Array Searches and Sorts
Liu University of North Florida 2
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
*/
Provide comments in this form for the Project1 class.
Comments for the class:
/**
* COP 3538: Project 1 – Array Searches and Sorts
* <p>
* Description of the class using as many lines as needed
* with <p> between paragraphs. Including descriptions of the
* input required and output generated.
*
* @author <your name>
* @version <the date you last modified the program>
*/
public class Project1
{
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
*/
COP3538 Project 1 – Array Searches and Sorts
Liu University of North Florida 3
Output Example:
COP3538 Project 1
Instructor: Xudong Liu
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
------------------------------------------------------------------------------------------
Mississippi Jackson MS 2,991,207 South 4
New Hampshire Concord NH 1,323,459 New England 2
Pennsylvania Harrisburg PA 12,773,801 Middle Atlantic 18
. . .
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: 3
States sorted by Population.
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
------------------------------------------------------------------------------------------
Wyoming Cheyenne WY 582,658 West 1
Vermont Montpelier VT 626,630 New England 1
North Dakota Bismarck ND 723,393 Midwest 1
. . .
COP3538 Project 1 – Array Searches and Sorts
Liu University of North Florida 4
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: 5
Enter the state name: Florida
Sequential search
State Name: Florida
Capital City: Tallahassee
State Abbr: FL
State Population: 19,552,860
Region: South
US House Seats: 27
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: 5
Enter the state name: Canada
Sequential search
Error: State Canada not found
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: 2
States sorted by State name.
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
------------------------------------------------------------------------------------------
COP3538 Project 1 – Array Searches and Sorts
Liu University of North Florida 5
Alabama Montgomery AL 4,833,722 South 7
Alaska Juno AK 735,132 West 1
Arizona Phoenix AZ 6,626,624 Southwest 9
. . .
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: 5
Enter the state name: Kentucky
Binary search
State Name: Kentucky
Capital City: Frankfort
State Abbr: KY
State Population: 4,395,295
Region: South
US House Seats: 6
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: 23
Invalid choice enter 1-6: 0
Invalid choice enter 1-6: A
Invalid choice enter 1-6: 6
Have a good day!
Explanation / Answer
Project1.java
- - - -
import java.io.*;
import java.nio.file.Files;
import java.text.NumberFormat;
import java.util.*;
public class Project1 {
/**
* The variety of states that will be stacked
* from a document when the program is run.
*/
private static State[] states;
/**
* A case of a Scanner to peruse in
* client contribution from the support.
*/
private static last SCANNER = new Scanner(System.in);
/**
* The principle technique for our program.
*
* @param args
* Arguments entered on the summon
* line (unused).
* @throws IOException
* If a mistake happens when perusing
* a document.
*/
open static void main(String[] args) tosses IOException {
printWelcomeMessages();
readFile();
while (genuine) {
System.out.println();
attempt {
switch (printMenu()) {
case 1:
printStateReport();
break;
case 2:
sortByStateName();
break;
case 3:
sortByPopulation();
break;
case 4:
sortByRegion();
break;
case 5:
findAndPrintGivenState();
break;
case 6:
stop();
break;
default:
toss new Exception();
}
} get (Exception e) {
System.out.println();
System.out.println("Please enter a number from 1 to 6!");
}
}
}
/**
* Prints the appreciated messages to the
* comfort when the program begins.
*/
private static void printWelcomeMessages() {
System.out.println("Project 1");
System.out.println("Array Searches and Sorts");
}
/**
* Reads the record and stores it in the
* states exhibit.
*
* @throws IOException
* If a mistake happens when perusing
* the document.
*/
private static void readFile() tosses IOException {
Document record;
while (genuine) {
System.out.print("Enter the document name: ");
document = new File(SCANNER.nextLine());
on the off chance that (!file.exists()) {
System.out.println();
System.out.println("That document doesn't appear to exist!");
System.out.println();
proceed;
}
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[] data = line.split(",");
String name = information[0];
String capitalCity = information[1];
String truncation = information[2];
int populace = Integer.parseInt(information[3]);
String area = information[4];
int houseSeats = Integer.parseInt(information[5]);
State = new State(name, capitalCity, truncation, populace, area, houseSeats);
states[i - 1] = state;
}
System.out.println();
System.out.println("There were " + states.length + " state records read.");
}
/**
* Prints the menu to the support and
* prompts the client to enter an ID.
*
* @return
* The ID that the client chooses.
*/
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 decision: ");
return Integer.parseInt(SCANNER.nextLine());
}
/**
* Prints the data for each
* 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 : states) {
System.out.println(state);
}
}
/**
* Sorts the states cluster by name.
*/
private static void sortByStateName() {
for (int external = 0; external < states.length - 1; outer++) {
for (int internal = states.length - 1; inward > external; inward - ) {
in the event that (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 arranged by name.");
}
/**
* Sorts the states cluster by populace.
*/
private static void sortByPopulation() {
for (int external = 0; external < states.length - 1; outer++) {
int most minimal = external;
for (int internal = external + 1; inward < states.length; inner++) {
in the event that (states[inner].getPopulation() > states[lowest].getPopulation()) {
most minimal = inward;
}
}
if(lowest != external) {
State temp = states[lowest];
states[lowest] = states[outer];
states[outer] = temp;
}
}
System.out.println();
System.out.println("States arranged by population.");
}
/**
* Sorts the states cluster by area.
*/
private static void sortByRegion() {
int internal, external;
for (external = 1; external < states.length; outer++) {
State temp = states[outer];
internal = external;
while (internal > 0 && states[inner - 1].getRegion().compareTo(temp.getRegion()) > 0) {
states[inner] = states[inner - 1];
internal - ;
}
states[inner] = temp;
}
System.out.println();
System.out.println("States arranged by region.");
}
/**
* Prompts the client to enter the name
* of a state and show its data,
* in the event that it exists.
*/
private static void findAndPrintGivenState() {
System.out.println();
System.out.print("Enter the state name: ");
String stateName = SCANNER.nextLine();
State = invalid;
for (State s : states) {
in the event that (s.getName().equalsIgnoreCase(stateName)) {
state = s;
break;
}
}
System.out.println();
in the event that (state == invalid) {
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 decent day!");
System.exit(0);
}
}
- - - -
State.java
- -
import java.text.NumberFormat;
open class State actualizes Comparable<State> {
private String name;
private String capitalCity;
private String contraction;
private int populace;
private String district;
private int houseSeats;
open State(String name, String capitalCity, String contraction, int populace, String locale,
int houseSeats) {
this.name = name;
this.capitalCity = capitalCity;
this.abbreviation = contraction;
this.population = populace;
this.region = district;
this.houseSeats = houseSeats;
}
open String getName() {
return this.name;
}
open void setName(String name) {
this.name = name;
}
open String getCapitalCity() {
return this.capitalCity;
}
open void setCapitalCity(String capitalCity) {
this.capitalCity = capitalCity;
}
open String getAbbreviation() {
return this.abbreviation;
}
open void setAbbreviation(String contraction) {
this.abbreviation = contraction;
}
open int getPopulation() {
return this.population;
}
open void setPopulation(int populace) {
this.population = populace;
}
open String getRegion() {
return this.region;
}
open void setRegion(String district) {
this.region = district;
}
open int getHouseSeats() {
return this.houseSeats;
}
open void setHouseSeats(int houseSeats) {
this.houseSeats = houseSeats;
}
@Override
open int compareTo(State state) {
return state.name.compareTo(this.name);
}
@Override
open 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", shortened form));
sb.append(String.format("%1$-16s", NumberFormat.getInstance().format(population)));
sb.append(String.format("%1$-20s", locale));
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.