Question

In: Computer Science

Java programming language should be used Implement a class called Voter. This class includes the following:...

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

Solutions

Expert Solution

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


Related Solutions

Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
JAVA - Design and implement a class called Flight that represents an airline flight. It should...
JAVA - Design and implement a class called Flight that represents an airline flight. It should contain instance data that represent the airline name, the flight number, and the flight’s origin and destination cities. Define the Flight constructor to accept and initialize all instance data. Include getter and setter methods for all instance data. Include a toString method that returns a one-line description of the flight. Create a driver class called FlightTest, whose main method instantiates and updates several Flight...
In simple Java language algorithm: Implement a static stack class of char. Your class should include...
In simple Java language algorithm: Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Using the stack class you created in problem 1), write a static method called parse that parses...
Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Programming Language: C# CheckingAccount class You will implement the CheckingAccount Class in Visual Studio. This is...
Programming Language: C# CheckingAccount class You will implement the CheckingAccount Class in Visual Studio. This is a sub class is derived from the Account class and implements the ITransaction interface. There are two class variables i.e. variables that are shared but all the objects of this class. A short description of the class members is given below: CheckingAccount Class Fields $- COST_PER_TRANSACTION = 0.05 : double $- INTEREST_RATE = 0.005 : double - hasOverdraft: bool Methods + «Constructor» CheckingAccount(balance =...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level,...
(In java language) Write an abstract class called House. The class should have type (mobile, multi-level, cottage, etc.) and size. Provide the following methods: A no-arg/default constructor. A constructor that accepts parameters. A constructor that accepts the type of the house and sets the size to 100. All other required methods. An abstract method for calculating heating cost. Come up with another abstract method of your own. Then write 2 subclasses, one for mobile house and one for cottage. Add...
Java Programming language. Proof of concept class design based on the following ideas Look at your...
Java Programming language. Proof of concept class design based on the following ideas Look at your refrigerator and think about how you would model it as a class. Considerations include: A refrigerator is made by a company on a manufacturing date and has an overall size based on length, width, and height A refrigerator contains a number of shelves and drawers for storing dairy, meats, and vegetables A refrigerator also has storage areas on the door for things like bottled...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will...
Write a class called VLPUtility with the following static methods: Java Language 1. concatStrings that will accept a variable length parameter list of Strings and concatenate them into one string with a space in between and return it. 2. Overload this method with two parameters, one is a boolean named upper and one is a variable length parameter list of Strings. If upper is true, return a combined string with spaces in upper case; otherwise, return the combined string as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT