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. readElectionsString 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 IntegorparseIntsplit;
String statekane split;
int officeID IntegorparseIntsplit;
boolean senate split@qualsYS Serate";
int year IntegerparseIntsplit;
String cantane split;
String party split;
int votes Integer.parseLntsplit;
boolean wimer splittolomerCaseequalstrwe;
To complete this method:
Search the "years" list for the readin year.
IN that year, search that YearNode's "states" CLL for the correct state.
Reference the previbus methods for the above steps.
In that state, search the state's "elections" ElectionNode SLL for the readin 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 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 readin 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 readin 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 ptrgetNext nul OR ptrgetRacelD your readin 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 readin racelD, you need to check if your readin candidate has been inserted.
Use election,isCandidateCANDIDATEreplace 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.modifyCandidatecanName vates, party
If it hasn't, add the candidate with the info with election.addCandidatecanName vobes, party, isWirner
If its racedD is not equal to the readin racelD, then it should be the at end of the fist. So ptrgetNext should be null.
Create a new ElectionNode with the readin 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 readin 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 houscscnate
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.readLinesplit;
Then access the Year Nane with split
For each year you read, search the yea
states list aka that years "states" reference wi
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
