I have this Data Structures project that I\'m struggle with and need help. Speci
ID: 3871311 • Letter: I
Question
I have this Data Structures project that I'm struggle with and need help.
Specification:
Do not use Java-provided classes (List, Collections, Comparator) or methods (Collections.sort) for those you are asked to implement.
Here is the State class for previous project to be reused and corrected
import java.util.Comparator;
/**
*
* This class stores the information about a state.
*
* It also facilitates the sorting by
*
* state name, capital, abbreviation, population, region and house seats
*
* and display the information contained in this class.
*
* @author your name
*
* @version September 13, 2017
*
*/
public class State
{
private String stateName;
private String capitalCity;
private String stateAbbreviation;
private int statePopulation;
private String region;
private int houseSeats;
/**
*
* Default constructor
*
*/
public State()
{
}
/**
*
* Constructor with fields
*
* @param stateName
*
* @param capitalCity
*
* @param stateAbbreviation
*
* @param statePopulation
*
* @param region
*
* @param houseSeats
*
*/
public State(String stateName, String capitalCity, String stateAbbreviation, int statePopulation, String region, int houseSeats)
{
super();
this.stateName = stateName;
this.capitalCity = capitalCity;
this.stateAbbreviation = stateAbbreviation;
this.statePopulation = statePopulation;
this.region = region;
this.houseSeats = houseSeats;
}
/**
*
* The getter method for state name
*
* @return stateName
*
*/
public String getStateName()
{
return stateName;
}
/**
*
* The setter method for stateName
*
* @param stateName
*
*/
public void setStateName(String stateName)
{
this.stateName = stateName;
}
/**
*
* The getter method for capital city
*
* @return capitalCity
*
*/
public String getCapitalCity()
{
return capitalCity;
}
/**
*
* The setter method for capitalCity
*
* @param capitalCity
*
*/
public void setCapitalCity(String capitalCity)
{
this.capitalCity = capitalCity;
}
/**
*
* The getter method for state abbreviation
*
* @return stateAbbreviation
*
*/
public String getStateAbbreviation()
{
return stateAbbreviation;
}
/**
*
* The setter method for stateAbbreviation
*
* @param stateAbbreviation
*
*/
public void setStateAbbreviation(String stateAbbreviation)
{
this.stateAbbreviation = stateAbbreviation;
}
/**
*
* The getter method for state population
*
* @return statePopulation
*
*/
public int getStatePopulation()
{
return statePopulation;
}
/**
*
* The setter method for statePopulation
*
* @param statePopulation
*
*/
public void setStatePopulation(int statePopulation)
{
this.statePopulation = statePopulation;
}
/**
*
* The getter method for region
*
* @return region
*
*/
public String getRegion()
{
return region;
}
/**
*
* The setter method for region
*
* @param region
*
*/
public void setRegion(String region)
{
this.region = region;
}
/**
*
* The getter method for the US house seats
*
* @return houseSeats
*
*/
public int getHouseSeats()
{
return houseSeats;
}
/**
*
* The setter method for the US house seats
*
* @param houseSeats
*
*/
public void setHouseSeats(int houseSeats)
{
this.houseSeats = houseSeats;
}
/**
*
* This method display the members of this class in formatted way
*
*/
public void display()
{
System.out.println("State Name: " + stateName);
System.out.println("Capital City: " + capitalCity);
System.out.println("State Abbr: " + stateAbbreviation);
System.out.println("State Population: " + statePopulation);
System.out.println("Region: " + region);
System.out.println("US House Seats: " + houseSeats);
}
@Override
public String toString()
{
return stateName + " " + capitalCity + " " + stateAbbreviation + " " + statePopulation + " " + region + " " + houseSeats;
}
public static Comparator<State> stateNameComparator = new Comparator<State>()
{
public int compare(State s1, State s2)
{
String stateName1 = s1.getStateName().toUpperCase();
String stateName2 = s2.getStateName().toUpperCase();
//ascending order
return stateName1.compareTo(stateName2);
}
}
}
And the State2..csv file to be read from
State,Capital,Abbreviation,Population,Region,US House Seats
Alabama,Montgomery,AL,4833722,South,7
Alaska,Juno,AK,735132,West,1
Arizona,Phoenix,AZ,6626624,Southwest,9
Arkansas,Little Rock,AR,2959373,South,4
California,Sacramento,CA,38332521,West,53
Colorado,Denver,CO,5268367,West,7
Connecticut,Hartford,CT,3596080,New England,5
Delaware,Dover,DE,925749,Middle Atlantic,1
Florida,Tallahassee,FL,19552860,South,27
Georgia,Atlanta,GA,9992167,South,14
Hawaii,Honolulu,HI,1404054,West,2
Idaho,Boise,ID,1612136,West,2
Illinois,Springfield,IL,12882135,Midwest,18
Indiana,Indianapolis,IN,6570902,Midwest,9
Iowa,Des Moines,IA,3090416,Midwest,4
Kansas,Topeka,KS,2893957,Midwest,4
Kentucky,Frankfort,KY,4395295,South,6
Louisiana,Baton Rouge,LA,4625470,South,6
Maine,Augusta,ME,1328302,New England,2
Maryland,Annapolis,MD,5928814,Middle Atlantic,8
Massachusetts,Boston,MA,6692824,New England,9
Michigan,Lansing,MI,9895622,Midwest,14
Minnesota,St Paul,MN,5420380,Midwest,8
Mississippi,Jackson,MS,2991207,South,4
Missouri,Jefferson City,MO,6044171,Midwest,8
Montana,Helena,MT,1015165,West,1
Nebraska,Lincoln,NE,1868516,Midwest,3
Nevada,Carson City,NV,2790136,West,4
New Hampshire,Concord,NH,1323459,New England,2
New Jersey,Trenton,NJ,8899339,Middle Atlantic,12
New Mexico,Santa Fe,NM,2085287,Southwest,3
New York,Albany,NY,19651127,Middle Atlantic,27
North Carolina,Raleigh,NC,9848060,South,13
North Dakota,Bismarck,ND,723393,Midwest,1
Ohio,Columbus,OH,11570808,Midwest,16
Oklahoma,Oklahoma City,OK,3850568,Southwest,5
Oregon,Salem,OR,3930065,West,5
Pennsylvania,Harrisburg,PA,12773801,Middle Atlantic,18
Rhode Island,Providence,RI,1051511,New England,2
South Carolina,Columbia,SC,4774839,South,7
South Dakota,Pierre,SD,844877,Midwest,1
Tennessee,Nashville,TN,6495978,South,9
Texas,Austin,TX,26448193,Southwest,36
Utah,Salt Lake City,UT,2900872,West,4
Vermont,Montpelier,VT,626630,New England,1
Virginia,Richmond,VA,8260405,Middle Atlantic,11
Washington,Olympia,WA,6971406,West,10
West Virginia,Charleston,WV,1854304,Middle Atlantic,3
Wisconsin,Madison,WI,5742713,Midwest,8
Wyoming,Cheyenne,WY,582658,West,1
Now, here the Requirements for the project:
COP3538 Project 2 – Stacks and Priority Queues
1. Reuse your State class from Project 1 (correct it if necessary)
2. Create a class named Stack that will implement a stack of state objects using an array.
Support the following methods.
a. Constructor that creates the stack array based on a size provided.
b. A push method to push a state on the stack
c. A pop method to pop a state off the stack and return it.
d. A printStack method to print the stack, see the example.
e. An isEmpty method that returns true if the stack is empty, false otherwise
f. An isFull method that returns true if the stack is full, false otherwise
3. Create a class named Priority that implements a priority queue of state objects using an
array, based on population, highest population is the highest priority. Support the following
methods:
a. Constructor that creates the stack array based on a size provided.
b. An insert method to insert a state into the queue.
c. A remove method to remove a state from the front of the queue and return it.
d. A printQueue method to print the queue in priority order, see the example.
e. An isEmpty method that returns true if the queue is empty, false otherwise
f. An isFull method that returns true if the queue is full, false otherwise
4. Create a class named Project2 that will:
a. Read the csv file (States2.csv) of states and create a single stack of state objects
containing states from regions South, West, and Midwest that data (discard any
states not in those regions, do not modify the input file.).
b. Print the stack starting with the top of the stack.
c. Create three priority queues, one for each region.
d. Pop states off the stack one at a time and insert them into the appropriate priority
queue until the stack is empty.
e. Print all three priority queues in this order: South, West, then Midwest.
f. Remove items from the South priority queue, one at a time, and push them on the
stack.
g. Remove items from the West priority queue, one at a time, and push them on the
stack.
h. Remove items from the Midwest priority queue, one at a time, and push them on the
stack.
Print the stack, then exit the program
The queues and the stack should be printed in the following format:
Stack Contents: or South Priority Queue Contents:
State Name Capital City State Abbr State Population Region US House Seats
-------------------------------------------------------------------------------------------------------
Florida Tallahassee FL 19,552,860 South 27
Arkansas Little Rock AR 2,959,373 South 4
Alabama Montgomery AL 4,833,722 South 7
Provide comments in this form for the State, Stack, and Priority classes:
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 Project2 class.
/**
* COP 3538: Project 2 – Stacks and Priority Queues
* <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 Project2
{
Example:
COP3538 Project 2
Instructor: Xudong Liu
Stacks and Priority Queues
Enter the file name: States2.csv
There were 33 state records put on the stack.
Stack Contents:
State Name Capital City State Abbr State Population Region US House Seats
-------------------------------------------------------------------------------------------------------
Wyoming Cheyenne WY 582,658 West 1
Wisconsin Madison WI 5,742,713 Midwest 8
Washington Olympia WA 6,971,406 West 10
. . .
South Priority Queue Contents:
State Name Capital City State Abbr State Population Region US House Seats
-------------------------------------------------------------------------------------------------------
Florida Tallahassee FL 19,552,860 South 27
Georgia Atlanta GA 9,992,167 South 14
. . .
West Priority Queue Contents:
State Name Capital City State Abbr State Population Region US House Seats
-------------------------------------------------------------------------------------------------------
California Sacramento CA 38,332,521 West 53
Washington Olympia WA 6,971,406 West 10
. . .
Midwest Priority Queue Contents:
State Name Capital City State Abbr State Population Region US House Seats
-------------------------------------------------------------------------------------------------------
Illinois Springfield IL 12,882,135 Midwest 18
Ohio Columbus OH 11,570,808 Midwest 16
. . .
Stack Contents:
State Name Capital City State Abbr State Population Region US House Seats
-------------------------------------------------------------------------------------------------------
North Dakota Bismarck ND 723,393 Midwest 1
South Dakota Pierre SD 844,877 Midwest 1
. . .
Explanation / Answer
Given below are the needed classes and output. Post a comment if any issues. If the answer helped , pelase rate it . Thank you
Stack.java
public class Stack{
private State[] states;
private int current;
public Stack(int size)
{
states = new State[size];
current = 0;
}
public void push(State s)
{
if(!isFull())
states[current++] = s;
}
public State pop()
{
if(!isEmpty())
return states[--current];
else
return null;
}
public boolean isEmpty()
{
return current == 0;
}
public boolean isFull()
{
return current == states.length;
}
public int size()
{
return current;
}
public void printStack()
{
System.out.println("State Name Capital City State Abbr State Population Region US House Seats");
System.out.println("-------------------------------------------------------------------------------------------------------");
for(int i = current - 1; i >= 0; i--)
System.out.println(states[i]);
}
}
Priority.java
public class Priority {
private State[] states;
private int current;
public Priority(int size)
{
states = new State[size];
current = 0;
}
public void insert(State s)
{
if(!isFull())
{
//find the correct place where to insert new state
int insertPoint = 0;
while(insertPoint < current)
{
if(s.getStatePopulation() <= states[insertPoint].getStatePopulation())
insertPoint++;
else
break;
}
//since we want ot insert at insertPoint, move all elements starting from
//insertPoint to end , one position right
for(int i = current - 1; i >= insertPoint; i--)
states[i+1] = states[i];
//now insert the new state
states[insertPoint] = s;
current++;
}
}
public State remove()
{
State val = null;
if(!isEmpty())
{
val = states[0]; //get the 1st element
//move all others one place left
for(int i = 1; i < current; i++)
states[i-1] = states[i];
current--;
}
return val;
}
public boolean isEmpty()
{
return current == 0;
}
public boolean isFull()
{
return current == states.length;
}
public int size()
{
return current;
}
public void printQueue()
{
System.out.println("State Name Capital City State Abbr State Population Region US House Seats");
System.out.println("-------------------------------------------------------------------------------------------------------");
for(int i = 0; i < current; i++)
System.out.println(states[i]);
}
}
Project2.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Project2 {
private static State createState(String line)
{
String tokens[] = line.split(",");
State s = new State();
s.setStateName(tokens[0]);
s.setCapitalCity(tokens[1]);
s.setStateAbbreviation(tokens[2]);
s.setStatePopulation(Integer.parseInt(tokens[3]));
s.setRegion(tokens[4]);
s.setHouseSeats(Integer.parseInt(tokens[5]));
return s;
}
public static void main(String[] args) {
Scanner keybd = new Scanner(System.in);
String filename;
System.out.println("Stacks and Priority Queues");
System.out.print("Enter the file name: ");
filename = keybd.nextLine().trim();
Stack stk = new Stack(60);
Priority south = new Priority(50);
Priority west = new Priority(50);
Priority midwest = new Priority(50);
try {
Scanner inFile = new Scanner(new File(filename));
inFile.nextLine(); //skip the 1st line of column heading
//push south west and midwest states on stack
while(inFile.hasNextLine())
{
String line = inFile.nextLine().trim();
if(line.isEmpty())
continue;
State s = createState(line);
//process only states from south , west and midwest
if(s.getRegion().equalsIgnoreCase("South") ||
s.getRegion().equalsIgnoreCase("West") ||
s.getRegion().equalsIgnoreCase("Midwest"))
stk.push(s);
}
inFile.close();
//display stack
System.out.println("There were " + stk.size() + " state records put on stack");
System.out.println("Stack contents:");
stk.printStack();
//pop from stack and add to queues
while(!stk.isEmpty())
{
State s = stk.pop();
if(s.getRegion().equalsIgnoreCase("South"))
south.insert(s);
else if(s.getRegion().equalsIgnoreCase("West"))
west.insert(s);
else if(s.getRegion().equalsIgnoreCase("Midwest"))
midwest.insert(s);
}
System.out.println(" South Priority Queue contents");
south.printQueue();
System.out.println(" West Priority Queue contents");
west.printQueue();
System.out.println(" Midwest Priority Queue contents");
midwest.printQueue();
//remove from south queue and put on stack
while(!south.isEmpty())
stk.push(south.remove());
//remove from west queue and put on stack
while(!west.isEmpty())
stk.push(west.remove());
//remove from midwest queue and put on stack
while(!midwest.isEmpty())
stk.push(midwest.remove());
System.out.println("Stack contents:");
stk.printStack();
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
}
Output:
Stacks and Priority Queues
Enter the file name: States2.csv
There were 33 state records put on stack
Stack contents:
State Name Capital City State Abbr State Population Region US House Seats
-------------------------------------------------------------------------------------------------------
Wyoming Cheyenne WY 582658 West 1
Wisconsin Madison WI 5742713 Midwest 8
Washington Olympia WA 6971406 West 10
Utah Salt Lake City UT 2900872 West 4
Tennessee Nashville TN 6495978 South 9
South Dakota Pierre SD 844877 Midwest 1
South Carolina Columbia SC 4774839 South 7
Oregon Salem OR 3930065 West 5
Ohio Columbus OH 11570808 Midwest 16
North Dakota Bismarck ND 723393 Midwest 1
North Carolina Raleigh NC 9848060 South 13
Nevada Carson City NV 2790136 West 4
Nebraska Lincoln NE 1868516 Midwest 3
Montana Helena MT 1015165 West 1
Missouri Jefferson City MO 6044171 Midwest 8
Mississippi Jackson MS 2991207 South 4
Minnesota St Paul MN 5420380 Midwest 8
Michigan Lansing MI 9895622 Midwest 14
Louisiana Baton Rouge LA 4625470 South 6
Kentucky Frankfort KY 4395295 South 6
Kansas Topeka KS 2893957 Midwest 4
Iowa Des Moines IA 3090416 Midwest 4
Indiana Indianapolis IN 6570902 Midwest 9
Illinois Springfield IL 12882135 Midwest 18
Idaho Boise ID 1612136 West 2
Hawaii Honolulu HI 1404054 West 2
Georgia Atlanta GA 9992167 South 14
Florida Tallahassee FL 19552860 South 27
Colorado Denver CO 5268367 West 7
California Sacramento CA 38332521 West 53
Arkansas Little Rock AR 2959373 South 4
Alaska Juno AK 735132 West 1
Alabama Montgomery AL 4833722 South 7
South Priority Queue contents
State Name Capital City State Abbr State Population Region US House Seats
-------------------------------------------------------------------------------------------------------
Florida Tallahassee FL 19552860 South 27
Georgia Atlanta GA 9992167 South 14
North Carolina Raleigh NC 9848060 South 13
Tennessee Nashville TN 6495978 South 9
Alabama Montgomery AL 4833722 South 7
South Carolina Columbia SC 4774839 South 7
Louisiana Baton Rouge LA 4625470 South 6
Kentucky Frankfort KY 4395295 South 6
Mississippi Jackson MS 2991207 South 4
Arkansas Little Rock AR 2959373 South 4
West Priority Queue contents
State Name Capital City State Abbr State Population Region US House Seats
-------------------------------------------------------------------------------------------------------
California Sacramento CA 38332521 West 53
Washington Olympia WA 6971406 West 10
Colorado Denver CO 5268367 West 7
Oregon Salem OR 3930065 West 5
Utah Salt Lake City UT 2900872 West 4
Nevada Carson City NV 2790136 West 4
Idaho Boise ID 1612136 West 2
Hawaii Honolulu HI 1404054 West 2
Montana Helena MT 1015165 West 1
Alaska Juno AK 735132 West 1
Wyoming Cheyenne WY 582658 West 1
Midwest Priority Queue contents
State Name Capital City State Abbr State Population Region US House Seats
-------------------------------------------------------------------------------------------------------
Illinois Springfield IL 12882135 Midwest 18
Ohio Columbus OH 11570808 Midwest 16
Michigan Lansing MI 9895622 Midwest 14
Indiana Indianapolis IN 6570902 Midwest 9
Missouri Jefferson City MO 6044171 Midwest 8
Wisconsin Madison WI 5742713 Midwest 8
Minnesota St Paul MN 5420380 Midwest 8
Iowa Des Moines IA 3090416 Midwest 4
Kansas Topeka KS 2893957 Midwest 4
Nebraska Lincoln NE 1868516 Midwest 3
South Dakota Pierre SD 844877 Midwest 1
North Dakota Bismarck ND 723393 Midwest 1
Stack contents:
State Name Capital City State Abbr State Population Region US House Seats
-------------------------------------------------------------------------------------------------------
North Dakota Bismarck ND 723393 Midwest 1
South Dakota Pierre SD 844877 Midwest 1
Nebraska Lincoln NE 1868516 Midwest 3
Kansas Topeka KS 2893957 Midwest 4
Iowa Des Moines IA 3090416 Midwest 4
Minnesota St Paul MN 5420380 Midwest 8
Wisconsin Madison WI 5742713 Midwest 8
Missouri Jefferson City MO 6044171 Midwest 8
Indiana Indianapolis IN 6570902 Midwest 9
Michigan Lansing MI 9895622 Midwest 14
Ohio Columbus OH 11570808 Midwest 16
Illinois Springfield IL 12882135 Midwest 18
Wyoming Cheyenne WY 582658 West 1
Alaska Juno AK 735132 West 1
Montana Helena MT 1015165 West 1
Hawaii Honolulu HI 1404054 West 2
Idaho Boise ID 1612136 West 2
Nevada Carson City NV 2790136 West 4
Utah Salt Lake City UT 2900872 West 4
Oregon Salem OR 3930065 West 5
Colorado Denver CO 5268367 West 7
Washington Olympia WA 6971406 West 10
California Sacramento CA 38332521 West 53
Arkansas Little Rock AR 2959373 South 4
Mississippi Jackson MS 2991207 South 4
Kentucky Frankfort KY 4395295 South 6
Louisiana Baton Rouge LA 4625470 South 6
South Carolina Columbia SC 4774839 South 7
Alabama Montgomery AL 4833722 South 7
Tennessee Nashville TN 6495978 South 9
North Carolina Raleigh NC 9848060 South 13
Georgia Atlanta GA 9992167 South 14
Florida Tallahassee FL 19552860 South 27