Instructor Solutions Manual For Introduction To Java Programming And Data Structures, Comprehensive Version, 11th Edition
11th Edition
ISBN: 9780134671581
Author: 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
Write a method called swapItems 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 swapItems(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 7
java
Write the following method in Java:
public void reverse()
The method will reverse the linked list. Head will point to the last node of the linked list and all
the pointers (addresses) will be reversed. The last node (a new head) will point to the second last
node and so on. The old head node will become the tail node in the reversed LinkedList.
Use the LinkedList class discussed in the video lecture and write the reverse() method in the same
class.
Test your method in the main method.
Following picture depicts the reversal of the LinkedList.
NULL
Head
I
V
R
I
↑
Head
> NULL
Chapter 24 Solutions
Instructor Solutions Manual For 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
- Write a (JAVA program language) to do the following task: Input n from user. Create an array to store n double values. Initialize it with n random numbers ranging from 10.0 to 100.0 (both included). Pass the array to a method that calculates the sum of those n numbers. Print the sum in the method. NOTE VERY IMPORTANT: DONT USE (linked list, data structure concepts or exception handling)arrow_forward4. For a singly linked list, each node has data attribute as well as next. head is given as a reference to the head Node. Write a method void deleteNotes(int k) in the Linkedlist class that deletes the first k nodes from the linkedlist. You may assume that k-1 corresponds to removing the head node. The method must throw an exception if k exceeds the size of the list.arrow_forwardWrite a method max() which take a generic singly linked list as an argument and returns the maximum element reference in the list. If the list is empty, please throw an empty collection exception. The method prototype is defined as follows. public static > T max(SingleLinkedList list) throws EmptyCollectionException You can use any of public methods in these following three classes: 1. The SingleLinkedList class diagram is as follows: SingleLinkedNode 1-SingleLinkedNodenext -T element O• SingleLinkedNodeg 9+SinglelinkedNode(T elem) +SingleLinkedlNode first SingleLinkedNode last int numNodes +SingleLinkedList) +void addFirst(T element)arrow_forward
- Exercise 3: Provide a static method that checks whether a generic array list is a palindrome; that is, whether the values at index i and n- 1- i are equal to each other, where n is the size of the array list. Write a tester class to check your method.arrow_forwardJ a vaarrow_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_forward
- Write a Java program that uses ArrayList and Iterator. It should input from user the names and ages of your few friends in a loop and add into ArrayList. Finally, it should use an Iterator to display the data in a proper format. ( Sample run of the program:-) List of my Friends Enter name and age [friend# 0] Khalid Al-shamri 22.5 Do you want to add another friend (y/n)? y Enter name and age [friend# 1] Rahsed Al-anazi 21.1 Do you want to add another friend (y/n)? y Enter name and age [friend# 2] Salem Al-mutairi 23.7 Do you want to add another friend (y/n)? n Here is the data you entered: 0. Khalid Al-shamri, 22.5 1. Rahsed Al-anazi, 21.1 2. Salem Al-mutairi, 23.7arrow_forwardJAVA PROGRAM MODIFY THIS PROGRAM SO IT READS THE TEXT FILES IN HYPERGRADE. I HAVE PROVIDED THE INPUTS AND THE FAILED TEST CASE AS A SCREENSHOT. HERE IS THE WORKING CODE TO MODIFY: import java.io.*;import java.util.*;public class NameSearcher { private static List<String> loadFileToList(String filename) throws FileNotFoundException { List<String> namesList = new ArrayList<>(); File file = new File(filename); if (!file.exists()) { throw new FileNotFoundException(filename); } try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { String line = scanner.nextLine().trim(); String[] names = line.split("\\s+"); for (String name : names) { namesList.add(name.toLowerCase()); } } } return namesList; } private static Integer searchNameInList(String name, List<String> namesList) {…arrow_forwardProvide a different implementation of ChoiceQuestion. Instead of storing the choices in an array list, the addChoice method should add the choice to the question text. For this purpose, an addLine method has been added to the Question class. Use the following files: Question.java /** A question with a text and an answer.*/public class Question{ private String text; private String answer; /** Constructs a question with empty text and empty answer. */ public Question() { text = ""; answer = ""; } /** Sets the answer for this question. @param correctResponse the answer */ public void setAnswer(String correctResponse) { answer = correctResponse; } /** Checks a given response for correctness. @param response the response to check @return true if the response was correct, false otherwise */ public boolean checkAnswer(String response) { return response.equals(answer); } /** Add a line of text to…arrow_forward
- how to answer the following question in java screen shot shows the ABList class Suppose a list names contains 8 elements. A call to names.set(8, "Albert") results in: A. an exception being thrown. B. an 8 element list with "Albert" as the last element. C. a 9 element list with "Albert" as the last element. D. a 9 element list with "Albert" as the next to last element. The only index-related method of the ABList class that can make good use of the protected find method is: A. add B. set C. get D. indexOf E. remove An application can indicate that it wants to use the "natural order" to order the elements of a SortedABList list by: A. passing the constructor an appropriate compareTo method. B. using the parameter-less constructor. C. using the standard list iteration approach. D. always adding elements to the front of the list. Suppose a list names contains 8 elements. A call to names.add(9, "Albert") results in: A. an exception being…arrow_forwardSolve exercise in Javaarrow_forwardWrite the full Java code for Searcher.javaarrow_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