Use the ArrayList class Add and remove objects from an ArrayList Protect from index errors when removing Practice with input loop Details: This homework is for you to get practice adding and removing objects from an ArrayList. The Voter class was used to create instances of Voters which held their name and a voter identification number as instance variables, and the number of instances created as a static variable. This was the class diagram:   The constructor takes a string, passed to the parameter n, which is the name of the voter and which should be assigned to the name instance variable. Every time a new voter is created, the static variable nVoters should be incremented. Also, every time a new voter is created, a new voterID should be constructed by concatenating the string “HI” with the value of nVoters and the length of the name. For example, if the second voter is named “Clark Kent”, then the voterID should be “HI210” because 2 is the value of nVoters and 10 is the number of characters in “Clark Kent”. If the third voter is named “Peter Parker”, then the voterID should be “HI312” because 3 is the value of nVoters and 12 is the number of characters in “Peter Parker”. Note that you will have to use a method to find the length of the names since you can not predict what will be typed. The static method getNVoters returns the value of nVoters. The instance method toString returns a concatenation of voterID and name, separated by a space. Create a main method that uses the Voter class to create voters. It will also use the Scanner class to get each voter’s name from the keyboard. A big difference from Assignment 05 is that the scanner will ask for a command. The user will be able to command the code to both add and remove voters. Here is an example of an interaction (user input is in bold). Command: +Peter Parker Voter List: [HI112 Peter Parker] Command: +Carol Danvers Voter List: [HI112 Peter Parker, HI213 Carol Danvers] Command: +Clark Kent Voter List: [HI112 Peter Parker, HI213 Carol Danvers, HI310 Clark Kent] Command: -Peter Parker Voter List: [HI213 Carol Danvers, HI310 Clark Kent] Command: ?Bad input No action taken. Voter List: [HI213 Carol Danvers, HI310 Clark Kent] Command: -Clark Kent Voter List: [HI213 Carol Danvers] Command: -Batman Voter List: [HI213 Carol Danvers] Command: -Carol Danvers Voter List: [] Command: Done In this interaction, voters are added using a plus sign and voters are removed by using a minus sign. After each command, the modified voter list is printed. Note that the voter lists are output from printing an ArrayList. (Remember, the toString method in the Voter class allows each voter to be output as a string concatenating the voter ID, a space, and the name. In order to accomplish this, you will need to take the input string and separate the first character from the rest. (Check the String methods for how to do this.) If the first character is “+”, then you use the remaining string as the name to create a new Voter instance. If the first character is “-”, then you use the remaining string to get the name of the voter to remove. If the first character is neither of these, you simply return the string “No action taken” and do nothing. When the user enters nothing, then the interaction stops. Adding voters should be easy. Be sure that you are creating voter instances and adding those. You could create a voter instance and add it to the ArrayList in a single statement. Don’t just add the names as an ArrayList of Strings. Removing voters is harder. You might want to write a method to handle removing voters. Here are some things to think about: The information you get about what to remove is the name, so you will have to search the voter ArrayList for a voter that matches the name. If there isn’t a match, then you do nothing. If there is a match, then you have to remove the element at that position.  Be careful about removing the last item in the ArrayList. Some things you might try could generate errors when dealing with the last part of the ArrayList. Be careful about not trying to remove from an empty list. If there is nothing in the ArrayList but it gets a command to remove something, it should not crash.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter18: Stacks And Queues
Section: Chapter Questions
Problem 16PE: The implementation of a queue in an array, as given in this chapter, uses the variable count to...
icon
Related questions
Question
  • Use the ArrayList class
    • Add and remove objects from an ArrayList
    • Protect from index errors when removing
  • Practice with input loop

Details:

This homework is for you to get practice adding and removing objects from an ArrayList.

  1. The Voter class was used to create instances of Voters which held their name and a voter identification number as instance variables, and the number of instances created as a static variable. This was the class diagram:

 

The constructor takes a string, passed to the parameter n, which is the name of the voter and which should be assigned to the name instance variable. Every time a new voter is created, the static variable nVoters should be incremented. Also, every time a new voter is created, a new voterID should be constructed by concatenating the string “HI” with the value of nVoters and the length of the name. For example, if the second voter is named “Clark Kent”, then the voterID should be “HI210” because 2 is the value of nVoters and 10 is the number of characters in “Clark Kent”. If the third voter is named “Peter Parker”, then the voterID should be “HI312” because 3 is the value of nVoters and 12 is the number of characters in “Peter Parker”. Note that you will have to use a method to find the length of the names since you can not predict what will be typed.

The static method getNVoters returns the value of nVoters. The instance method toString returns a concatenation of voterID and name, separated by a space.

  1. Create a main method that uses the Voter class to create voters. It will also use the Scanner class to get each voter’s name from the keyboard. A big difference from Assignment 05 is that the scanner will ask for a command. The user will be able to command the code to both add and remove voters. Here is an example of an interaction (user input is in bold).

    Command: +Peter Parker
    Voter List: [HI112 Peter Parker]
    Command: +Carol Danvers
    Voter List: [HI112 Peter Parker, HI213 Carol Danvers]
    Command: +Clark Kent
    Voter List: [HI112 Peter Parker, HI213 Carol Danvers, HI310 Clark Kent]
    Command: -Peter Parker
    Voter List: [HI213 Carol Danvers, HI310 Clark Kent]
    Command: ?Bad input
    No action taken.
    Voter List: [HI213 Carol Danvers, HI310 Clark Kent]
    Command: -Clark Kent
    Voter List: [HI213 Carol Danvers]
    Command: -Batman
    Voter List: [HI213 Carol Danvers]
    Command: -Carol Danvers
    Voter List: []
    Command:
    Done

    In this interaction, voters are added using a plus sign and voters are removed by using a minus sign. After each command, the modified voter list is printed. Note that the voter lists are output from printing an ArrayList. (Remember, the toString method in the Voter class allows each voter to be output as a string concatenating the voter ID, a space, and the name.

    In order to accomplish this, you will need to take the input string and separate the first character from the rest. (Check the String methods for how to do this.) If the first character is “+”, then you use the remaining string as the name to create a new Voter instance. If the first character is “-”, then you use the remaining string to get the name of the voter to remove. If the first character is neither of these, you simply return the string “No action taken” and do nothing.

    When the user enters nothing, then the interaction stops.

Adding voters should be easy. Be sure that you are creating voter instances and adding those. You could create a voter instance and add it to the ArrayList in a single statement. Don’t just add the names as an ArrayList of Strings.

Removing voters is harder. You might want to write a method to handle removing voters. Here are some things to think about:

  • The information you get about what to remove is the name, so you will have to search the voter ArrayList for a voter that matches the name. If there isn’t a match, then you do nothing. If there is a match, then you have to remove the element at that position. 
  • Be careful about removing the last item in the ArrayList. Some things you might try could generate errors when dealing with the last part of the ArrayList.
  • Be careful about not trying to remove from an empty list. If there is nothing in the ArrayList but it gets a command to remove something, it should not crash.

 

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Arrays
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
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning