(2 bookmarks)
In JAVA
You have been asked to write a program that can manage candidates for an upcoming election. This program needs to allow the user to enter candidates and then record votes as they come in and then calculate results and determine the winner.
This program will have three classes:
Candidate, Results and ElectionApp
Candidate Class: This class records the information for each candidate that is running for office.
Instance variables:
Methods:
Results Class: This class stores all of the candidates in an array using aggregation.
Constants:
Instance variables:
Methods:
ElectionApp:This is your main program. This class will display the menu and drive all the activities.
Methods:
Choose from the following options:
1- Add a candidate
2- Add votes
3- Determine winner
4- Display a list of candidates
5- Display winners
6- Exit
Design Requirements
View comments (1)
Expert Answer
An expert answer will be posted here
Post a question
Answers from our experts for your tough homework questions
public class Candidate {
String fname;
String lname;
String office;
String party;
int totalVotes;
boolean win;
Candidate(String fname, String lname, String office, String
party){
this.fname = fname;
this.lname = lname;
this.office = office;
this.party = party;
}
Candidate(Candidate pass){
fname = pass.fname;
lname = pass.lname;
office = pass.office;
party = pass.party;
}
public String getFname() {
return fname;
}
public String getLname() {
return lname;
}
public String getOffice() {
return office;
}
public String getParty() {
return party;
}
public int getTotalVotes() {
return totalVotes;
}
public boolean isWin() {
return win;
}
public void setTotalVotes(int totalVotes) {
this.totalVotes = totalVotes;
}
public void setWin(boolean win) {
this.win = win;
}
public void tostring(){
System.out.println("Name : "+fname+" "+lname);
System.out.println("Office :"+office);
System.out.println("Party :"+party);
System.out.println("Total Votes : "+totalVotes);
System.out.println("Is Winner :"+win);
}
public boolean equal(Candidate pass){
if(fname == pass.fname && lname == pass.lname &&
party == pass.party){
return true;
}
else{
return false;
}
}
}
import java.util.Scanner;
public class Results {
int BLOCK_SIZE = 3;
Candidate[] candidates;
int numberof_candidates;
String[] offices;
int numberof_offices;
Scanner input = new Scanner(System.in);
Results(){
candidates = new Candidate[BLOCK_SIZE];
offices = new String[BLOCK_SIZE];
numberof_candidates = 0;
numberof_offices = 0;
}
public int getNumberof_candidates() {
return numberof_candidates;
}
public int getNumberof_offices() {
return numberof_offices;
}
public void tostring(){
for(int i=0; i<BLOCK_SIZE; i++){
candidates[i].tostring();
}
}
public boolean hasCandidate(Candidate pass){
boolean result = false;
for(int i=0; i<BLOCK_SIZE; i++){
if(candidates[i] == pass){
result = true;
}
}
return result;
}
public boolean isCandidatesFull(){
if(candidates.length == BLOCK_SIZE){
return true;
}
else{
return false;
}
}
public void addCandidate(){
System.out.println("Enter candidate fame....");
String fname = input.next();
System.out.println("Enter candidate lname....");
String lname = input.next();
System.out.println("Enter candidate office...");
String office = input.next();
System.out.println("Enter candidate party....");
String party = input.next();
Candidate temp = new Candidate(fname,lname,office,party);
if(hasCandidate(temp) == true){
System.out.println("Candidate is already there....");
}
else{
addCandidate(temp);
}
}
public void addCandidate(Candidate pass){
if(isCandidatesFull() == false){
for(int i=0; i<BLOCK_SIZE; i++){
if(candidates[i] == null){
candidates[i] = pass;
}
}
}
else{
resizeCandidate();
}
}
public void resizeCandidate(){
BLOCK_SIZE +=1;
Candidate[] temp =new Candidate[BLOCK_SIZE];
for(int i=0; i<BLOCK_SIZE-1; i++){
temp[i] = candidates[i];
}
}
public void addVotes(){
for(int i=0; i<BLOCK_SIZE; i++){
System.out.println("Enter votes for candidate "+(i+1));
int v = input.nextInt();
candidates[i].totalVotes = v;
}
}
public void determineWiner(){
int max = 0;
for(int i=0; i<BLOCK_SIZE; i++){
if(candidates[i].totalVotes >0){
max = candidates[i].totalVotes;
}
}
for(int i=0; i<BLOCK_SIZE; i++){
if(candidates[i].totalVotes >max){
candidates[i].setWin(true);
}
else{
candidates[i].setWin(false);
}
}
}
}
import java.util.Scanner;
public class ElectionApp {
public void Menu(){
System.out.println("1- Add candidate");
System.out.println("2- Add Votes");
System.out.println("3- Determine Winner");
System.out.println("4- Display candidate list");
System.out.println("5- Display winner");
System.out.println("6- Exit");
System.out.println("Enter your choice");
Scanner input = new Scanner(System.in);
int choice = input.nextInt();
if(choice<0 ||choice >6){
System.out.println("Enter correct choice");
choice = input.nextInt();
}
else{
Results r = new Results();
if(choice == 1){
r.addCandidate();
}
else if(choice == 2){
r.addVotes();
}
else if(choice == 3){
r.determineWiner();
}
else if(choice ==4){
r.tostring();
}
else if(choice == 5){
r.determineWiner();
}
else{
System.out.println("Exit");
}
}
}
}
COMMENT DOWN FOR ANY QUERIES AND,
LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.
(2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...
Java program Candidate Class: This class records the information for each candidate that is running for office. Instance variables: first name last name office they are running for the party they represent total votes a boolean value to record if they won Methods: Custom constructor that accepts first and last name, office and party Custom constructor that accepts an instance of another customer Getters for all instance variables and setters for total votes and the boolean variable toString: This method...
COSC 1437 C++ Project Assignment 2 Poll Summary Utilize a dynamic array of structure to summarize votes for a local election Specification: Write a program that keeps track the votes that each candidate received in a local election. The program should output each candidate’s name, the number of votes received, and the percentage of the total votes received by the candidate. Your program should also output the winner of the election. To solve this problem, you will use one dynamic...
Overview JAVA LANGUAGE PROBLEM: The owner of a restaurant wants a program to manage online orders that come in. This program will require multiple classes. Summary class, customer class, order class and the driver class. Summary class This class will keep track of the cost of the order and output a summary, which includes the total for the order. The methods in this class are static. There are no instance variables, and instead uses an Order object that is passed...
Java Question 3 Implement a program to store the applicant's information for a visa office. You need to implement the following: A super class called Applicant, which has the following instance variables: A variable to store first name of type String. A variable to store last name of type String. A variable to store date of birth, should be implemented as another class to store day, month and year. A variable to store number of years working of type integer...
DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...
In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...
It’s almost election day and the election officials need a program to help tally election results. There are two candidates for office—Polly Tichen and Ernest Orator. The program’s job is to take as input the number of votes each candidate received in each voting precinct and find the total number of votes for each. The program should print out the final tally for each candidate—both the total number of votes each received and the percent of votes each received. Clearly...
Programming Assignment 6 Write a Java program that will implement a simple appointment book. The program should have three classes: a Date class, an AppointmentBook class, and a Driver class. • You will use the Date class that is provided on Blackboard (provided in New Date Class example). • The AppointmentBook class should have the following: o A field for descriptions for the appointments (i.e. Doctor, Hair, etc.). This field should be an array of String objects. o A field...
Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...
Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...