<p><strong>Requirements</strong></p> <ul> <li>Begin with the completed code from
ID: 3640628 • Letter: #
Question
<p><strong>Requirements</strong></p><ul>
<li>Begin with the completed code from the voting example</li>
<li>Modify the program so that the total votes cast per state are shown (i.e., add a column to each row summing the total votes cast for all candidates per state)</li>
<li>Modify the program so that the total votes per candidate are shown (i.e., add a last row showing total votes received for all three columns of votes)</li>
</ul>
<p>As a hint, realize that you may make it easier on yourself by declaring new arrays, though most likely these can be 1D arrays to accomplish the needed task.</p>
<p> </p>
<p> </p>
<p>Code to be altered from voting example:</p>
<p>------------</p>
<p> </p>
<p>public static void main(String[] args) throws IOException</p>
<p>    {</p>
<p>        // TODO code application logic here</p>
<p> </p>
<p>        File election = new File("voting_2008.txt");</p>
<p>        Scanner sc = new Scanner(election);</p>
<p> </p>
<p>        String[] states = new String[51];</p>
<p>        int[][] votes = new int[51][3];</p>
<p> </p>
<p>        for(int s=0; s<51; s++)</p>
<p>        {</p>
<p>            states[s] = sc.nextLine();</p>
<p>        }</p>
<p> </p>
<p>        for(int c=0; c<3; c++)</p>
<p>        {</p>
<p>            for(int s=0; s<51; s++)</p>
<p>                    {</p>
<p>                    votes[s][c] = sc.nextInt();   </p>
<p>                    }</p>
<p>        }</p>
<p>        Formatter fmt = new Formatter();</p>
<p>        fmt.format("%20s%12s%12s%12s", "State", "Obama", "McCain", "Other");</p>
<p>        System.out.println(fmt);</p>
<p>        for(int s=0; s<51; s++)</p>
<p>        {</p>
<p>            fmt = new Formatter();</p>
<p>            fmt.format("%20s", states[s]);</p>
<p>            System.out.print(fmt);</p>
<p>            for(int c=0; c<3; c++)</p>
<p>            {</p>
<p>                fmt = new Formatter();</p>
<p>                fmt.format("%12d", votes[s][c]);</p>
<p>                System.out.print(fmt);</p>
<p> </p>
<p>            }</p>
<p> </p>
<p>            System.out.println();</p>
<p> </p>
<p> </p>
<p> </p>
<p>            }</p>
<p>        }</p>
<p> -------------------------------------------------------------------</p>
<p>Voting_2008.txt file contents:  just copy and paste it into a .txt file to make sure the code works.</p>
<p>--------------------------------------------------------------------</p>
<p> </p>
<p> </p>
<p>Alabama</p>
<p>Alaska</p>
<p>Arizona</p>
<p>Arkansas</p>
<p>California</p>
<p>Colorado</p>
<p>Connecticut</p>
<p>Delaware</p>
<p>D. C.</p>
<p>Florida</p>
<p>Georgia</p>
<p>Hawaii</p>
<p>Idaho</p>
<p>Illinois</p>
<p>Indiana</p>
<p>Iowa</p>
<p>Kansas</p>
<p>Kentucky</p>
<p>Louisiana</p>
<p>Maine</p>
<p>Maryland</p>
<p>Massachusetts</p>
<p>Michigan</p>
<p>Minnesota</p>
<p>Mississippi</p>
<p>Missouri</p>
<p>Montana</p>
<p>Nebraska</p>
<p>Nevada</p>
<p>New Hampshire</p>
<p>New Jersey</p>
<p>New Mexico</p>
<p>New York</p>
<p>North Carolina</p>
<p>North Dakota</p>
<p>Ohio</p>
<p>Oklahoma</p>
<p>Oregon</p>
<p>Pennsylvania</p>
<p>Rhode Island</p>
<p>South Carolina</p>
<p>South Dakota</p>
<p>Tennessee</p>
<p>Texas</p>
<p>Utah</p>
<p>Vermont</p>
<p>Virginia</p>
<p>Washington</p>
<p>West Virginia</p>
<p>Wisconsin</p>
<p>Wyoming</p>
<p>813479</p>
<p>123594</p>
<p>1034707</p>
<p>422310</p>
<p>8274473</p>
<p>1288633</p>
<p>997773</p>
<p>255459</p>
<p>245800</p>
<p>4282367</p>
<p>1844123</p>
<p>325871</p>
<p>236440</p>
<p>3419348</p>
<p>1374039</p>
<p>828940</p>
<p>514765</p>
<p>751985</p>
<p>782989</p>
<p>421923</p>
<p>1629467</p>
<p>1904098</p>
<p>2872579</p>
<p>1573354</p>
<p>554662</p>
<p>1441911</p>
<p>232159</p>
<p>333319</p>
<p>533736</p>
<p>384826</p>
<p>2215422</p>
<p>472422</p>
<p>4804945</p>
<p>2142651</p>
<p>141403</p>
<p>2940044</p>
<p>502496</p>
<p>1037291</p>
<p>3276363</p>
<p>296571</p>
<p>862449</p>
<p>170924</p>
<p>1087437</p>
<p>3528633</p>
<p>327670</p>
<p>219262</p>
<p>1959532</p>
<p>1750848</p>
<p>303857</p>
<p>1677211</p>
<p>82868</p>
<p>1266546</p>
<p>193841</p>
<p>1230111</p>
<p>638017</p>
<p>5011781</p>
<p>1073629</p>
<p>629428</p>
<p>152374</p>
<p>17367</p>
<p>4046219</p>
<p>2048759</p>
<p>120566</p>
<p>403012</p>
<p>2031179</p>
<p>1345648</p>
<p>682379</p>
<p>699655</p>
<p>1048462</p>
<p>1148275</p>
<p>295273</p>
<p>959862</p>
<p>1108854</p>
<p>2048639</p>
<p>1275409</p>
<p>724597</p>
<p>1445814</p>
<p>243882</p>
<p>452979</p>
<p>412827</p>
<p>316534</p>
<p>1613207</p>
<p>346832</p>
<p>2752771</p>
<p>2128474</p>
<p>168887</p>
<p>2677820</p>
<p>960165</p>
<p>738475</p>
<p>2655885</p>
<p>165391</p>
<p>1034896</p>
<p>203054</p>
<p>1479178</p>
<p>4479328</p>
<p>596030</p>
<p>98974</p>
<p>1725005</p>
<p>1229216</p>
<p>397466</p>
<p>1262393</p>
<p>164958</p>
<p>19794</p>
<p>8762</p>
<p>39020</p>
<p>26290</p>
<p>291011</p>
<p>39200</p>
<p>19592</p>
<p>4783</p>
<p>2686</p>
<p>83275</p>
<p>39276</p>
<p>7131</p>
<p>19002</p>
<p>77828</p>
<p>35903</p>
<p>25804</p>
<p>23996</p>
<p>27140</p>
<p>29497</p>
<p>13967</p>
<p>42267</p>
<p>68117</p>
<p>88976</p>
<p>61606</p>
<p>10606</p>
<p>41224</p>
<p>16709</p>
<p>14983</p>
<p>21285</p>
<p>9610</p>
<p>48778</p>
<p>10904</p>
<p>83232</p>
<p>39664</p>
<p>7448</p>
<p>103951</p>
<p>0</p>
<p>52098</p>
<p>83228</p>
<p>9804</p>
<p>23624</p>
<p>7997</p>
<p>35367</p>
<p>79247</p>
<p>33781</p>
<p>6810</p>
<p>38723</p>
<p>73152</p>
<p>13545</p>
<p>43813</p>
<p>6832</p>
Explanation / Answer
My bad. I've fixed it now. Dont forget to rate it. import java.io.File; import java.io.IOException; import java.util.Formatter; import java.util.Scanner; public class Program { public static void main(String[] args) throws IOException { File election = new File ("voting_2008.txt"); Scanner sc = new Scanner(election); String[] states = new String[51]; int[][] votes = new int[51][3]; int sum; for(int s = 0; s < 51; s++) { states[s] = sc.nextLine(); } for(int c = 0; c < 3; c++) { for(int s = 0; s < 51; s++) { votes[s][c] = sc.nextInt(); } } int totalSum[] = new int[4]; for(int i=0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.