Write a java program that processes a set of ballots Assume

Write a java program that processes a set of ballots. Assume you have 1000 canadidates in all, each of whom is assigned a unique ID number from 0 to 999. The program should run by allowing the user to enter the ID number of the selected candidte on each ballot. The user should do this for as many ballots as needed, until entering a negative sentinel value to exit. Upon exiting, display a list of candidates\' ID numbers and their number of votes recieved, but only if the candidate recieved at least one vote.

Include error checking to ensure that the user can\'t type in an ID number above 999.

Solution

// Ballots.java

import java.io.*;
import java.util.*;

class Ballots
{
public static void main(String args[])
{
   Scanner scan = new Scanner(System.in);
    int frequency[]=new int[1000]; //array for storing frequency of all digits
     
for(int i=0; i<1000; i++)
{
frequency[i]=0; //intializing the count of every digit with \'0\'
}

   while(true)
   {
   System.out.print(\"Enter the ID number(0 to 999) : \");
   int idNumber = scan.nextInt();
         
          if(idNumber < 0)
              break;

          else if (idNumber > 999)
              System.out.println(\"Invalid Input\ \");  
     
          else
       frequency[idNumber]++; //increasing the frequency of that digit.
   }

System.out.println(\"===============\"); //this is just for styling the look of the output
System.out.println(\"ID\\tVOTES\");
System.out.println(\"===============\");

for(int i=0; i<1000; i++)
{
if(frequency[i]!=0) //printing only those digits whose count is not \'0\'
System.out.println(i+\"\\t\"+frequency[i]);
}
}
}

/*
output:

Enter the ID number(0 to 999) : 345
Enter the ID number(0 to 999) : 443
Enter the ID number(0 to 999) : 12
Enter the ID number(0 to 999) : 34
Enter the ID number(0 to 999) : 56
Enter the ID number(0 to 999) : 34
Enter the ID number(0 to 999) : 56
Enter the ID number(0 to 999) : 556
Enter the ID number(0 to 999) : 56
Enter the ID number(0 to 999) : 33
Enter the ID number(0 to 999) : 23
Enter the ID number(0 to 999) : 90191
Invalid Input

Enter the ID number(0 to 999) : 999
Enter the ID number(0 to 999) : 445
Enter the ID number(0 to 999) : -1
===============
ID   VOTES
===============
12   1
23   1
33   1
34   2
56   3
345   1
443   1
445   1
556   1
999   1


*/

Write a java program that processes a set of ballots. Assume you have 1000 canadidates in all, each of whom is assigned a unique ID number from 0 to 999. The pr
Write a java program that processes a set of ballots. Assume you have 1000 canadidates in all, each of whom is assigned a unique ID number from 0 to 999. The pr

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site