Question

In: Computer Science

Create JAVA PROGRAM, and write comment for codes also. 1) Let the user enter in candidate...

Create JAVA PROGRAM, and write comment for codes also.

1) Let the user enter in candidate names for a ballot. Then imagine the program is moved to a voting booth. Each voter enters their unique name and who they vote for, and when there are no more voters display who won. (Imagine something outside your program is telling it there are no more voters.)

Solutions

Expert Solution

import java.util.*;
import java.io.*;

public class Main
{
public static void main (String[] args) throws IOException{

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("please enter names of all candidates for election (seperated by comma)");
String[] names = br.readLine().trim().split(",");
  
Map<String , Integer> hm = new HashMap<>(); //// hasmap to store candidate name and vote count for them
int size = names.length;

/// storing candiadte name with initial vote count as 0
for(int i = 0; i<size;i++)
{
hm.put(names[i].toLowerCase().trim(), 0);
}
  

////////// Moving to polling booth //////////////
System.out.println("------POLLING BOOTH------");
  
boolean isThereAVoter = false; //// variable which indicates presence of a voter


System.out.println("Is there any voter?"); /// intraction with outsider for first time
String outsiders_reply = br.readLine().trim(); /// listening reply of outsider

if(outsiders_reply.toLowerCase().equals("yes"))
{
isThereAVoter = true;
while(isThereAVoter)
{   
////displaying ballot paper ////
System.out.println("--Ballot Paper--");
for(String s : names)
{
System.out.println(s);
}
  
System.out.println();
System.out.println();
  

///// taking voter name and candiadte name whom he/she wants to vote for
System.out.print("Sir , Please Enter your name : ");
br.readLine().trim();

System.out.print("Sir, Please enter your choice of candidate : ");
String candidate_choice = br.readLine().trim().toLowerCase();
  
////// Checking the validating of entered candidate name

// if valid candidate name , then
if(hm.containsKey(candidate_choice))
{
///// adding voter's vote into the corresponding candiadte's vote count
hm.put(candidate_choice, hm.get(candidate_choice) + 1);
System.out.println("Sucessfullly voted!");

System.out.println();
System.out.println();

///// telling outsider to send next voter
System.out.println("Hey outsider!, is there any voter left?");
outsiders_reply = br.readLine().trim();

if(outsiders_reply.toLowerCase().equals("yes"))
{
System.out.println("Please send inside.");
}

else ///// if no voter is left... displaying the winner
{
isThereAVoter = false; ///s setting presence of voter to false as there is no voter left
  
/////// getting the winner from the hasmap in which the candiadte name and corresponing vote count is stored
Set<Map.Entry<String, Integer>> set = hm.entrySet();
Iterator itr = set.iterator();
String winner = "";
int max_vote = 0;
  
while(itr.hasNext())
{
Map.Entry<String, Integer> e = (Map.Entry<String, Integer>)itr.next();
if(e.getValue() > max_vote)
{
winner = e.getKey();
  
max_vote = e.getValue();
}
  
}

/////// announcement for the winner
System.out.println("Winnner is ------ " + winner);

}
}

////// if name of candidate enterd by voter is not present in ballot paper, then
else
{
System.out.println();
System.out.println("Please enter a candidate name from the above list");
System.out.println();
}
}
}

////// if no voter came to vote
else{
System.out.println("No over is intrested to vote, Hence they didn't come");
}
  
  
}
}


Related Solutions

Create JAVA PROGRAM, and write comment for codes also. 4) Prompt the user for how much...
Create JAVA PROGRAM, and write comment for codes also. 4) Prompt the user for how much stuff a truck can carry, in pounds. Then ask them for the weight of each thing they add until they stop. Don't forget to be safe.
Create JAVA PROGRAM, and write comment for codes also. 3) You want to keep track of...
Create JAVA PROGRAM, and write comment for codes also. 3) You want to keep track of your progress towards running a 10K. There are two kinds of races - 5K and 10K. The program needs to ask what race was run and what the time was in seconds until the user quits. When they quit, display the average and best time for each type of race in minutes.
Create JAVA PROGRAM, and write comment for codes also. 2) Every extra 3500 calories means gaining...
Create JAVA PROGRAM, and write comment for codes also. 2) Every extra 3500 calories means gaining a pound. Falling short by 3500 means losing a pound. Get the base amount of calories the person needs in a day (BMR). Then have the user enter in calorie totals for each day and display their weight every week. They might eat more than once in a day.
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
Write a java program to let the user enter the path to any directory on their...
Write a java program to let the user enter the path to any directory on their computers. Then list all the files in that directory.
Create in Java a program that will prompt the user to enter aweight for a...
Create in Java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates both bolus and infusion rates based on weight of patient in an interactive GUI application, label it AMI Calculator. The patients weight will be the only entry from the user. Use 3999 as a standard for calculating BOLUS: To calculate the BOLUS you will multiply 60 times the weight of the patient for a total number. IF the...
Create in java a program that will prompt the user to enter a weight for a...
Create in java a program that will prompt the user to enter a weight for a patient in kilograms and that calculates infusion rates based on weight of patient in an interactive GUI application, label it HEPCALC. The patients’ weight will be the only entry from the user. To calculate the infusion rate you will multiply 12 times the weight divided by 50 for a total number. The end result will need to round up or down the whole number....
Java Program 1. Write a program that asks the user: “Please enter a number (0 to...
Java Program 1. Write a program that asks the user: “Please enter a number (0 to exit)”. Your program shall accept integers from the user (positive or negative), however, if the user enters 0 then your program shall terminate immediately. After the loop is terminated, return the total sum of all the previous numbers the user entered. a. What is considered to be the body of the loop? b. What is considered the control variable? c. What is considered to...
5) Create the following in a Java program Create a scanner Prompt the user to enter...
5) Create the following in a Java program Create a scanner Prompt the user to enter the name where the box of mail is shipping from and create the variable and relate to scanner Prompt the user to enter the name of destination where the box of mail will be shipped and create the variable and relate to scanner Prompt the user to enter the weight of the package and create variable to relate to scanner Calculate cost of shipping...
Program should be written in Java a) Write a program that asks the user to enter...
Program should be written in Java a) Write a program that asks the user to enter the approximate current population of India. You should have the computer output a prompt and then YOU (as the user should enter the population.)  For testing purposes you may use the value of 1,382,000,000 from August 2020. Assume that the growth rate is 1.1% per year. Predict and print the predicted population for 2021 and 2022. The printout should include the year and the estimated...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT