Question

(2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...

(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:

  • 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 should output the details of a Candidate. See sample run
  • equals: This method should determine if a Candidate passed in is equal to the current candidate. Two candidates are equal if their first and last name are the same and their parties are equal.

Results Class: This class stores all of the candidates in an array using aggregation.

     Constants:

  • BLOCK_SIZE set to 3. This variable represents the size of an array and the size it increases by each time it is resized.

Instance variables:

  • An array that stores Candidates
  • An integer that stores the actual number of elements that are populated in the candidate array
  • An array that stores Strings that represent each of the offices represented
  • An integer value that stores the actual number of elements that are populated in the offices array

Methods:

  • Constructor: Set a default constructor that does the following:
    • Instantiates the two arrays to BLOCK_SIZE.
    • Sets the two size variables to 0.
  • Getters for the two sizes
  • toString: This method should call the Candidate toString for each item in the array
  • hasCandidate: This method accepts a Candidate and using a for loop, searches the candidate array for the received candidate. If the candidate is found, return true. Otherwise, return false.
  • isCandidatesFull: This method returns true if the size of the array matches the length of the array. Otherwise, it returns false.
  • isOfficeFull: This method returns true if the size of the array matches the length of the array. Otherwise, it returns false.
  • addCandidate(): This method will prompt and accept user input to get the information needed to create a candidate.
    • It will use the hasCandidate method to ensure that a candidate is not on the list before being added to the list. If candidate is already on the list, return a message that reads, “Candidate already on ballot.”
    • If candidate is not on the list, then call the overloaded addCandidate method and pass in the candidate to be added.
  • addCandidate(Candidate c): This private method accepts the candidate that will be added to the array.
    • First you must verify that the list is not full. If it is full, then you call the resizeCandidate method.
    • Create a new instance of candidate and add it to the candidates array.
    • Determine whether this is a new office by searching the office array for a match. If no match (or array is empty), add the office to the office array, but you must first determine if the array is full, and if so, resize it.
  • addVotes: This method will prompt the user to enter votes for each candidate.
  • determineWinner: This method will display a list of offices that currently have candidates and the user will choose which office they want to determine winner for. It will build a menu of offices, from the offices array and display it to the user. It will call the method createCandidateListByOffice and will pass the users input to the method.
  • createCandidateListByOffice: This private method accepts user input and will build a temporary array that contains a list of all candidates that are running for the specific office the user has selected.
  • findHighestVotes: This private method receives the temporary array and the size of the array from createCandidateListByOffice method.
    • If there is only one candidate running for that office, then the candidate must have at least one vote to win.
    • If there is more than one candidate, then it will search the temp array for the most votes for that office. Set the boolean won value for the winning candidate to true.
    • If the scores are equal, then there is no winner.
  • displayWinners: This method outputs all candidates who have won their race.

ElectionApp:This is your main program. This class will display the menu and drive all the activities.

     Methods:

  • Main Method: This method will instantiate any variables, call the output menu and then call methods in the results class based on the choices of the user. The program should continue until the user selects option 6. It should validate the user input to ensure that the numbers range from 1 to 6 and will repeat the menu if the option is not valid.
  • outputMenu: This method displays the menu

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

  • Create a class diagram for 2 classes – Candidate and Results

View comments (1)

Expert Answer

An expert answer will be posted here

Post a question

Answers from our experts for your tough homework questions

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

Add a comment
Know the answer?
Add Answer to:
(2 bookmarks) In JAVA You have been asked to write a program that can manage candidates...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java program Candidate Class: This class records the information for each candidate that is running for...

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

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

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

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

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

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

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

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

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

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

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT