In: Computer Science
Java programming language should be used
Implement a class called Voter. This class includes the following:
a name field, of type String.
An id field, of type integer.
A method String setName(String) that stores its input into the name attribute, and returns the
name that was just assigned.
A method int setID(int) that stores its input into the id attribute, and returns the id number
that was just assigned.
A method String getName() that return the name attribute.
A method int getID() that returns the id attribute.
A constructor method that takes no inputs, and then sets the name attribute to none and the
id attribute to -1.
A constructor method that takes both an integer and a string, in that order. The integer is
stored in the id attribute, and the string gets stored in the name attribute.
Implement a Voters class (notice the ‘s’ at the end of the class name).
In terms of attributes, this class will simply have a HashSet of Voter inside.
As methods, implement the following:
▪ boolean addVoter(Voter), which adds the input Voter to the set and returns true, if the voter doesn’t exist in the set already. If the voter does exist in the set, the method returns false. Two objects of type Voter are equal if they have the same ID.
▪ boolean deleteVoter(int), which deletes the Voter from the set that has the passed integer as its ID, and returns true, if the voter exist in the set. If the voter does not exist in the set, the method returns false.
▪ boolean isASubsetOf(Voters), which returns true if the Voters for which it is called is a subset of the Voters it receives as input. Otherwise, it returns false.
Instantiate two objects of class Voters
▪ an object called registeredVoters.
▪ An object called votersWhoHaveCastBallots
You should test your code with a main program. In that main program you should do the following:
▪ add different voters to the registeredVoters object.
▪ Try to add a voter to the registeredVoters that already exists. You should get an error
message.
▪ Try to delete a voter from the registeredVoters that doesn’t exists. You should get an
error message.
▪ Try to delete a voter from the registeredVoters that does exists. You should get a
message confirming that the operation was carried out.
▪ Add a voter to votersWhoHaveCastBallots who is not in registeredVoters. Check to see
if the HashSet inside votersWhoHaveCastBallots is a subset of the HashSet inside registeredVoters. This should cause your program to display an error message notifying of this fact, and display the voter object that caused the problem.
◦ Next, run the deleteVoter method on votersWho HaveCastBallots until it is a subset of registeredVoters. Check to see if votersWhoHaveCastBallots is a subset
of registeredVoters. This should cause your program to display a message notifying of this fact, and displaying the elements of votersWhoHaveCastBallots which cause this error message.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
//Voter.java
public class Voter {
//private fields
private String name;
private int id;
//setters and getters
public String setName(String name) {
this.name = name;
return this.name;
}
public int setID(int id) {
this.id = id;
return this.id;
}
public int getID() {
return id;
}
public String getName() {
return name;
}
//constructor initializing name to "none" and id to -1
public Voter() {
name = "none";
id = -1;
}
//constructor taking an id and a name
public Voter(int id, String name) {
this.id = id;
this.name = name;
}
}
//Voters.java file
import java.util.HashSet;
public class Voters {
// attribute - private voters HashSet
private HashSet<Voter> voters;
// constructor to initialize the set
public Voters() {
voters = new HashSet<Voter>();
}
// adds a voter to the voters list and returns true if added, false if not
public boolean addVoter(Voter voter) {
for (Voter v : voters) {
if (v.getID() == voter.getID()) {
// already exists, printing error message (for testing)
System.out.println("Voter with ID " + v.getID()
+ " already exists!");
return false; // indicating operation failed
}
}
// if no duplication found, adding voter
voters.add(voter);
// printing success message (for testing)
System.out.println("Voter with ID " + voter.getID()
+ " added successfully!");
return true; // indicating operation succeeded
}
// deletes a voter with given id if exists
public boolean deleteVoter(int id) {
for (Voter v : voters) {
if (v.getID() == id) {
// found, removing
voters.remove(v);
// printing a message (for testing)
System.out.println("Voter with ID " + id + " is deleted!");
return true;
}
}
// not found
// printing a message (for testing)
System.out.println("Voter with ID " + id + " is not found!");
return false;
}
// returns true if this is a subset of other voters list
public boolean isASubsetOf(Voters other) {
// looping through each voter on this hashset
for (Voter v1 : voters) {
// initially v1 is not found
boolean found = false;
// looping through each voter on other hashset
for (Voter v2 : other.voters) {
if (v1.getID() == v2.getID()) {
// found on other set
found = true;
// exiting inner loop
break;
}
}
// if v1 is not found on other set, displaying error and returning
// false
if (!found) {
System.out.println("Voter with ID: " + v1.getID()
+ " is not found on other voters set");
return false;
}
}
return true; // all voters of this set exist on other
}
// main method for testing
public static void main(String[] args) {
// creating two Voters object
Voters registeredVoters = new Voters();
Voters votersWhoHaveCastBallots = new Voters();
// adding voters to registeredVoters
System.out.println("Adding voters to registeredVoters list...");
registeredVoters.addVoter(new Voter(1, "Alice"));
registeredVoters.addVoter(new Voter(2, "Bob"));
registeredVoters.addVoter(new Voter(3, "Cait"));
registeredVoters.addVoter(new Voter(4, "David"));
registeredVoters.addVoter(new Voter(5, "Eric"));
registeredVoters.addVoter(new Voter(6, "Ford"));
// trying to test duplicate addition
Voter v = new Voter(2, "Bob");
System.out
.println("\nTrying to add a voter that already exist in registeredVoters");
registeredVoters.addVoter(v);
// testing deletion
System.out
.println("\nTrying to delete a voter that exist in registeredVoters");
registeredVoters.deleteVoter(v.getID());
v = new Voter(12, "Zach");
System.out
.println("\nTrying to delete a voter that does not exist in registeredVoters");
registeredVoters.deleteVoter(v.getID());
// adding voters to votersWhoHaveCastBallots
System.out
.println("\nAdding voters to votersWhoHaveCastBallots list...");
votersWhoHaveCastBallots.addVoter(new Voter(1, "Alice"));
votersWhoHaveCastBallots.addVoter(new Voter(4, "David"));
votersWhoHaveCastBallots.addVoter(new Voter(3, "Cait"));
votersWhoHaveCastBallots.addVoter(new Voter(2, "Bob"));
votersWhoHaveCastBallots.addVoter(new Voter(21, "Kevin"));
// testing isASubsetOf. should be false because voters with IDs 2 and 21
// exist on votersWhoHaveCastBallots but does not exist on
// registeredVoters
System.out
.println("\nChecking if votersWhoHaveCastBallots is a subset of registeredVoters");
if (votersWhoHaveCastBallots.isASubsetOf(registeredVoters)) {
System.out
.println("votersWhoHaveCastBallots is a subset of registeredVoters");
}
//deleting voter with id 21
System.out.println("\nDeleting voter with ID 21");
votersWhoHaveCastBallots.deleteVoter(21);
System.out
.println("\nChecking if votersWhoHaveCastBallots is a subset of registeredVoters");
if (votersWhoHaveCastBallots.isASubsetOf(registeredVoters)) {
System.out
.println("votersWhoHaveCastBallots is a subset of registeredVoters");
}
//deleting voter with id 2
System.out.println("\nDeleting voter with ID 2");
votersWhoHaveCastBallots.deleteVoter(2);
System.out
.println("\nChecking if votersWhoHaveCastBallots is a subset of registeredVoters");
//now votersWhoHaveCastBallots must be a subset of registeredVoters
if (votersWhoHaveCastBallots.isASubsetOf(registeredVoters)) {
System.out
.println("votersWhoHaveCastBallots is a subset of registeredVoters");
}
}
}
/*OUTPUT*/
Adding voters to registeredVoters list...
Voter with ID 1 added successfully!
Voter with ID 2 added successfully!
Voter with ID 3 added successfully!
Voter with ID 4 added successfully!
Voter with ID 5 added successfully!
Voter with ID 6 added successfully!
Trying to add a voter that already exist in registeredVoters
Voter with ID 2 already exists!
Trying to delete a voter that exist in registeredVoters
Voter with ID 2 is deleted!
Trying to delete a voter that does not exist in registeredVoters
Voter with ID 12 is not found!
Adding voters to votersWhoHaveCastBallots list...
Voter with ID 1 added successfully!
Voter with ID 4 added successfully!
Voter with ID 3 added successfully!
Voter with ID 2 added successfully!
Voter with ID 21 added successfully!
Checking if votersWhoHaveCastBallots is a subset of registeredVoters
Voter with ID: 21 is not found on other voters set
Deleting voter with ID 21
Voter with ID 21 is deleted!
Checking if votersWhoHaveCastBallots is a subset of registeredVoters
Voter with ID: 2 is not found on other voters set
Deleting voter with ID 2
Voter with ID 2 is deleted!
Checking if votersWhoHaveCastBallots is a subset of registeredVoters
votersWhoHaveCastBallots is a subset of registeredVoters