you are required to create a console application that will capture the amount of votes that a student representative has received during the annual student body elections. the application must capture the student names and the amount of votes. in your solution, include a class called votes. the class must include the following:
*variables to store the student name and votes received.
* variables to store the total and average votes.
*use a constructor or method to set the variables.
* two methods to calculate the total and average votes.
* a method to print the report. the report must display good or poor voting attendance. if the total amount of student votes is greater than 150, display good voting attendance, otherwise display poor voting attendance
import java.util.ArrayList;
import java.util.Scanner;
class Votes {
// variables to store the student name and votes received.
private ArrayList<String> studentNames;
private ArrayList<Integer> votesReceived;
// variables to store the total and average votes.
private int totalVotes;
private double averageVotes;
/**
* Constructor
*/
public Votes() {
this.studentNames = new ArrayList<String>();
this.votesReceived = new ArrayList<Integer>();
this.totalVotes=0;
}
// use a constructor or method to set the variables.
public void addStudentName(String name) {
this.studentNames.add(name);
}
public void addStudentVotesReceived(int votesReceived) {
this.votesReceived.add(votesReceived);
}
// two methods to calculate the total and average votes.
private void calculateTotalVotes() {
for(int i=0;i<votesReceived.size();i++) {
totalVotes+=votesReceived.get(i);
}
}
/**
* Calculate average votes
*/
private void calculateAverageVotes() {
this.averageVotes=(double)totalVotes/(double)votesReceived.size();
}
/**
* a method to print the report. the report must display good or poor voting
* attendance. if the total amount of student votes is greater than 150, display
* good voting attendance, otherwise display poor voting attendance
*/
public void printReport() {
System.out.println();
for(int i=0;i<votesReceived.size();i++) {
System.out.println("The student "+studentNames.get(i)+" received "+votesReceived.get(i)+" votes.");
}
calculateTotalVotes();
calculateAverageVotes();
System.out.println("\nThe total votes: "+this.totalVotes);
System.out.println("The average votes: "+this.averageVotes+"\n");
if(this.totalVotes>150) {
System.out.println("Good voting attendance.\n");
}else {
System.out.println("Poor voting attendance.\n");
}
}
}
public class VotesProject {
/**
* The start point of the program
*
* @param args
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Votes votes=new Votes();
System.out.print("Enter the number of students: ");
int numberStudents=scanner.nextInt();
for(int i=0;i<numberStudents;i++) {
scanner.nextLine();
System.out.print("Enter the student name "+(i+1)+": ");
String studentName=scanner.nextLine();
votes.addStudentName(studentName);
System.out.print("How many votes did student "+studentName+" receive?: ");
int votesReceived=scanner.nextInt();
votes.addStudentVotesReceived(votesReceived);
}
votes.printReport();
scanner.close();
}
}
Comments
Leave a comment