Question: need help with setting up the readStates ( ) and ReadElections method. readElections ( String file ) This method reads over the same CSV files

need help with setting up the readStates() and ReadElections method. readElections(String file)
This method reads over the same CSV files as before. But now we use all the data, and insert ElectionNodes inside the correct StateNodes. Each line in the file is a single candidate in a specific election.
Each StateNode contains an ElectionNode reference "elections", which is the head of a Singularly Linked List of elections. You will insert these ElectionNodes for each unique racelD, and then insert candidates.
You can use the following to read and fully parse the line:
```
String line = StdIn.readLins();
String[] split = line.split(",");
int raceID = Integor_parseInt(split[0]);
String statekane = split[1];
int officeID = Integor__parseInt(split[2]);
boolean senate = split[[3]-@quals("Y.S. Serate");
int year = Integer_parseInt(split[4]);
String cantane = split[5];
String party = split[6];
int votes = Integer.parseLnt(split[7]);
boolean wimer = split[8]-tolomerCase()-equals("trwe");
```
To complete this method:
1) Search the "years" list for the read-in year.
2) IN that year, search that YearNode's "states" CLL for the correct state.
- Reference the previbus methods for the above steps.
3) In that state, search the state's "elections" ElectionNode SLL for the read-in election's raceID. See below for more on inserting into the Elections list. Here is an example of the driver for Hawail in the year 2000. Bear in mind that you should thirk of other best cases that may exercise all parts of your insertion algorithrn.
- How to lnsert in the elections list
Our goal is to insert a new BectionNode in the singularly Iiriked list of elections (within the carrect StateNade, in the correct YearNade). Rerrember, each StateNode in a states list have a field called 'elections' which is a reference to the front of this SLL.
First. check if that elections list is empty (aka if the 'elections' field within that StateNode is null). If it is, create a new ElectionNodes) and set its information with the info you read from the CSV line, and add the read-in candidate to that election node with addCandidate(). Then, set the containing StateNodes 'elections' field to that new ElectionNode. Then you're done, so cantinue onto the next CSV line.
If the SLL is not empty, then we want to check if an ElectionNode containing the read-in election already exists, as we do not want duplicates in the fist.
- To traverse the list, declare a ElectionNode variable and set it to the state.get Elections(). for the corresponding state in the right year. This grabs a secondary reference to the front of the elections list.
- Traverse the fist using a while loop, similar to traversing the years list. The condition should check if the (ptr.getNext()== nul OR ptr.getRacelD()\(==\) your read-in racelD). This makes the loop bresk at either the duplicate election or the end of the list.
- Afer your traversal loop ends, check the value of ptr:
* If it's racelD is equal to the read-in racelD, you need to check if your read-in candidate has been inserted.
- Use election,isCandidate(CANDIDATE)(replace CANDIDATE with a String candidate name) to check if a specific candidate has already been added to that elections.
- If it has, modify that candidate with your new info with election.modifyCandidate(canName, vates, party).
- If it hasn't, add the candidate with the info with election.addCandidate(canName, vobes, party, isWirner)
- If its racedD is not equal to the read-in racelD, then it should be the at end of the fist. So ptr.getNext() should be null.
- Create a new ElectionNode with the read-in information. Make sure to set all the attributes for isSenate, officelD, racelD, and more.
- Insert that new node at the end of the list, by setting the 'next' feld of the last node in the list equal to your new node
- Add the read-in candidate to that election node with addCandidate() electian > J GectionAnclyais;jva >\&s ElactionAralysius >\(\Theta \) years
package election;
inpert java.tine.Year;
\({}^{\prime *}+\)
* Election Analysis class which parses past clection data for the housc/scnate
* in csv format, and implenents methods which can return infornation about candidates
and nationwide clection results.
* It stores the election data by year, state, then election using nested linked structurcs.
* The years field is a singly linked list of vearNodes.
* Each Yearkade has a statcs Circularly linked list of statcNades
* Each StateNode has its own singly linked list of ElecticnNodes, which are elections
* that occured in that state, in that ycar.
* This structure allows information about clecticns to be stored, by year and state.
*(author Colin Sullivan
public class ElectionAnalysis
// Reference to the front of the Years SLL
private YearWode years;
public YearNode years ()\(\{\)
\}
/* R \(\\)
* Read through the lines in the given clecticns csV file
* Loap Though lines with Stdin.hasNextLine()
* split each line with:
" String[] split - StdIn.readLine().split(",");
* Then access the Year Nane with split[4]
* For each year you read, search the yea ```
* states list (aka that years "states" reference wi
need help with setting up the readStates ( ) and

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!