Question
Build off Java code provided at bottom: Create a LinkedNode data type that includes the following data along with the appropriate getters/setters/constructors/toString: name (String) age
Build off Java code provided at bottom:
Create a LinkedNode data type that includes the following data along with the appropriate getters/setters/constructors/toString:
name (String)
age (int)
favColor (String)
next (LinkedNode)
Create a testing program that uses only the methods you coded in LinkedNode and does the following (in this specific order):
1.create a reference variable to indicate the beginning of your list
2.create and link 4 new nodes so that you basically have a singly linked list
3.display the list (create a method that uses a loop to do this)
4.add 1 more node
5.display the list
6.delete node 3
7.display the list
8.delete the last node
9.display the list
Draw your final list on paper
public class LinkedNode {
private String name;
private int age;
private String favColor;
private LinkedNode next;
LinkedNode(){
}
LinkedNode( String name, int age, String favColor){
this.name = name;
this.age = age;
this.favColor = favColor;
this.next = null;
}
/**
*
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the favColor
*/
public String getFavColor() {
return favColor;
}
/**
* @param favColor the favColor to set
*/
public void setFavColor(String favColor) {
this.favColor = favColor;
}
/**
* @return the next
*/
public LinkedNode getNext() {
return next;
}
/**
* @param next the next to set
*/
public void setNext(LinkedNode next) {
this.next = next;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "LinkedNode [name=" + name + ", age=" + age + ", favColor=" + favColor + ", next=" + next + "]";
}
} ////test class
public class TestLinkedNode {
/**
* @param args
*/
public static void main(String[] args) {
LinkedNode temp = new LinkedNode("Bob", 20, "red");
LinkedNode node1 = new LinkedNode("James", 22, "green");
LinkedNode node2 = new LinkedNode("Luke", 55, "blue");
LinkedNode node3 = new LinkedNode("Ted", 77,"pink" );
LinkedNode node4 = new LinkedNode("John", 5, "yellow");
temp.setNext(node1);
node1.setNext(node2);
node2.setNext(node3);
node3.setNext(node4);
System.out.println(temp.toString());
}
public static void display(LinkedNode g) {
while(g.getNext()!=null)
{
System.out.println(g.toString());
g = g.getNext();
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started