Java: An Introduction to Problem Solving and Programming (8th Edition)
8th Edition
ISBN: 9780134462035
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 12.2, Problem 13STQ
Program Plan Intro
“HashSet” class:
- This class is able to store a set of objects. It also implements the “Collection” interface.
- This class stores a set instead of list of items.
- It means that there can be no duplicate elements.
- The class is named “HashSet” since the
algorithm used to implement the set is called a hash table. - The package for “HashSet” class is “import java.util.HashSet;”
Syntax for defining “HashSet” class:
HashSet<Base_type> variable_name = new HashSet<Base_type>();
From the above syntax, “Base_type” represents “Integer”, “Double” or “String”.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Write code to create three instances of a
HashMap class. Give two the same, and
the third different data. Compare the
objects and state the result
Which of the following statements are true about a HashSet ? Select all that
apply.
Hint: you should be able to answer this question based only on the material
presented in class.
If E inherits Object.hashCode(), then there will be no collisions.
A HashSet iterator should iterate through the Hashset elements in the order in
which they were added to the Hashset.
Two HashSets are equal (according to the equals() method) if they have the
same size and their elements can be paired such that the pairs are equal
according to the E.equals() method.
OA HashSet could be implemented using a HashMap namely the key is
the same as the value.
Java Code: Add a HashMap to your Lexer class and initialize all the keywords. Change your lexer so that it checks each string before making the WORD token and creates a token of the appropriate type if the work is a key word. When the exact type of a token is known, you should NOT fill in the value string, the type is enough. For tokens with no exact type we still need to fill in the token’s string. Rename “WORD” to “IDENTIFIER”.
Strings and characters will require some additions to your state machine. Create “STRINGLITERAL” and “CHARACTERLITERAL” token types. These cannot cross line boundaries.
Your lexer should throw an exception if it encounters a character that it doesn’t expect outside of a comment, string literal or character literal. Create a new exception type that includes a good error message and the token that failed. Ensure that the ToString method prints nicely.
Add “line number” to your Token class. Keep track of the current line number in your lexer and populate each…
Chapter 12 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
Ch. 12.1 - Suppose aList is an object of the class...Ch. 12.1 - Prob. 2STQCh. 12.1 - Prob. 3STQCh. 12.1 - Prob. 4STQCh. 12.1 - Can you use the method add to insert an element at...Ch. 12.1 - Prob. 6STQCh. 12.1 - Prob. 7STQCh. 12.1 - If you create a list using the statement...Ch. 12.1 - Prob. 9STQCh. 12.1 - Prob. 11STQ
Ch. 12.1 - Prob. 12STQCh. 12.2 - Prob. 13STQCh. 12.2 - Prob. 14STQCh. 12.2 - Prob. 15STQCh. 12.2 - Prob. 16STQCh. 12.3 - Prob. 17STQCh. 12.3 - Prob. 18STQCh. 12.3 - Prob. 19STQCh. 12.3 - Write a definition of a method isEmpty for the...Ch. 12.3 - Prob. 21STQCh. 12.3 - Prob. 22STQCh. 12.3 - Prob. 23STQCh. 12.3 - Prob. 24STQCh. 12.3 - Redefine the method getDataAtCurrent in...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.3 - Repeat Question 25 for the method...Ch. 12.4 - Revise the definition of the class ListNode in...Ch. 12.4 - Prob. 30STQCh. 12.5 - What is the purpose of the FXML file?Ch. 12.5 - Prob. 32STQCh. 12 - Repeat Exercise 2 in Chapter 7, but use an...Ch. 12 - Prob. 2ECh. 12 - Prob. 3ECh. 12 - Repeat Exercises 6 and 7 in Chapter 7, but use an...Ch. 12 - Write a static method removeDuplicates...Ch. 12 - Write a static method...Ch. 12 - Write a program that will read sentences from a...Ch. 12 - Repeat Exercise 12 in Chapter 7, but use an...Ch. 12 - Write a program that will read a text file that...Ch. 12 - Revise the class StringLinkedList in Listing 12.5...Ch. 12 - Prob. 12ECh. 12 - Write some code that will use an iterator to...Ch. 12 - Prob. 14ECh. 12 - Write some code that will use an iterator to...Ch. 12 - Prob. 17ECh. 12 - Revise the method selectionSort within the class...Ch. 12 - Repeat the previous practice program, but instead...Ch. 12 - Repeat Practice Program 1, but instead write a...Ch. 12 - Write a program that allows the user to enter an...Ch. 12 - Write a program that uses a HashMap to compute a...Ch. 12 - Write a program that creates Pet objects from data...Ch. 12 - Repeat the previous programming project, but sort...Ch. 12 - Repeat the previous programming project, but read...Ch. 12 - Prob. 9PPCh. 12 - Prob. 10PPCh. 12 - Prob. 11PPCh. 12 - Prob. 12PPCh. 12 - Prob. 13PPCh. 12 - Prob. 14PPCh. 12 - Prob. 15PP
Knowledge Booster
Similar questions
- Modify the Instructor class below by overriding the hashCode and equals methods. Then write a class that stores several Instructor objects in a HashSet. The class should be able to display all the instructors in the set, and allow the user to search for an instructor. Demonstrate the class in an application. Below is the instructor class. ============================================================== /** This class stores data about an instructor.*/public class Instructor{ private String lastName; // Last name private String firstName; // First name private String officeNumber; // Office number /** This constructor initializes the last name, first name, and office number. @param lname The instructor's last name. @param fname The instructor's first name. @param office The office number. */ public Instructor(String lname, String fname, String office) { lastName = lname; firstName = fname; officeNumber = office; } /** The copy constructor initializes the object as a copy of another…arrow_forward1. The following interfaces are part of the Java Collections Framework except? a. SortedListb. SortedMapc. Setd. ArrayListe. HashSet 2. Which assertion about the HashMap class is not true? a. HashMap class is not synchronized.b. HashMap class extends AbstractMap and implements Map interface.c. HashMap maintain order of its element.d. HashMap uses a hashtable to store the map.arrow_forwardConsider records for patients at a medical facility. Each record contains an integer identification for a patient and strings for the date, the reason for the visit, and the treatment prescribed. Design and implement the class PatientRecord so that it overrides the method hashCode. Write a program that tests your new class. Design a class PatientDataBase that stores instances of PatientRecord, as described above. The class should provide at least three query operations, as follows. Given a patient identification and date, the first operation should return the reason for the visit, and the second operation should return the treatment. The third query operation should return a list of dates, given a patient identification.arrow_forward
- Consider records for patients at a medical facility. Each record contains an integer identification for a patient and strings for the date, the reason for the visit, and the treatment prescribed. Design and implement the class PatientRecord so that it overrides the method hashCode. Write a program that tests your new class. Design a class PatientDataBase that stores instances of PatientRecord, as described above. The class should provide at least three query operations, as follows. Given a patient identification and date, the first operation should return the reason for the visit, and the second operation should return the treatment. The third query operation should return a list of dates, given a patient identification. Implement the class PatientDataBase. Using a hash table to store the patient records. Write a program that demonstrates and tests this class. JAVA PROGRAMarrow_forwardConsider records for patients at a medical facility. Each record contains an integer identification for a patient and strings for the date, the reason for the visit, and the treatment prescribed. Design and implement the class PatientRecord so that it overrides the method hashCode. Write a program that tests your new class. Design a class PatientDataBase that stores instances of PatientRecord, as described above. The class should provide at least three query operations, as follows. Given a patient identification and date, the first operation should return the reason for the visit, and the second operation should return the treatment. The third query operation should return a list of dates, given a patient identification. Implement the class PatientDataBase. Use a hash table to store the patient records. Write a program that demonstrates and tests this class.arrow_forward. Write a code using the following hints and apply HashMap<K, V> and its methods get and put for public class CourseInformation (i) Create a HashMap object called Course to add a key and its value. //Key is your course name and value is your course code. (ii) Use put method to map a course name with the course code. // add any FOUR COURSE NAMES WITH ITS COURSE CODE which are represented as a String (iii) Use get method to find the course code for any one of the course.arrow_forward
- 1: Create a new class with attributes name and address. Both data type will be String. Create a constructor and assign the parameters to the attributes. Now override the toString() method. Is this possible? 2: Create a class with the main method and create an instance of the class that you created in Test Stem 1. What method can you call to get the hashCode of your class? 3: What is the return type of the hash code ? 4: Create an instance of the string in your test class and assign a value. Which of the following operators can be used in conjunction with a string object? + - & . += 5: Supposed that we have a string with a value “This is my string.”. Which method can be used to extract a specific word from the given string? 6: Supposed that we have a string with a value “This is my string.”. Which expression can be used to extract the word “my” from the given string? 7: Now create an String and assign a value with spaces in the beginning, end and middle. Try out the method…arrow_forwardIs the following declaration of a HashMap valid: HashMap nun new HashMap(); True Falsearrow_forwardla) Declare and instantiate a HashMap object named hMap whose values are Integers and keys are Strings. 1b) Write a for-each loop to find the total of the values in this HashMap.arrow_forward
- Java Objects and Linked Data: Select all of the following statements that are true. The Java Class Library's class ArrayList implements the LinkedList interface. In Java, when one reference variable is assigned to another reference variable, both references then refer to the same object. In Java, the "equals"-method that any object inherits can be overwritten to compare objects field by field.arrow_forwardThe putIfAbsent function in HashMap seems useless.arrow_forwardDefine a class named Element with the following private data fields: String name; // The name of the chemical element int atomicNumber; // The atomic number of the element double atomicWeight; // The atomic weight of the element Override the toString() and equals() methods for the class with methods you design. Define a Java HashMap named elements that uses the String value of a chemical symbol as the key and an instance of the Element class as the value. For example, the chemical symbol for Carbon is the letter “C”. The atomic number of Carbon is 6, and the atomic weight of Carbon is 12.011. To add Carbon to the HashMap named elements, first create an instance of the Element class using “Carbon”, 6, and 12.011. Then add an entry to the HashMap collection using “C” as the key and the Element object as the value. Write a menu-driven program that prompts your user with the following six choices: Select an option number from the following menu: Option Action ------…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
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)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education