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
Concept explainers
Question
Chapter 12, Problem 14PP
Program Plan Intro
Remove duplicates names from file
- Import required package.
- Define “InvitationList” class.
- Define main function.
- Create “HashSet” for names.
- Declare required variables.
- Assign scanner object to “null”.
- Inside the “try” block,
- Define “namesList” file using “FileInputStream”.
- Check condition using “while” loop.
- Read name from file.
- Check if the name is in the set or not. If the name is in the set already, then ignore it. If the name is not contains in the set, then add the given name to “names_set”.
- Close the file.
- Display statement.
- Display all names in set using “for” loop
- Catch the exception using “catch” keyword.
- Display given error and exit the program.
- Define main function.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
JAVA
Create a new class HashSetManipulation with a main method.
Create a new HashSet set and populate it with initial values "A", "B", and "C" using the add method.
Print the size of the set using set.size().
Use a for loop to print the values in the set.
Use the add method to add a new value "D" to the set and print the result of the set to show that the value has been added.
Use the remove method to remove value "A" from the set and print the result of the set to show that the value has been removed.
Use the contains method to check if the value "C" exists in the set and print the result.
In JAVA, use a HashMap to count and store the frequency counts for all the words from a large text document. Then, display the contents of this HashMap with given words and their frequency in the document.
Next, please create a set view of the Map and store the contents in an array. Sort this array based on key value and display it.
Finally, sort the array in decreasing order by frequency and display it.
Write a Java application CountryList. In the main method, do the following:1. Create an array list of Strings called countries.2. Add "Canada", "India", "Mexico", "Peru" in that order.3. Use the enhanced for loop to print all countries in the array list, one per line.4. Add "Spain" at index 15. Replace the element at index 2 with "Vietnam". You must use the set method.6. Replace the next to the last element with "Brazil". You must use the set method. You willlose one point if you use 3 in the set method. Do this in a manner that would replace thenext to the last element, no matter the size of the array list.7. Remove the object "Canada" Do not remove at an index. Your code should work if"Canada" was at a different location. There is a version of remove method that willremove a specific object.8. Get and print the first element followed by "***"9. Call method toString() on countries to print all elements on one line.10. Use the enhanced for loop to print all countries in the array list,…
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
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
- Write a program that uses a HashMap to compute a histogram of positivenumbers entered by the user. The HashMap’s key should be the numberthat is entered, and the value should be a counter of the number of timesthe key has been entered so far. Use −1 as a sentinel value to signal theend of user input. For example, if the user inputs:512355321-1then the program should output the following (not necessarily in this order):The number 3 occurs 2 times.The number 5 occurs 3 times.The number 12 occurs 1 times.The number 21 occurs 1 times.arrow_forwardThis problem is based on Hashing. Please use Python and do not make it overly lengthy and complicated making it difficult to understand. PLEASE USE PYTHON and do not make the code too difficult to understand. Keep it simple. Thanks Given an array containing Strings, you need to write a code to store them in a hashtable. Assume that the Strings contain a combination of capital letters and numbers, and the String array will contain no more than 9 values. (total number of consonants*24 + summation of the digits) %9. In case of a collision, use linear probing.For a String “ST1E89B8A32”, it’s hash function will produce the value=(3*24+(1+8+9+8+3+2))%9=4, hence it will be stored in index 4 of the hash table.arrow_forwardOne use of a hash table is to implement a set data type. You will implement the methods addElement, find, toString, resize, and the MySetIterator inner class in the class MySet. MySet uses a separate chaining hash table to implement a set of integers. To get started, import the starter file, MySet.java into the hashset package you create in a new Java Project. Please do not change any of the method signatures in either class. Implement the methods described below. You are free to test your code however you prefer. Below is method Signature class: package hashset; import java.util.Iterator; public class MySet { // implements a set using a separate chaining hash table private class Node { private Integer element; private Node next; private Node(Integer e, Node n) { element = e; next = n; } } private Node table[]; //an array of linked list private int tableSize; //current number of lists in the table private int…arrow_forward
- One use of a hash table is to implement a set data type. You will implement the methods addElement, find, toString, resize, and the MySetIterator inner class in the class MySet. MySet uses a separate chaining hash table to implement a set of integers. To get started, import the starter file, MySet.java into the hashset package you create in a new Java Project. Please do not change any of the method signatures in either class. Implement the methods described below. You are free to test your code however you prefer. For additional programming practice, you can implement the union, intersect, difference methods (see the comments in the code for the desired functionality). private void resize()This method "doubles" the table size and reinserts the values stored in the current table. The table size should remain prime. private boolean find(Integer e)This method returns true if the integer e is in the set and false otherwise. private void addElement(Integer e)If e is not in the set, add e to…arrow_forwardPlease CODE in java ! Keep the code neat. Add comments. No need for long comments. If you have answered this question before please dont copy and paste the same answer you did for the other person. Need this tonight before 11 pm EST. Thanks !arrow_forwardIn this assignment, you will compare the performance of ArrayList and LinkedList. More specifically, your program should measure the time to “get” and “insert” an element in an ArrayList and a LinkedList.You program should 1. Initializei. create an ArrayList of Integers and populate it with 100,000 random numbersii. create a LinkedList of Integers and populate it with 100,000 random numbers2. Measure and print the total time it takes to i. get 100,000 numbers at random positions from the ArrayList 3. Measure and print the total time it takes to i. get 100,000 numbers at random positions from the LinkedList 4. Measure and print the total time it takes to i. insert 100,000 numbers in the beginning of the ArrayList 5. Measure and print the total time it takes to i. insert 100,000 numbers in the beginning of the LinkedList 6. You must print the time in milliseconds (1 millisecond is 1/1000000 second).A sample run will be like this:Time for get in ArrayList(ms): 1Time for get in…arrow_forward
- For this lab, you will need to use the files BagInterface.java, Item.java, and LinkedBag.java.arrow_forwardPLEASE MAKE IT RUN LIKE THE EXPECTED PICTUREarrow_forwardWrite a Java program that asks the user to enter items until the user chooses to stop, stores them into links (nodes), and chains the links together into a linked list. You will need to make the following changes: Your linked list should store information about Customers' Transactions that contain three fields: customer name (string), customer number (int), and transaction description (string). Instead of putting all of your code inside the main source code file, you should create the following functions: o addToStart(Item x) o addToEnd(Itemn x) o insertAt(Item x, int n) o remove(int customer number) o reverse() o printList() • Instead of simply asking the user if they want to add a new link, your main program should offer the user a menu of options: o Add a new link to the end o Add a new link to the beginning o Insert an element at index n into the list. 0 Remove a link from the list o Reverse the list o print out the entire list o Quit the programarrow_forward
- Create an ArrayList of strings to store the names of celebrities or athletes. Add five names to the list. Process the list with a for loop and the get() method to display the names, one name per line. Pass the list to a void method. Inside the method, Insert another name at index 2 and remove the name at index 4. Use a foreach loop to display the arraylist again, all names on one line separated by asterisks. After the method call in main, create an iterator for the arraylist and use it to display the list one more time.arrow_forwardCreate a Java program. You have a list of Student ID's followed by the course number(separated by aa space) that the student is enrolled in. The listing is in no particular order. For eg. is student 1 is in CS100 and CS200 while student 2 is in CS105 and MATH210 then the list may look like this: 1 CS100 2 MATH210 2 CS105 1 CS200 Write a program that reads data in this format from the console. If the ID is -1 then stop inputting data. Use the HashMap class to map from an Integer(Student ID) to an ArrayList of type String that holds each class that the student is enrolled in. The declaration should look like this: HashMap<Integer, ArrayList<String>> students = new HashMap<Integer, ArrayList<String>>(); After all data is input, iterate through the map and output the student ID and all classes stored in the vector for that student. The result should be a list of classes organized by student id.arrow_forward: Imagine you are reading in a stream of integers. Periodically, you wish to be able to look up the rank of a number x (the number of values less than or equal to x). Implement the data structures and algorithms to support these operations. That is, implement the method track(int x), which is called when each number is generated, and the method getRankOfNumber(int x), which returns the number of values less than or equal to x (not including x itself). EXAMPLE Stream (in order of appearance): 5, 1, 4, 4, 5, 9, 7, 13, 3 getRank Of Number (1) = 0 getRankOf Number (3) = 1 getRankOfNumber (4) = 3arrow_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