Question: Create a new Java project called Project . To this project you'll be adding several classes: I. GameCharacter Create a class GameCharacter that has a
Create a new Java project called Project. To this project you'll be adding several classes:
I. GameCharacter
Create a class GameCharacter that has a single member variable: a String storing the character's name. In this class, you should have the following methods:
A single-parameter constructor which sets the name.
Override the toString() method from the Object class
Override the equals() method from the Object class
IMPORTANT: in order to correctly override this method, you must use the signature "public boolean equals(Object other)"
II. SinglyLinkedList
Add this SinglyLinkedList class and modify it as follows:
Change it to use generics.
Add a method remove(ValueType e), which takes an element e to remove.
It should remove the first node whose value is equal to e. (Hint: use the equals()method)
This method should return true if a node was removed. (e was in the list)
This method should return false if no node was removed. (e was not in the list)
Code:
public class SinglyLinkedList { private Node head; private int size; public SinglyLinkedList() { head = null; size = 0; } public void insertAtHead(int v) { //Step 1: Create a new node //Step 2: set the next of the new node to head Node newNode = new Node(v, head);
//Step 3: Change head reference to new node head = newNode; //Step 4: increase size of list size++; } public String toString() { //Case 1: list is empty if(head == null) { return "The list is Empty!"; } //Case 2: list is not empty String result = ""; Node temp = head; while(temp != null) { result += temp.toString() + " "; temp = temp.next; } return result; } /** * * @return the value at the head of the list which is getting removed */ public int removeHead() { if(head == null) { throw new IllegalStateException("List is empty"); } int value = head.value; head = head.next; size--; return value; } private class Node { private int value; private Node next; public Node(int v, Node n) { value = v; next = n; } public String toString() { return "" + value; } }
}
III. DoublyLinkedList
Add this DoublyLinkedList class to the project. This class already uses generics, so you only need to add the remove method, which is the same as in a singly linked list:
Add a method remove(ValueType e), which takes an element e to remove.
It should remove the first node whose value is equal to e. (Hint: use the equals()method)
This method should return true if a node was removed. (e was in the list)
This method should return false if no node was removed. (e was not in the list)
Code:
public class DoublyLinkedList
} private static class Node
}
IV. Driver
Create a Driver class with a main method. In main, you should test your remove methods by creating singly and doubly linked lists of GameCharacter objects, and removing objects from them. Make sure to test:
Removing elements from the head, middle, and tail of the list.
Removing elements from a single-element list.
Removing elements from an empty list.
Attempting to remove elements that are not in the list.
Attempting to remove elements that are in the list.
Attempting to remove an element that appears multiple times in the list.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
