Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Suppose you are counting blalots in an election for Supreme Ruler of the Univers

ID: 3681671 • Letter: S

Question

Suppose you are counting blalots in an election for Supreme 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 Java 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 shoul 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 numbers and their number of votes recieved, but only if the candidate recieved atleast one vote.

Explanation / Answer

import java.util.Scanner;

public class CountingBlalots {

   public static void main(String[] args) {
      
       int blalots[] = new int[1000];
      
       Scanner sc = new Scanner(System.in);
       int id = 0;
      
       while(true){
           // reading id
           System.out.print("Enter Id number: ");
           id = sc.nextInt();
          
           if(id < 0)
               break;
           else if(id > 999)
               System.out.println("ID can be in range 0-999");
           else{
               // reading votes
               System.out.print("Enter vote received by "+id+" candidate: ");
               int vote = sc.nextInt();
               blalots[id] = vote;
           }
       }
      
       // printing id and votes
       System.out.println("ID"+"           "+"Votes");
       for(int i=0; i<1000; i++){
           if(blalots[i] > 0)
               System.out.println(i+"           "+blalots[i]);
       }
   }
}

/*

Output:

Enter Id number: 2
Enter vote received by 2 candidate: 1222312
Enter Id number: 5
Enter vote received by 5 candidate: 12231
Enter Id number: -1
ID           Votes
2           1222312
5           12231

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote