Explanation of Solution
Modified definition of “ListNode” class in Listing 12.4:
The modified definition of “ListNode” class in Listing 12.4 is given below:
//Define class "ListNode2"
public class ListNode2<E>
{
//Declare variable "data" and "link" in type parameter
private E data;
private ListNode2<E> link;
//Create default constructor for "ListNode2"
public ListNode2()
{
link = null;
data = null;
}
/* Create parameterized constructor for "ListNode2" */
public ListNode2(E newData, ListNode2<E> linkValue)
{
/* Assign value to "data" and "link" */
data = newData;
link = linkValue;
}
//Accessor method for assign value to data
public void setData(E newData)
{
data = newData;
}
//Mutator method for return data
public E getData( )
{
;&#x...
Want to see the full answer?
Check out a sample textbook solutionChapter 12 Solutions
Java: An Introduction to Problem Solving and Programming (8th Edition)
- Would you please help me to create the my elif under the class OrderedList? I am not really so sure on what to code if the node has already a value. Thank you. class Node: def __init__(self, data): self.data = data self.next = None class OrderedList: list = [] head = Node(None) tail = Node(None) prev = Node(None) cur = Node(None) def insert(self, val): self.newNode = Node(val) if self.head.data == None: self.head = self.newNode self.tail = self.newNode self.list.append(self.newNode.data) elif self.head.data != None: self.head = self.newNode self.tail = self.newNode self.list.append(self.newNode.data) #main myList = OrderedList() myList.insert(10)arrow_forwardYou are familiar with ArrayList and have used it in different capacities. The most prominent feature of ArrayList is that it expands automatically as elements are added to it. It can be traversed using a for loop, for each loop and iterator( which you have implemented in Lab 2).In this lab you are going to implement your own ArrayList and its functionalities.Task 1.1: Create a class Generic ArrayList with attributes.Task 1.2: Implement a constructor for your ArrayList class.Task 1.3: Implement add() method for adding elements to ArrayList. Remember, ArrayList adds elements dynamically. It doesn't depend on user defined size.Task 1.4: Implement remove() method for removing an element from a specific index. Remember, ArrayList stores elements in a consecutive sequence. If an element is removed from middle you have to re-adjust the elements in the correct sequence. Note: Break down your implementation by defining helper methods for different tasks, rather than implementing your logic in a…arrow_forwardTo demonstrate working with ArrayLists, we will be working with four source files: a Dessert class, two classes that extend Dessert called IceCream and Cake, and a TestDessert class. The TestDessert class contains a main() method that declares an ArrayList to hold Dessert objects. This ArrayList is referenced by the variable named "list". Examine TestDessert's main() method and notice the four comments. You are to implement code that accomplishes the tasks described in each step. For step 1, you are to populate the ArrayList with 10 IceCream and Cake objects. These objects should be inserted into the ArrayList at random. This means each run of the program should produce an ArrayList with different proportions of IceCream and Cake objects. After this operation, the ArrayList should contain 10 total objects (IceCream and Cake objects). Display the ArrayList after the operation. For step 2, if the first and last dessert in the ArrayList are different (one is IceCream and the other is…arrow_forward
- Write a method called addSmallest to be considered in a class outside the KWArrayList class. This method will have a parameter list of class type KWArrayList. The method will add the smallest element of the list, at the end of the list. Method heading: public static void addSmallest (KWArrayList list) Before run: 3 5 100 40 67 22 After run: 3 5 100 40 67 22 3arrow_forwardYou are going to implement a program that creates an unsorted list by using a linked list implemented by yourself. NOT allowed to use LinkedList class or any other classes that offers list functions. It is REQUIRED to use an ItemType class and a NodeType struct to solve this homework. The “data.txt” file has three lines of data 100, 110, 120, 130, 140, 150, 160 100, 130, 160 1@0, 2@3, 3@END You need to 1. create an empty unsorted list 2. add the numbers from the first line to list using putItem() function. Then print all the current keys to command line in one line using printAll(). 3. delete the numbers given by the second line in the list by using deleteItem() function. Then print all the current keys to command line in one line using printAll().. 4. putItem () the numbers in the third line of the data file to the corresponding location in the list. For example, 1@0 means adding number 1 at position 0 of the list. Then print all the current keys to command line in one…arrow_forwardWrite a method called addBiggest to be considered in a class outside the KWArrayList class. This method will have a parameter list of class type KWArrayList. The method will add the biggest element of the list, at the end of the 1ist. Method heading: public static void addBiggest (KWArrayList list). Before run: 35 100 40 67 22 After run: 3 5 100 40 67 22 100arrow_forward
- Javaarrow_forwardA Maze Room : In this lab, we will make a maze game. The maze is based on Linked Lists. Instead of having one possible direction (next), we will have 4 possible directions. Rooms: Use the below code as a basis to build your own room class. Implement this class in the file room.py A room will be the basic object for our maze game. A room can have 4 doors (pertaining to north, south, east, and west). Attached to each these directions we have either another room or None (we could also imagine that the None doors are just walls). We want the player to be able to tell what room they are in. Each room will have a unique description. When the player enters a room, the program will describe the room. This way the player will know if they went back to a room that have already been to. You MAY NOT change the method's arguments/names in ANY way. class Room():def __init__(self, descr):#Description of the room to print out#These should be unique so the player knows where they areself.descr =…arrow_forwardQuestion 7 Which of the following class implements the List interface? Collection AbstractList Listlterator ArrayList«E>arrow_forward
- 2. Revise the genericStack class we did in the class to implement it using an array instead of ArrayList. You need to check the array size before adding a new element. If it is full, create a new array that double the current size and copy the elements from the current to the new array and then add the new array. (2 points) 3. Use the genericStack class we did the class (not the one in question 2), implement the binary search public static <E extends Comparable<E>) int binarySearch (E[] list, E key) and a method that returns the maximum value element public static <E extends Comparable<E>) E max(E[] list (2 points)arrow_forwardWhat is the difference between the ArrayList class and the LinkedList class?arrow_forwardIn this java assignment, we will need to use arraylists so we can store data for an employee. If the user wants to enter data for more than one employee, it should print like this: Employee ListId Name ----------------------- 1. First Name 2. Bob Smith If the user only enters one employee, it should print out a paystub for that employee that would look like this (with overtime coming into play as well, paying an extra 50% beyond 40 hours): ------------------------------- Id - 1 Name - First M. Last Address - 1234 Main St, City, St, Zip Phone - 1234567890 Email - sample@gmail.com Hours worked - 45 Hourly Rate - $10 Regular Pay, 40 hours at $10/hr - $400 Overtime Pay, 5 hours at $15/hr - $75 Gross Total - $475 ------------------------------ Federal tax (20%) - $95 State Tax (5%) - $23.75 Fica Tax (3%) - $14.25 ------------------------------ Net Check - $342 The program should have one class per .java file all calling to a main.java file using getters and setters. Here are the…arrow_forward
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT