Question

Use Java in Eclipse to create an online voting system for Kenya- the country.

Use Java in Eclipse to create an online voting system for Kenya- the country.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import java.lang.*;
import java.util.*;
import java.io.*;
//class Coach
class Vote{
   //driver method
   public static void main(String args[])throws Exception{
       BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
       //You can edit the names of the party as per your wish and any number of parties and nowhere else you have to make any change
       String[] partyNames = {"Party A","Party B","Party C","Party D","Party E","Party F"};
       //if you change the number of parties the number of votes elements should also change accordingly
       int[] votes = {0,0,0,0,0,0};
       int choice = 0;
      
       System.out.println("Welcome to the Online Voting System of Kenya");
       System.out.println("-------------------------------------------------------");
       //infinite loop for all the users to vote
       while(true) {
           //asking the user to enter the number along side the party name
           System.out.println("\nCast your vote,\nEnter the number alongside your party of choice: ");
           for(int i=0;i<partyNames.length;i++) {
               System.out.println((i+1)+" for "+partyNames[i]);
           }
           //0 will exit and show the result
           System.out.println("0 to get the Results ");
           choice = Integer.parseInt(in.readLine());
           if(choice<0||choice>partyNames.length) {
               System.out.println("Invalid Choice! Try Again");
           }else if(choice == 0){
               break;
           }else {
               //incrementing the vote count in the array corresponding to the party name
               votes[choice-1] += 1;
               System.out.println("You Voted For "+partyNames[choice-1]+". Thank You!");
               System.out.println("-----------------------------------------------------");
           }
       }
       System.out.println("-----------------------------------------------------");
      
       //initially setting the first party as the highest
       int max = votes[0];
       int maxIndex = 0;
       //finding the highest vote winner
       System.out.println("\nVoting Results:\nParty names alongside their vote counts: ");
       for(int i=0;i<partyNames.length;i++) {
           //if the current element in the array has a vote greater than the previous max this party is the current winner
           if(votes[i]>max) {
               max = votes[i];
               maxIndex = i;
           }
           System.out.println(partyNames[i]+"\t\t\t"+votes[i]+" votes");
       }
       System.out.println("\nThe party with highest vote is/are: ");
       //Checking for any tie
       for(int i=0;i<partyNames.length;i++) {
           if(votes[i] == max) {
               //displaying the winners
               System.out.println(partyNames[i]+" with "+votes[i]+" votes!");
           }
       }
   }
}

_____________________________________________________________________

CODE IMAGES AND OUTPUT:

- eclipse-workspace - Akshansh/src/Vote.java - Eclipse IDE File Edit Source Refactor Navigate Search Project Run Window Help

- eclipse-workspace - Akshansh/src/Vote.java - Eclipse IDE Eile Edit Source Refactor Navigate Search Project Run Window Help

OUTPUT:

I am not showing you the entire output where all the votes are cast because they are the same, you can try it youtself, so I am showing you the main parts of the output:

- eclipse-workspace - Akshansh/src/Vote.java - Eclipse IDE File Edit Source Refactor Navigate Search Project Run Window Help- eclipse-workspace - Akshansh/src/Vote.java - Eclipse IDE File Edit Source Refactor Navigate Search Project Run Window Help- eclipse-workspace - Akshansh/src/Vote.java - Eclipse IDE File Edit Source Refactor Navigate Search Project Run Window Help__________________________________________________

Feel free to ask any questions in the comments section
Thank You!

?mid=&wid=52562&sid=&tid=8561&rid=LOADED?mid=&wid=52562&sid=&tid=8561&rid=BEFORE?mid=&wid=52562&sid=&tid=8561&rid=FINISH?mid=&wid=52562&sid=&tid=8561&rid=OPTOUT

Add a comment
Know the answer?
Add Answer to:
Use Java in Eclipse to create an online voting system for Kenya- the country.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT