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.   Write a class that tests and demonstrates that your Candidate and Election classes implement the specification given above. Note that this does not need  to be a Junit test class. Your test class must illustrate all of the features that you have implemented.  This will best be done by having at least one separate test method for each  feature. For instance: • A method that creates a Candidate object and prints out the result of  calling each of its accessor (get) methods;  • A method that adds a vote to an existing Candidate object. • A method that creates an Election object, creates a few Candidate objects and adds them to it, and then calls the printAll method; • A method that finds a matching Candidate; • A method that does not find a matching Candidate; • etc. Note that the list above is not a complete list of the test class’s methods and  you will need to define more than those for this part. It  will be worth writing this test class alongside writing the other classes. For  instance, develop the test methods for the basic Candidate class before going  on to write the Election class

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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.

 

Write a class that tests and demonstrates that your Candidate and Election
classes implement the specification given above. Note that this does not need 
to be a Junit test class.
Your test class must illustrate all of the features that you have implemented. 
This will best be done by having at least one separate test method for each 
feature. For instance:
• A method that creates a Candidate object and prints out the result of 
calling each of its accessor (get) methods; 
• A method that adds a vote to an existing Candidate object.
• A method that creates an Election object, creates a few Candidate
objects and adds them to it, and then calls the printAll method;
• A method that finds a matching Candidate;
• A method that does not find a matching Candidate;
• etc.
Note that the list above is not a complete list of the test class’s methods and 
you will need to define more than those for this part. It 
will be worth writing this test class alongside writing the other classes. For 
instance, develop the test methods for the basic Candidate class before going 
on to write the Election class

Expert Solution
steps

Step by step

Solved in 3 steps with 4 images

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

Write a class that tests and demonstrates that your Candidate and Election
classes implement the specification given above. Note that this does not need 
to be a Junit test class.
Your test class must illustrate all of the features that you have implemented. 
This will best be done by having at least one separate test method for each 
feature. For instance:
• A method that creates a Candidate object and prints out the result of 
calling each of its accessor (get) methods; 
• A method that adds a vote to an existing Candidate object.
• A method that creates an Election object, creates a few Candidate
objects and adds them to it, and then calls the printAll method;
• A method that finds a matching Candidate;
• A method that does not find a matching Candidate;
• etc.
Note that the list above is not a complete list of the test class’s methods and 
you will need to define more than those for this part. It 
will be worth writing this test class alongside writing the other classes. For 
instance, develop the test methods for the basic Candidate class before going 
on to write the Election class 

 

Solution
Bartleby Expert
SEE SOLUTION
Knowledge Booster
Unreferenced Objects
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education