Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
Introduction to Java Programming and Data Structures, Comprehensive Version (11th Edition)
11th Edition
ISBN: 9780134670942
Author: Y. Daniel Liang
Publisher: PEARSON
Expert Solution & Answer
Book Icon
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)

Blurred answer
Students have asked these similar questions
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
Complete the method void addFirst(T item). It adds an item of type T to the front of the deque. It must not use any loops or recursion. Each operation must take constant time, that is, it does not depend on the deque's size. Additionally, if the item is null, instead of adding it into the deque, reject and throw an illegal argument exception. Here's the skeleton code // EXERCISE 6.2 ADD NOT NULL TO FRONT /** * Adds an non-null item of type T to the front of the deque. * @param item is a type T object. @throws IllegalArgumentException if the item is null. */ public void addFirst(T item) { }
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
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education