Thank you.
- Manny
*Program Purpose: This program processes the results for the 2008 Democratic and/or Republican presidential
primary elections.
Expand|Select|Wrap|Line Numbers
- import staugIO.StaugIO;
- public class Primaries2008{
- //DECLARE PRIVATE VARIABLES****************************************************************************************************
- private int choice, count1 , count2, count3, count4, totalCount, whichPrimary,
- democraticPrimaryCount, republicanPrimaryCount;
- private double candidate1, candidate2, candidate3, candidate4;
- private char answer;
- private char anotherVoter = 'V';
- private boolean anotherPrimary = false;
- private String tab5 = " ";
- private String tab3 = " ";
- private String democraticResults = "2008 Democratic Primary Preliminary Results";
- private String republicanResults = "2008 Republican Primary Preliminary Results";
- private StaugIO io;
- private DecimalFormat percent = new DecimalFormatter("##0%");
- //FIRST LEVEL OF REFINEMENT METHODS*********************************************************************************************
- public void beginPrimary()
- {
- do
- {
- int totalCount = 0;
- int count1 = 0;
- int count2 = 0;
- int count3 = 0;
- int count4 = 0;
- double candidate1 = 0.0;
- double candidate2 = 0.0;
- double candidate3 = 0.0;
- double candidate4 = 0.0;
- do{
- private void selectPrimary()
- {//BEGIN selectPrimary
- if((whichPrimary <= 0 ) || (whichPrimary > 2));
- {
- io.writeInfo("Invalid primary choice!");
- }
- else{
- switch(whichPrimary)
- {
- case 1: ++democraticPrimaryCount; break;
- case 2: ++republicanPrimaryCount; break;
- }
- }//END IF
- }//END selectPrimary
- if((democraticPrimaryCount == 1) && (republicanPrimaryCount == 1));
- {
- anotherPrimary = false;
- }
- while ((whichPrimary <> 1 ) && (whichPrimary <> 2))
- do{
- if(whichPrimary == 1);
- {//BEGIN IF
- private democraticPrimary()
- {//BEGIN democraticPrimary
- switch(choice)
- {//BEGIN switch
- case 1: io.writeInfo("1. Hillary Clinton"); break;
- case 2: io.writeInfo("2. Barack Obama"); break;
- case 3: io.writeInfo("3. John Edwards"); break;
- case 4: tallyVote();break;{
- }
- default: io.writeInfo("Invalid candidate choice!"); break;
- anotherVoter = 'v';
- }//END Switch
- }//END democraticPrimary
- }//END IF
- if((choice > 0) && (choice < 5));
- {
- answer = io.writeInfo("Thank you! Your vote has been recorded.\n\n");
- io.writeInfo("Election Official:\n\n");
- io.writeInfo("To reset for another primary enter 'P'.");
- io.writeInfo("\nTo exit and get preliminary primary results enter 'E'.");
- }
- if((answer == 'v') || (answer == 'V'))
- {
- anotherVoter == answer;
- }
- else
- if((answer == 'p') || (answer == 'P'))
- {
- anotherVoter = 'e';
- anotherPrimary = false;
- }
- while((anotherVoter == 'v') || (anotherVoter == 'V'));
- }//END DO
- if(whichPrimary == 1)
- {
- democraticPrimaryWinner();
- }
- else
- {
- republicanPrimaryWinner();
- }
- while(anotherPrimary = true)
- }//END DO
- if((democraticPrimaryCount == 1) && (republicanPrimaryCount == 1))
- {
- io.writeInfo("\nTwo primaries have already been processed.");
- io.writeInfo("\n\nPrimary election results will follow!");
- io.writeInfo(democraticResults);
- io.writeInfo(republicanResults);
- else
- if(whichPrimary == 1)
- {
- io.writeInfo(democraticResults);
- else
- io.writeInfo(republicanResults);
- }
- }
- }
- }
- }//RETURN
- //SECOND LEVEL OF REFINEMENT*******************************************************************************
- private selectPrimary()
- {//BEGIN selectPrimary
- whichPrimary = io.writeInfo("Election Official: Which primary election are you setting for? ");
- io.writeInfo("\n\n1. Democratic Primary");
- io.writeInfo("\n2. Republican Primary");
- }//RETURN selectPrimary
- private democraticPrimary()
- {//BEGIN democraticPrimary
- choice = io.writeInfo("ELECTION 2008 REPUBLICAN PRIMARY\n\n");
- io.writeInfo("1. Hillary Clinton\n");
- io.writeInfo("2. Barack Obama\n");
- io.writeInfo("3. John Endwards\n\n");
- io.writeInfo("Enter the number for the presidential candidate of your choice: \n\n");
- }//RETRUN democraticPrimary
- private republicanPrimary()
- {
- choice = io.writeInfo("ELECTION 2008 REPUBLICAN PRIMARY\n\n");
- io.writeInfo("1. Rudy Giuliani\n");
- io.writeInfo("2. Fred Thompson\n");
- io.writeInfo("3. Mitt Romney\n");
- io.writeInfo("4. John McCain\n\n");
- io.writeInfo("Enter the number for the presidential candidate of your choice: \n\n")
- }//RETURN republicanPrimary
- private tallyVote()
- {//BEGIN tallyVote
- switch(choice)
- {
- case 1: ++count1;break;
- case 2: ++count2;break;
- case 3: ++count3;break;
- case 4: ++count4;break;
- }
- ++totalCount;
- }//RETURN tallyVote
- private democraticPrimaryWinner()
- {
- candidate1 = (double)(count1)/totalCount;
- candidate2 = (double)(count2)/totalCount;
- candidate3 = (double)(count3)/totalCount;
- democraticResults += ("\n\n1. Hillary clinton" + tab5 + tab5 + percent.format(candidate1)
- + "\n2. Barack Obama " + tab5 + percent.format(candidate2)
- + "\n3. John Edwards " + tab5 + percent.format(candidate3) + "\n\n");
- }//RETURN democraticPrimaryWinner
- private republicanPrimaryWinner()
- {
- candidate1 = (double)(count1)/totalCount;
- candidate2 = (double)(count2)/totalCount;
- candidate3 = (double)(count3)/totalCount;
- candidate4 = (double)(count4)/totalCount;
- republicanResults += ("\n\n1. Fred Thompson" + tab5 + tab5 + percent.format(candidate2)
- + "\n2. Rudy Giuliani" + tab5 + percent.format(candidate1)
- + "\n3. Mitt Romney " + tab5 + percent.format(candidate3) + "\n\n"
- + "\n4. John McCain " + tab5 + tab5 percent.format(candidate4) + "\n\n");
- }//RETURN republicanPrimaryWinner
- }//RETURN beingPrimary
- }//END class PRIMARIES2008
- public static void main(String[] args) {
- }