[CSC 204] Help on Assignment 4
Andrew J. Pounds
pounds_aj at mercer.edu
Tue Nov 29 20:56:54 EST 2016
Here is some help for those of you struggling to get the array of states
built in Assignment 4.
/* At this point you should have processed all of your stations and have
them in an array. In the example below that array is called stations and
it is of type Station (which is defined in Station.java. Since you now
have all of the station ID's and the states that they are associated
with, you can use that data to build an array of type State. I'll give
you a head start... */ int numberOfStates=1; State[] stateData = new
State[numberOfStates]; stateData[0] = new State( stations[0].getState()
); String state; // I'm going to run through all of the stations in my
array for ( int i=1; i<stations.length; i++ ){ boolean found = false;
int j = 0; // Now I'm going to look through the stateData array and see
if the state // that is tied the index i in the station array is already
in the stateData array. // If it is then I set the boolean found to
true. while ( j<stateData.length && !found ) { if (
stations[i].getState().equals(stateData[j].getState())) { found = true;
} j++; } // If the state for index i in the stations array is already in
the stateData array // then I don't need to add it again. If it is NOT
found in the stateData array, // then I need to increase the size of my
array and add the new data to the end. // Look how similar this is to
the last thing you did in processing the stations! if (!found) { State[]
newstateData = new State[stateData.length+1]; for (int k=0;
k<stateData.length; k++ ) newstateData[k] = stateData[k];
newstateData[newstateData.length-1] = new State(stations[i].getState());
stateData = newstateData; } } // To make sure it is working, print out
the states for ( State s : stateData ) System.out.println(s.getState());
// Now - go fix your State.java file so it will work with what I have
done above! You need // to add the instance variables and methods (like
getState to return the two character // state abbreviation. // Once you
get this far you should be ready to start processing the daily.txt file.
--
Andrew J. Pounds, Ph.D. (pounds_aj at mercer.edu)
Professor of Chemistry and Computer Science
Mercer University, Macon, GA 31207 (478) 301-5627
http://faculty.mercer.edu/pounds_aj
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://theochem.mercer.edu/pipermail/csc204/attachments/20161129/860800ee/attachment.html>
More information about the csc204
mailing list