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
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
Step by step
Solved in 3 steps with 4 images
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