Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Question Imagine that you have a base class named linkedBase that implement

ID: 3759763 • Letter: J

Question

Java Question
Imagine that you have a base class named linkedBase that implements the following code:
Public int getlength() Public Boolean isempty() Protected final node getNodeAt(int givenPosition) Protected final getFirstNode() Protected final T removeFirstNode() Protected final T removeAfterNode(Node nodebefore)
Write a subclass of linkedBase that adds a single method named removeEqualNeighbors. It should remove any adjacent elements with the same values. After the method is called there should be no neighboring elements in the list with the same value.
Assume that the nodes class has a getData() method that returns a T, and that you can use the equals method to see if two elements are equal. Java Question
Imagine that you have a base class named linkedBase that implements the following code:
Public int getlength() Public Boolean isempty() Protected final node getNodeAt(int givenPosition) Protected final getFirstNode() Protected final T removeFirstNode() Protected final T removeAfterNode(Node nodebefore)
Write a subclass of linkedBase that adds a single method named removeEqualNeighbors. It should remove any adjacent elements with the same values. After the method is called there should be no neighboring elements in the list with the same value.
Assume that the nodes class has a getData() method that returns a T, and that you can use the equals method to see if two elements are equal. Java Question

Public int getlength() Public Boolean isempty() Protected final node getNodeAt(int givenPosition) Protected final getFirstNode() Protected final T removeFirstNode() Protected final T removeAfterNode(Node nodebefore)
Write a subclass of linkedBase that adds a single method named removeEqualNeighbors. It should remove any adjacent elements with the same values. After the method is called there should be no neighboring elements in the list with the same value.
Assume that the nodes class has a getData() method that returns a T, and that you can use the equals method to see if two elements are equal.

Explanation / Answer

public class LinkedChainBase<T> implements java.io.Serializable {
private Node firstNode;
private int length;
  
public LinkedChainBase(){
clear();
}
public final void clear() {//clear()
firstNode = null;
length = 0;
}
  
  
protected void addFirstNode(Node newNode){
assert newNode != null:"null argument in addFirstNode";
newNode.setNextNode(firstNode);
firstNode = newNode;
length++;
}
  
protected Node getFirstNode(){
return firstNode;
}
  
protected Node getNodeAt(int givenPosition){
assert (!isEmpty() && ((1 <= givenPosition) && (givenPosition <= length)));
Node currentNode = firstNode;
for(int counter = 1; counter < givenPosition; counter++){
currentNode = currentNode.next;
}
assert currentNode != null;
return currentNode;
}
  
protected void addAfterNode(Node nodeBefore, Node newNode){
assert newNode != null:"illegal to add a null node";
newNode.setNextNode(nodeBefore.getNextNode());
nodeBefore.setNextNode(newNode);
}
  
protected T removeFirstNode(){
T result = null;
if(firstNode != null){
result = firstNode.data;
firstNode = firstNode.getNextNode();
}
return result;
}

// protected T removeAfterNode(Node nodeBefore){
//
// }
public boolean isEmpty() {
boolean result;
if(length == 0){
assert firstNode == null;
result = true;
}
else{
assert firstNode != null;
result = false;
}
return result;
}
  
public int getLength() {
return length;
}

public void display() {
Node currentNode = firstNode;
while(currentNode != null){
System.out.println(currentNode.data);
currentNode = currentNode.next;
}
}
  
public T getEntry(int givenPosition) {
T result = null;
if((!isEmpty()) && ((givenPosition >= 1) && (givenPosition <= getLength()))){
result = getNodeAt(givenPosition).getData();
}
return result;
}
  
public boolean contains(T anEntry) {
boolean found = false;
Node currentNode = getFirstNode();
while(!found && currentNode != null){
if(currentNode.getData().equals(anEntry)){
found = true;
break;
}
currentNode = currentNode.getNextNode();
}
return found;
}
  
/*

protected class Node{
private T data;
private Node next;
  
protected Node(T dataPortion){
data = dataPortion;
next = null;
}
private Node(T dataPortion, Node nextNode){
data = dataPortion;
next = nextNode;
}
protected T getData(){
return data;
}
protected void setData(T dataPortion){
data = dataPortion;
}
protected Node getNextNode(){
return next;
}
protected void setNextNode(Node nextNode){
next = nextNode;
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote