Write a class called Candidate to store details of a candidate in the election.  The class must define fields to store the following details: • The name of the candidate. The value will be a String which will  always have a length of at least 1 character. • The number of votes the candidate has received. This is an integer  value. The value will always be greater-than or equal to zero. The class must have a single constructor that takes the name of a candidate.  You do not need to check that the value passed to the constructor is valid.  You may assume that it always will be. In other words, you may assume that  the name String will be at least one character long. The initial value of votes must always be zero when the object is constructed and it is not passed as a  parameter to the constructor. The class must define the following methods: • An accessor (get) method for each field. • A method called voteFor that adds 1 to the votes field. • A method called print to print the values of the fields on a single line  in exactly the following format: Dodgy Drew has 5 votes. where "Dodgy Drew" is an example of a String in the name field and 5 is an example of the current value in the votes field. Each Candidate object will have its own particular values for these fields.   THEN Write a class called Election that will store Candidate objects for a  particular election event. Write the Election class with the following features: • It must use a suitable list-style collection class from the java.util  package to store multiple Candidate objects; • An addCandidate method that takes a Candidate object as  parameter and stores it in the collection. You may assume that no two  Candidate objects with the same name will be stored via this method. Note that this method must not create a Candidate object. • A getCandidates method that returns the collection of Candidate objects. • A printAll method that iterates over the collection and prints out  details of all the candidates in the collection by calling each Candidate object’s print method. At the end of the method, a summary of the  total number of votes cast so far for all candidates must be printed. For  instance: Dodgy Drew has 5 votes. Trustworthy Taylor has 12 votes. Posh Phil has 6 votes. Total votes cast: 23 • A findCandidate method that takes a String as a parameter and  returns the Candidate object whose name field exactly matches the  parameter’s value. If there is no match, return null. It is guaranteed  that all candidates will have different names. • A findTopCandidates method that takes a single integer as a  parameter (representing a number of votes) and returns a separate list  of all Candidate objects whose votes field has a value greater-than or  equal-to the parameter’s value. No Candidate objects are to be removed from the Election object’s list and the list object returned must be a different object from the Election object’s list of all donors. The list must be empty if no candidate has  sufficient votes. • A removeLast method that finds the Candidate(s) with the fewest  number of votes and removes them from the collection. The method  must return the number of candidates removed. The following rules apply: If more than one candidate has the fewest  number of votes then all of those matching candidates must be  removed, unless removing them all would mean that there were no  candidates left. For instance: o Three candidates with 5, 4 and 3 votes each: the one with 3  votes would be removed, leaving two remaining. The method  returns 1. o Three candidates with 5, 3 and 3 votes each: the ones with 3  votes would be removed, leaving one remaining. The method  returns 2. o Three candidates with 3, 3 and 3 votes each: no one would be  removed, leaving three remaining. The method returns 0.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Write a class called Candidate to store details of a candidate in the election. 
The class must define fields to store the following details:
• The name of the candidate. The value will be a String which will 
always have a length of at least 1 character.
• The number of votes the candidate has received. This is an integer 
value. The value will always be greater-than or equal to zero.
The class must have a single constructor that takes the name of a candidate. 
You do not need to check that the value passed to the constructor is valid. 
You may assume that it always will be. In other words, you may assume that 
the name String will be at least one character long. The initial value of votes
must always be zero when the object is constructed and it is not passed as a 
parameter to the constructor.
The class must define the following methods:
• An accessor (get) method for each field.
• A method called voteFor that adds 1 to the votes field.
• A method called print to print the values of the fields on a single line 
in exactly the following format:
Dodgy Drew has 5 votes.
where "Dodgy Drew" is an example of a String in the name field and 5
is an example of the current value in the votes field. Each Candidate
object will have its own particular values for these fields.

 

THEN


Write a class called Election that will store Candidate objects for a 
particular election event.
Write the Election class with the following features:
• It must use a suitable list-style collection class from the java.util 
package to store multiple Candidate objects;
• An addCandidate method that takes a Candidate object as 
parameter and stores it in the collection. You may assume that no two 
Candidate objects with the same name will be stored via this method.
Note that this method must not create a Candidate object.
• A getCandidates method that returns the collection of Candidate
objects.
• A printAll method that iterates over the collection and prints out 
details of all the candidates in the collection by calling each Candidate
object’s print method. At the end of the method, a summary of the 
total number of votes cast so far for all candidates must be printed. For 
instance:
Dodgy Drew has 5 votes.
Trustworthy Taylor has 12 votes.
Posh Phil has 6 votes.
Total votes cast: 23
• A findCandidate method that takes a String as a parameter and 
returns the Candidate object whose name field exactly matches the 
parameter’s value. If there is no match, return null. It is guaranteed 
that all candidates will have different names.
• A findTopCandidates method that takes a single integer as a 
parameter (representing a number of votes) and returns a separate list 
of all Candidate objects whose votes field has a value greater-than or 
equal-to the parameter’s value.
No Candidate objects are to be removed from the Election object’s list
and the list object returned must be a different object from the Election
object’s list of all donors. The list must be empty if no candidate has 
sufficient votes.
• A removeLast method that finds the Candidate(s) with the fewest 
number of votes and removes them from the collection. The method 
must return the number of candidates removed.
The following rules apply: If more than one candidate has the fewest 
number of votes then all of those matching candidates must be 
removed, unless removing them all would mean that there were no 
candidates left. For instance:
o Three candidates with 5, 4 and 3 votes each: the one with 3 
votes would be removed, leaving two remaining. The method 
returns 1.
o Three candidates with 5, 3 and 3 votes each: the ones with 3 
votes would be removed, leaving one remaining. The method 
returns 2.
o Three candidates with 3, 3 and 3 votes each: no one would be 
removed, leaving three remaining. The method returns 0.

Expert Solution
steps

Step by step

Solved in 2 steps with 4 images

Blurred answer
Follow-up Questions
Read through expert solutions to related follow-up questions below.
Follow-up Question

is this just for the candidate class or is the election class included?

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Data members
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY