l. GameCharacter Create a class GameCharacter that has a single member variable:
ID: 3700839 • Letter: L
Question
l. 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 o IMPORTANT: in order to correctly override this method, you must use the signature "public boolean equals(Object other)" - If you do not recall how to implement an equals method, either see section 7.3 of Absolute Java (the textbook for Intro. II), or look at this page e (the third result when Googling for this) which provides a very good explanation of what to do and why to do it.Explanation / Answer
I have attempted the code. Please comment me if you have any issues: -
GameCharacter.java
public class GameCharacter {
private String name;
public GameCharacter(String name) {
this.name = name;
}
public String toString() {
return name;
}
public boolean equals(GameCharacter other) {
if (this.getClass() != other.getClass()) {
return false;
}
return true;
}
}
SinglyLinkedList.java
public class SinglyLinkedList<GenericType> {
private Node head;
private int size;
// constructor
public SinglyLinkedList() {
head = null;
size = 0;
}
// insert at front
public void insertAtFront(GenericType value) {
Node newNode = new Node(value, head);
head = newNode;
size++;
}
// efficient size method
public int efficientSize() {
return size;
}
// method to remove things from anywhere in the list
public boolean remove(GenericType character) {
if (size == 0) {
// do nothing
}
// checks for size of 1 and makes sure the character entered is in the
// list.
else if (size == 1) {
if (character.equals(head.getValue())) {
head = null;
size--;
return true;
}
}
// checks the first element in the list and makes sure the character
// entered is
// equal to the first element also.
else if (character.equals(head.getValue())) {
head = head.getNext();
size--;
return true;
}
// checks the second element and makes sure its in the list.
else if (character.equals(head.getNext().getValue())) {
head.setNext(head.getNext().getNext());
size--;
return true;
}
// checks the rest of the list and removes characters accordingly
else {
Node temp = head;
if (character.equals(temp.getNext().getNext().getValue())) {
for (; temp.getNext().getNext().getValue() != character; temp = temp.getNext()) {
}
temp.getNext().setNext(temp.getNext().getNext().getNext());
size--;
return true;
}
}
return false;
}
// descriptor
public String toString() {
String ret = "";
// iterate through the list, and append each value to ret
for (Node temp = head; temp != null; temp = temp.getNext()) {
ret += (temp.getValue() + " ");
}
return ret;
}
private class Node {
private GenericType value;
private Node next;
public Node(GenericType value2, Node next) {
this.value = value2;
this.next = next;
}
public GenericType getValue() {
return value;
}
public Node getNext() {
return next;
}
public void setNext(Node n) {
next = n;
}
public String toString() {
return "" + value;
}
}
}
DoublyLinkedList.java
// make this list generic
public class DoublyLinkedList<ValueType> {
// member variables
private Node head;
private Node tail;
private int size;
// constructor
public DoublyLinkedList() {
// create sentinel nodes: head, tail
head = new Node(null, null, null);
tail = new Node(null, null, null);
// link together
head.setNext(tail);
tail.setPrev(head);
// set size to zero nodes
size = 0;
}
// insertAtFront method
public void insertAtFront(ValueType value) {
insertBetween(value, head, head.getNext());
}
// private helper method to insert between two nodes
private void insertBetween(ValueType value, Node predecessor, Node successor) {
// which nodes need to be update?
// predecessor.next must point the new node
// new node's prev must point to predecessor
// new node's next must point to successor
// successor.prev must point to the new node
// first, create new node
Node newNode = new Node(value, predecessor, successor);
// update predecessor
predecessor.setNext(newNode);
// update successor
successor.setPrev(newNode);
// since we added a new node, increase the size
size++;
}
public boolean remove(ValueType character) {
if (size == 0) {
// do nothing
}
// checks for size of 1 and makes sure the character entered is in the
// list.
else if (size == 1) {
if (character.equals(head.getValue())) {
head.setNext(null);
size--;
return true;
}
}
// Check if the size is greater than 1 but its the first element of the
// list and
// will remove it.
else if (character.equals(head.getNext().getValue())) {
head.setNext(head.getNext().getNext());
head.getNext().getNext().setPrev(head);
size--;
return true;
}
// verifies and removes the rest of the specified values from the list.
else {
Node temp = head.getNext();
if (character.equals(head.getNext().getValue())) {
for (; temp.getValue() != character; temp = temp.getNext()) {
System.out.println(temp);
}
temp.getPrev().setNext(temp.getNext());
temp.getNext().setPrev(temp.getPrev());
size--;
return true;
}
}
return false;
}
// size method
public int size() {
return size;
}
// toString method
public String toString() {
// string to return
String ret = "";
// iterate through values, adding each to the string
for (Node temp = head.getNext(); temp != tail; temp = temp.getNext()) {
ret += (temp.getValue() + " ");
}
// return constructed string
return ret;
}
// internal node class
private class Node {
private ValueType value;
private Node next;
private Node prev;
public Node(ValueType value, Node prev, Node next) {
this.value = value;
this.next = next;
this.prev = prev;
}
public ValueType getValue() {
return value;
}
public Node getNext() {
return next;
}
public void setNext(Node n) {
next = n;
}
public Node getPrev() {
return prev;
}
public void setPrev(Node p) {
prev = p;
}
public String toString() {
return "" + value;
}
}
}
Driver.java
/*
* This program will create two lists and add gamecharacters to each list. It will then remove any object a user gives it and update the size.
*/
public class Driver {
public static void main(String[] args) {
// creates both lists with 8 values inside
SinglyLinkedList<GameCharacter> SingleList = new SinglyLinkedList<GameCharacter>();
DoublyLinkedList<GameCharacter> DoubleList = new DoublyLinkedList<GameCharacter>();
GameCharacter gc1 = new GameCharacter("bob1");
GameCharacter gc2 = new GameCharacter("bob2");
GameCharacter gc3 = new GameCharacter("bob3");
GameCharacter gc4 = new GameCharacter("bob4");
GameCharacter gc5 = new GameCharacter("bob5");
GameCharacter gc6 = new GameCharacter("bob6");
GameCharacter gc7 = new GameCharacter("bob7");
GameCharacter gc8 = new GameCharacter("bob8");
// inserts some of our values into the list
SingleList.insertAtFront(gc1);
SingleList.insertAtFront(gc2);
// SingleList.insertAtFront(gc3);
SingleList.insertAtFront(gc4);
SingleList.insertAtFront(gc5);
SingleList.insertAtFront(gc6);
// SingleList.insertAtFront(gc7);
SingleList.insertAtFront(gc8);
DoubleList.insertAtFront(gc1);
DoubleList.insertAtFront(gc2);
// DoubleList.insertAtFront(gc3);
DoubleList.insertAtFront(gc4);
DoubleList.insertAtFront(gc5);
DoubleList.insertAtFront(gc6);
// DoubleList.insertAtFront(gc7);
DoubleList.insertAtFront(gc8);
// prints out our list with the size
System.out.println("Initial Single List: " + SingleList + " Size: " + SingleList.efficientSize());
System.out.println("Initial Double List: " + DoubleList + " Size: " + DoubleList.size());
// attemps to remove things inside the lists and not inside the lists
SingleList.remove(gc8);
SingleList.remove(gc7);
SingleList.remove(gc4);
SingleList.remove(gc3);
SingleList.remove(gc1);
DoubleList.remove(gc8);
DoubleList.remove(gc7);
DoubleList.remove(gc4);
DoubleList.remove(gc3);
DoubleList.remove(gc1);
// prints out our final list.
System.out.println("Final Single List: " + SingleList + " With size of: " + SingleList.efficientSize());
System.out.println("Final Double List: " + DoubleList + " With size of: " + DoubleList.size());
}
}
Output
Initial Single List: bob8 bob6 bob5 bob4 bob2 bob1 Size: 6
Initial Double List: bob8 bob6 bob5 bob4 bob2 bob1 Size: 6
Final Single List: bob6 bob5 bob2 bob1 With size of: 4
Final Double List: bob6 bob5 bob4 bob2 bob1 With size of: 5
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.