Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Expert Solution & Answer
Chapter 24.3, Problem 24.3.7CP
Explanation of Solution
Given “get (index)” method:
The following statements will define the “get (index)” method:
//definition of “get” method
public E get (int index)
{
//call the method
checkIndex (index);
//return statement
return data [index];
}
Here, this method calls the “checkIndex (index)” (listing 24.2 in line 59 to 63) to throw an “IndexOutOfBoundsException”, if the index is out of bounds.
Given “add (index, e)” method:
The following statements will define the “add (index, e)” method:
//definition of “add” method
public void add (int index, E e)
{
//call the method
checkIndex (index);
//check the condition
if (index < 0 || index > size)
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
We wish to build a Hash Table, which is able to deal with collision by using linear and quadratic
probing. Please write a public class HashLinQuad as follows:
public class HashLinQuad {
private int[] table; //hash table
private int size; //current number of elements
private int capacity //capacity of the hash table
private HashLinQuad (int size){
table = new int[size];
}
public int addLin (int obj) {
… //add obj into table using linear probing
}
public int addQuad (int obj){
… //add obj into table using quadratic probing
}
…
}
1. Implement the two methods addLin and addQuad that apply linear and quadratic probing,
respectively.
2. Test your implemented methods addLin and addQuad . To this end, create the following
two objects in class
Main :
HashLinQuad linHashTable = new HashLinQuad(1249);
HashLinQuad quadHashTable = new HashLinQuad(1249);
3. Please insert the same 1000 random integer values (in the same order) into:
a. the object `linHashTable` by using the method `addLin` and count…
What is the best way to implement a stack or a queue so that it can hold an arbitrary number of elements? Select one: a. Using an array, and throwing an exception when the stack or queue is full. b. By creating a bigger array when the stack or queue is full, and copying the elements from the original array. c. Using linked lists to store the collection of elements.
- In class HashTable implement a hash table and consider the following:(i) Keys are integers (therefore also negative!) and should be stored in the tableint[] data.(ii) As a hash function take h(x) = (x · 701) mod 2000. The size of the table istherefore 2000. Be careful when computing the index of a negative key. Forexample, the index of the key x = −10 ish(−10) = (−7010) mod 2000 = (2000(−4) + 990) mod 2000 = 990.Hence, indices should be non-negative integers between 0 and 1999!(iii) Implement insert, which takes an integer and inserts it into a table. Themethod returns true, if the insertion is successful. If an element is already inthe table, the function insert should return false.(iv) Implement search, which takes an integer and finds it in the table. The methodreturns true, if the search is successful and false otherwise.(v) Implement delete, which takes an integer and deletes it form the table. Themethod returns true, if the deletion is successful and false otherwise.(vi)…
Chapter 24 Solutions
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Ch. 24.2 - Prob. 24.2.1CPCh. 24.2 - Prob. 24.2.2CPCh. 24.2 - Prob. 24.2.3CPCh. 24.2 - Prob. 24.2.4CPCh. 24.3 - What are the limitations of the array data type?Ch. 24.3 - Prob. 24.3.2CPCh. 24.3 - Prob. 24.3.3CPCh. 24.3 - What is wrong if lines 11 and 12 in Listing 24.2,...Ch. 24.3 - If you change the code in line 33 in Listing 24.2,...Ch. 24.3 - Prob. 24.3.6CP
Ch. 24.3 - Prob. 24.3.7CPCh. 24.4 - Prob. 24.4.1CPCh. 24.4 - Prob. 24.4.2CPCh. 24.4 - Prob. 24.4.3CPCh. 24.4 - Prob. 24.4.4CPCh. 24.4 - Prob. 24.4.5CPCh. 24.4 - Prob. 24.4.7CPCh. 24.4 - Prob. 24.4.8CPCh. 24.4 - Prob. 24.4.9CPCh. 24.4 - Prob. 24.4.10CPCh. 24.5 - Prob. 24.5.1CPCh. 24.5 - Prob. 24.5.2CPCh. 24.5 - Prob. 24.5.3CPCh. 24.6 - What is a priority queue?Ch. 24.6 - Prob. 24.6.2CPCh. 24.6 - Which of the following statements are wrong?...Ch. 24 - (Implement set operations in MyList) The...Ch. 24 - (Implement MyLinkedList) The implementations of...Ch. 24 - (Use the GenericStack class) Write a program that...Ch. 24 - Prob. 24.5PECh. 24 - Prob. 24.6PECh. 24 - (Fibonacci number iterator) Define an iterator...Ch. 24 - (Prime number iterator) Define an iterator class...
Knowledge Booster
Similar questions
- Hello The java function belw is generating a NullPointerException. How can I improve it? public int quadraticProbing(String[] hashArray, int initial_index, int collision) { int index = (initial_index + (collisions * collisions)) % maxSize; System.out.println("Index " + index); if (index == maxSize) { System.out.println("flag 04"); index = 0; // To wrap around the table } if (hashArray[index] == null) { return index; // We can insert at this location } else { // The index isn't empty so we need to insert the value // at the end of the list } collisions = ++collision; return quadraticProbing(hashArray, initial_index, collision); // Recursive call } Thanks in advance. Respectfully, Emmanuelarrow_forward2. Using the Hashtable class, write a spelling checker program that readsthrough a text file and checks for spelling errors. You will, of course, haveto limit your dictionary to several common words.3. Create a new Hash class that uses an arraylist instead of an array for thehash table. Test your implementation by rewriting (yet again) the computerterms glossary application.arrow_forwardComputer Science ** In JAVA** What would a generic radix sorter look like that computed the same function on Strings as my EXAMPLE (below), that checks true, or throw an exception….? EXAMPLE: StringComparator.java import java.util.Comparator; public class StringComparator implements Comparator { @Override public int compare(String s1, String s2) { return s1.toLowerCase().compareTo(s2.toLowerCase()); } } HeapSort.java import java.util.ArrayList; import java.util.Comparator; public class HeapSort { public void sortingHeapTree(ArrayList arr, Comparator c) { int n = arr.size(); for (int i = n / 2 - 1; i >= 0; i--) HeapTreeConstruction(arr, n, i, c); for (int i = n - 1; i >= 0; i--) { T temp = arr.get(0); arr.set(0, arr.get(i)); arr.set(i, temp); HeapTreeConstruction(arr, i, 0, c); } } void HeapTreeConstruction(ArrayList arr, int n, int i, Comparator c) { int root = i; // Initialize largest as root int l = 2 * i + 1; // left = 2*i + 1 int r = 2 * i + 2; // right = 2*i + 2 if…arrow_forward
- The exercise consists of Hashing. Note you are notallowed to rely on Java library classes in your implementation. Part 3Implement in Java a function that takes as input an integer n 2 and generates n differentstrings all hashing to the same value when the following hash function is used.public int hashCode(String s) { int hash = 0; int skip = Math.max(1, s.length() / 3); for (int i = 0; i < s.length(); i += skip) hash = (hash * 37) + s.charAt(i); return hash;}Assume ASCII characters. and please Briefly describe your implementation .arrow_forwardWrite a method called swapltems to be considered within the SingleLinkedList class that takes two parameters, index1 and index2 of type integer. The method swaps the data of the nodes at the given indexes. If any of the parameters indexes is invalid throw IndexOutOfBoundsException. Do not call any method of class SingleLinkedList in your method. Method heading: public void swapltems(int index1, int index2) Example: index1 = 2 index2 = 4 Before method call: list: 5 10 12 9 11 7 After method call: list: 5 10 11 9 12 7arrow_forwardCreate a test program to test this MergeSort Program: package ArraySplit; import java.util.Arrays; public class ArraySplit { public static void mergeSort(int[] a, int n) { if (n < 2) { return; } int mid = n / 2; int[] l = new int[mid]; int[] r = new int[n - mid]; for (int i = 0; i < mid; i++) { l[i] = a[i]; } for (int i = mid; i < n; i++) { r[i - mid] = a[i]; } mergeSort(l, mid); mergeSort(r, n - mid); merge(a, l, r, mid, n - mid); } public static void merge(int[] a, int[] l, int[] r, int left, int right) { int i = 0, j = 0, k = 0; while (i < left && j < right) { if (l[i] <= r[j]) { a[k++] = l[i++]; } else { a[k++] = r[j++]; } } while (i < left) { a[k++] = l[i++]; } while (j < right) { a[k++] = r[j++]; } } }arrow_forward
- Given the MileageTrackerNode class, complete main() in the MileageTrackerLinkedList class to insert nodes into a linked list (using the insertAfter() method). The first user-input value is the number of nodes in the linked list. Use the printNodeData() method to print the entire linked list. DO NOT print the dummy head node. Ex. If the input is: 3 2.2 7/2/18 3.2 7/7/18 4.5 7/16/18 the output is: 2.2, 7/2/18 3.2, 7/7/18 4.5, 7/16/18 public class MileageTrackerNode { private double miles; // Node data private String date; // Node data private MileageTrackerNode nextNodeRef; // Reference to the next node public MileageTrackerNode() { miles = 0.0; date = ""; nextNodeRef = null; } // Constructor public MileageTrackerNode(double milesInit, String dateInit) { this.miles =…arrow_forwardUsing eclipse, write a Console Java program that inserts 25 random integers in the range of 0 to 100 into a Linked List. (Use SecureRandom class from java.security package.SecureRandom rand = new SecureRandom(); - creates the random number objectrand.nextInt(100) - generates random integers in the 0 to 100 range)Using a ListItreator output the contents of the LinkedList in the original order. Using a ListItreator output the contents of the LinkedList in the reverse order.arrow_forwardin regards to java For this problem, I have a complete linked list program which supports the following commands from the user: insert, display, delete, average, find, insertBefore, insertAfter. insert allows them to insert a number into the current list, display will display the numbers in the current list etc. The program is complete except that I have removed the body of the method for find. Given the following class for the nodes in a linked list: public class Node { ... public Node getNext() {...} // return next field public int getData() {...} // returns data } Assuming that the variable head points to (i.e. contains the address of) the first node of a linked list, write the statements to find the first occurrence of the data value x in the linked list. For example, find(4) would return something like "Found at node position 20" or "Not found" Assume that the nodes in the linked list are counted starting from 1. Your statements will be inserted inside a method like the…arrow_forward
- Create a newconcrete class that implements MyMap using open addressing with double hashing.For simplicity, use f(key) = key % size as the hash function, where sizeis the hash-table size. Initially, the hash-table size is 6. The table size is doubledwhenever the load factor exceeds the threshold (0.5).arrow_forwardA Java Program Write a method called samePosition in a class called StackExt that receives two parameters st1 and st2 of type ArrayStack and a third parameter pos of type int. The method returns true, if stack st1 and st2 contains the same value in position pos. Otherwise, the method returns false. Assume that both stacks st1 and st2 contain same number of elements. Assume that the position of the top element is 0 and increases by 1 for each subsequent element. The elements of both stacks st1 and st2 must remain in the original relative positions in the stacks at the end of the method. Use common stack operations only such as push, pop, peek, isEmpty, constructor and copy constructor. You can create temporary objects of type ArrayStack in your method. Example: top st1: 4 5 3 10 8 st1: 5 9 4 10 1 If the pos is 3, then both stacks contain 10 in position 3. The method returns true. If the pos is 1, then st1 contains 5 and st2…arrow_forwardTo help manage the wizarding world, we need to write a reverse method. For the general purpose, let us write a generic reverse method. Namely, given an array of T objects, the reverse method would use a stack to reverse these T objects and return a new ArrayList with these T objects in reverse order. The generic method prototype is as follows: public <T> ArrayList<T> reverse(T[] wizards) throws Exception; To help reverse Hogwarts students' names, we can invoke the method as follows: reverse({"Harry Potter", "Ronald Bilius", "Hermione Jean Granger"}) , where {"Harry Potter", "Ronald Bilius", "Hermione Jean Granger"} is an array input and it will return an java.util.ArrayList instance, containing "Hermione Jean Granger" as the first element, "Ronald Bilius" as the second element, and "Harry Potter" as the third element. Task 1. Please use a Stack to implement the generic reverse method: public ArrayList reverse(T[] wizards) throws Exception First write…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