Question
package election_vote; import java.util.Scanner; import java.util.ArrayList;; public class Election { //Student Name: Vishal Sharma //Student ID: 21501565 public static void main(String[] args) { // TODO
package election_vote;
import java.util.Scanner; import java.util.ArrayList;; public class Election { //Student Name: Vishal Sharma //Student ID: 21501565 public static void main(String[] args) { // TODO Auto-generated method stub //Declare two ArrayList to store the names and vote count of candidate ArrayList
//Get the first vote from the user String vote = input.nextLine(); //Loop until the user the user enter "-1" or empty line while (!vote.equals("-1") && !vote.equals("")) {
//Check if the current vote already exists in the names ArrayList int index = names.indexOf(vote); //If the vote does not exist, add it to the names ArrayList and set its count to 1 if (index == -1) { names.add(vote); count.add(1); } else { //if the vote already exists increment the count int currentCount = count.get(index); count.set(index, currentCount + 1); } //Get the next vote input from the user vote = input.nextLine(); } // Initialise variables to store the max votes and the index of the candidate with the max votes int maxVotes = 0; int maxIndex = 0; //Loop through all the candidates for (int i = 0; i < names.size(); i++) { // If the current candidate has more votes than the current max, update the max votes and index if (count.get(i) > maxVotes) { maxVotes = count.get(i); maxIndex = i; } } //Prints the result of the Election System.out.println("####################"); System.out.println("# Election Results #"); System.out.println("#################### "); for (int i = 0; i < names.size(); i++) { System.out.println(names.get(i) + " " + "recieved" + " " +count.get(i) + " " + "votes"); } System.out.println("------------------------------------"); System.out.println("The winner is " + names.get(maxIndex) + " with " + maxVotes + " votes!"); } }
Give me the correct flow chart for the above code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started