Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package election; import java.util.Scanner; import java.util.ArrayList;; public class ELECTION { public static void main(String[] args) { // TODO Auto-generated method stub //Declare two ArrayList to

package election;

import java.util.Scanner;

import java.util.ArrayList;;

public class ELECTION {

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) + " " + ":" + " " +count.get(i) + " " + "votes");

}

System.out.println("------------------------------------");

System.out.println("The winner is " + names.get(maxIndex) + " with " + maxVotes + " votes)!");

}

}

Generate flowchart for this code and give me correct flowchart w

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

Managing Your Information How To Design And Create A Textual Database On Your Microcomputer

Authors: Tenopir, Carol, Lundeen, Gerald

1st Edition

1555700233, 9781555700232

More Books

Students also viewed these Databases questions

Question

c. What were the reasons for their move? Did they come voluntarily?

Answered: 1 week ago

Question

5. How do economic situations affect intergroup relations?

Answered: 1 week ago