Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 names = new ArrayList(); ArrayList count = new ArrayList(); //Create a Scanner object to read input from the user Scanner input = new Scanner(System.in); //Print welcome message & instruction for the user System.out.println("###############################################"); System.out.println("# Welcome to the election! #"); System.out.println("# Please enter the name of the candidate. #"); System.out.println("# End with -1 or an empty line. #"); System.out.println("###############################################");

//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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Modeling And Design

Authors: Toby J. Teorey, Sam S. Lightstone, Tom Nadeau, H.V. Jagadish

5th Edition

0123820200, 978-0123820204

More Books

Students also viewed these Databases questions