Will give you some practice with the syntax of using arrays

Will give you some practice with the syntax of using arrays. Note that this is very similar to Example 7.2 on pages 246-249 in the textbook. Suppose you are counting ballots in an election for Supreme (Yet Somehow Democratically Elected) Ruler of the Universe. Assume you have 1,000 candidates in all, each of whom is assigned a unique ID number from 0 to 999. Write a program that processes a set of ballots. The program should run by allowing the user to enter the ID number of the selected candidate on each ballot. The user should be able to do this for as many ballots as needed, until entering a negative sentinel value to exit. Upon exiting, display a list of the candidates\' ID number and their number of votes received, but only if the candidate received at least one vote. Include error checking to ensure that the user can\'t type in an ID number above 999. Below is an example of what your program might look like while running. Example program run for voting problem: Enter candidate\'s ID number (0-999, any negative number to exit): 123 Enter candidate\'s ID number (0-999, any negative number to exit): 0 Enter candidate\'s ID number (0-999, any negative number to exit): 123 Enter candidate\'s ID number (0-999, any negative number to exit): 5 Enter candidate\'s ID number (0-999, any negative number to exit): 662 Enter candidate\'s ID number (0-999, any negative number to exit): 2342 Invalid ID number! Enter candidate\'s ID number (0-999, any negative number to exit): 121 Enter candidate\'s ID number (0-999, any negative number to exit): 5 Enter candidate\'s ID number (0-999, any negative number to exit): 123 Enter candidate\'s ID number (0-999, any negative number to exit): 7 Enter candidate\'s ID number (0-999, any negative number to exit): 1000 Invalid ID number! Enter candidate\'s ID number (0-999, any negative number to exit): 12 Enter candidate\'s ID number (0-999, any negative number to exit): -1 Election Results: Candidate 0: 1 vote(s) Candidate 5: 2 vote(s) Candidate 7: 1 vote(s) Candidate 12: 1 vote(s) Candidate 121: 1 vote(s) Candidate 123: 3 vote(s) Candidate 662: 1 vote(s)

Solution

Election.java

import java.util.HashMap;

import java.util.Map;

public class Election

{

   Map<Integer,Integer> ballotVoteMap;

  

   public Election()

   {

       ballotVoteMap = new HashMap<Integer,Integer>();

   }

  

   public void voteBallotNumber(Integer ballotNumber)

   {

       if(ballotVoteMap.containsKey(ballotNumber))

       {

           int numberOfVotes = ballotVoteMap.get(ballotNumber).intValue();

           ballotVoteMap.put(ballotNumber, ++numberOfVotes);

       }

       else

       {

           ballotVoteMap.put(ballotNumber, 1);

       }

   }

  

   public void printResults()

   {

       for(Integer ballotNumber : ballotVoteMap.keySet())

       {

           int numberOfVotes = ballotVoteMap.get(ballotNumber).intValue();

           System.out.println(\"Candidate \" + ballotNumber + \": \" + numberOfVotes + \" vote(s)\");

       }      

   }

}

ElectionTester.java

import java.util.Scanner;

public class ElectionTester

{

   public static void main(String args[])

   {

       Election election = new Election();

       Scanner in = new Scanner(System.in);

       while(true)

       {

           System.out.println(\"Enter candidate\'s ID number (0-999, any negative number to exit):\");

           int ballotNumber = in.nextInt();

           if(ballotNumber < 0)

               break;

          

           if(ballotNumber < 999)

           {

               election.voteBallotNumber(ballotNumber);

           }          

       }

       election.printResults();

       in.close();

   }

}

 Will give you some practice with the syntax of using arrays. Note that this is very similar to Example 7.2 on pages 246-249 in the textbook. Suppose you are co
 Will give you some practice with the syntax of using arrays. Note that this is very similar to Example 7.2 on pages 246-249 in the textbook. Suppose you are co
 Will give you some practice with the syntax of using arrays. Note that this is very similar to Example 7.2 on pages 246-249 in the textbook. Suppose you are co

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site