📄 ch11.htm
字号:
private static synchronized void initializeTable(introws,String results[][]) {<BR> int i,j;<BR> StateEntry newState;<BR> // Create the state array...<BR> states = new StateEntry[rows/2];<BR> // Go through each row<BR> for (i = j = 0; i < rows;i+=2,++j) {<BR> newState= createStateEntry(i,results);<BR> // Now addto state array...<BR> states[j]= newState;<BR> } // end for<BR> }<BR><BR> // Update just parts of the table...<BR> private void partialUpdate(int rows,String results[][]){<BR> int i,j;<BR> StateEntry newState;<BR> // Kick out if rows is nota multiple of 2<BR> if ((rows % 2) != 0) {<BR> System.out.println("Datanot formatted right. Rejected.");<BR> return;<BR> }<BR> // Go through each row<BR> for (i = j = 0; i < rows;i+=2,++j) {<BR> // Createnew state table...<BR> newState= createStateEntry(i,results);<BR> // Now updatestate array...<BR> stateUpdate(newState);<BR> } // end for<BR> }<BR><BR> // Take index into results table and get a StateEntry<BR> // object from it...<BR>private static StateEntry createStateEntry(int index,String results[][]){<BR> Candidate newChallenger;<BR> Candidate newIncumbent;<BR> String stateName;<BR> double precincts = 0.0;<BR> int electoral = 0;<BR> int candElectoral = 0;<BR> int IncumbentVotes = 0;<BR> int ChallengerVotes = 0;<BR> int i = index;<BR> String s;<BR> // Firstrow is Challenger. Get his and state info...<BR> s = results[i][1];<BR> stateName= s.trim();<BR> // Get precinct,electoral,votes...<BR> try {<BR> s= results[i][2];<BR> ChallengerVotes= Integer.valueOf(<BR> s.trim()).intValue();<BR> s= results[i][3];<BR> precincts= Double.valueOf(s.trim()).doubleValue();<BR> s= results[i][4];<BR> electoral= Math.abs(Integer.valueOf(s.trim()).intValue());<BR> candElectoral= Integer.valueOf(s.trim()).intValue();<BR> if(candElectoral < 0)<BR> candElectoral= 0;<BR> }<BR> catch (NumberFormatExceptione) {<BR> System.out.println("Formaterror: " + e.getMessage());<BR> }<BR> s = results[i][0];<BR> newChallenger= new Candidate(s.trim(),<BR> candElectoral,ChallengerVotes);<BR> // Now getIncumbent info...<BR> ++i;<BR> try {<BR> s= results[i][2];<BR> IncumbentVotes= Integer.valueOf(s.trim()).intValue();<BR> s= results[i][4];<BR> candElectoral= Integer.valueOf(s.trim()).intValue();<BR> if(candElectoral < 0)<BR> candElectoral= 0;<BR> }<BR> catch (NumberFormatExceptione) {<BR> System.out.println("Formaterror: " + e.getMessage());<BR> }<BR> s = results[i][0];<BR> newIncumbent= new Candidate(s.trim(),<BR> candElectoral,IncumbentVotes);<BR> // Returnstate entry field...<BR> return newStateEntry(stateName,precincts,<BR> electoral,(IncumbentVotes+ ChallengerVotes),<BR> newIncumbent,newChallenger);<BR> }<BR><BR> // Get table of state listings. This isa copy of the<BR> // state entry TABLE, but not the individual listings. This<BR> // solves synchronization problems. Theuser of the table<BR> // will get the references to the states but doesnot actually<BR> // have references to the actual keys used by theElectionTable<BR> // class.<BR> public synchronized StateEntry[] getStates() {<BR> if (states == null)<BR> returnstates;<BR> StateEntry tempStates[] =new StateEntry[states.length];<BR> for (int i = 0; i < states.length;++i)<BR> tempStates[i]= states[i];<BR> return tempStates;<BR> }<BR><BR> // Get candidate total information...<BR> public synchronized Candidate getIncumbent() {<BR> return Incumbent;<BR> }<BR> public synchronized Candidate getChallenger() {<BR> return Challenger;<BR> }<BR><BR> // Update the totals for each Candidate...<BR> public synchronized void updateTotals() {<BR> int ChallengerPopular = 0;<BR> int ChallengerElectoral =0;<BR> int IncumbentPopular = 0;<BR> int IncumbentElectoral = 0;<BR> totalPopular = 0;<BR> // Calculate the totals...<BR> for (int i = 0; i < states.length;++i) {<BR> ChallengerPopular+= states[i].getChallenger().getPopular();<BR> ChallengerElectoral+= states[i].getChallenger().getElectoral();<BR> IncumbentPopular+= states[i].getIncumbent().getPopular();<BR> IncumbentElectoral+= states[i].getIncumbent().getElectoral();<BR> totalPopular+= states[i].getTotalVotes();<BR> } // end for<BR> // Update the Candidates...<BR> Incumbent = new Candidate("Incumbent",IncumbentElectoral,IncumbentPopular);<BR> Challenger = new Candidate("Challenger",ChallengerElectoral,</TT><FONT FACE="ZapfDingbats">Â</FONT><TT>ChallengerPopular);<BR> }<BR><BR> // Take a state field and find spot in state<BR> // table to update...<BR> public synchronized void stateUpdate(StateEntry newState){<BR> String name = newState.getName();<BR> for (int i = 0; i < states.length;++i) {<BR> // Replacestate that matches current entry...<BR> if (name.equals(states[i].getName())){<BR> states[i]= newState;<BR> return;<BR> }<BR> } // end for<BR>System.out.println("STATE UPDATE ERROR!");<BR> }<BR><BR> // Get total popular vote...<BR> public synchronized int getTotalPopular() {<BR> return totalPopular;<BR> }<BR><BR> // Get info for a specific state...<BR> private synchronized StateEntry getState(String name){<BR> for (int i = 0; i < states.length;++i) {<BR> // Replacestate that matches current entry...<BR> if (name.equals(states[i].getName())){<BR> returnstates[i];<BR> }<BR> } // end for<BR>System.out.println("STATE GET ERROR!");<BR> return null;<BR> }<BR>}</TT></BLOCKQUOTE><HR><H4>The Candidate Class</H4><P>Listing 11.4 displays the Candidate class, which contains thename and vote totals of a specific candidate. Although it's asimple accessor class, it effectively functions as a row in avirtual table of candidates.<HR><BLOCKQUOTE><B>Listing 11.4. The Candidate class.<BR></B></BLOCKQUOTE><BLOCKQUOTE><TT>// This is a simple accessor class tokeep information<BR>// about candidates...<BR>public class Candidate {<BR> String Name; // Name of candidate<BR> int to
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -