Write the following using java
Given a class ‘Node’ and ‘NodeList’, that contains the below information diagram.
-id: int
-name: String
-next: Node
+Node(id: int, name: String)
+setId(id: int): void
+getId(): int
+setName(name: String) : void
+getName(): String
+setNext(node: Node): void
+getNext(): Node
NodeList
-size: int
-root: Node
+add(node: Node): void
+size(): int
+findNode(node: Node): boolean
Implement add(Node), findNode(Node) and size methods in the NodeList class which is
provided to you. The methods should work as:
Using the following main method:
public static void main(String[] args)
{
NodeList list = new NodeList();
Node node = new Node(1, \"Book\");
Node node2 = new Node(2, \"Lappy\");
list.add(node);
list.add(node2);
System.out.println(\"Length : \"+list.size());
Node node3 = new Node(3, \"Glass\");
Node node4 = new Node(4, \"Pen\");
list.add(node3);
System.out.println(\"Length : \"+list.size());
if(list.findNode(node3)) System.out.println(\"Node found: \"+ node3.getName());
else
System.out.println(\"Node not found: \"+ node3.getName());
if(list.findNode(node4)) System.out.println(\"Node found: \"+ node4.getName());
else
System.out.println(\"Node not found: \"+ node4.getName());
}
Then it should return the following output:
Length : 2
Length : 3
Node found: Glass
Node not found: Pen
The given Node class is:
public class Node
{
private int id = 0;
private String name = \"\";
private Node next;
public Node(int id, String name)
{
this.id = id;
this.name = name;
this.next = null;
}
public Node getNext()
{
return next;
}
public void setNext(Node node)
{
this.next = node
}
public int getId()
{
return id;
{
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String toString()
{
return \"ID : \"+this.id+\" Name : \"+this.name;
}
}
The given NodeList class is:
public class NodeList
{
private int size = 0;
private Node root = null;
/*
* It has to take a new Node and add that to the next address of previous Node.
* If the list is empty, assign it as the \"root\"
* @Param - Node
*/
public void add(Node node)
{
// Implement this method!!!
}
/*
* It has to return the size of the NodeList
*
* @return size
*/
public int size()
{
// Implement this method!!!
}
/*
* It has to take a Node and checks if the node is in the list.
* If it finds the node, it returns true, otherwise false
*
* @param - Node
* @return boolean true/false
*/
public boolean findNode(Node node)
{
// Implement this method!!!
}
}Node
-id: int
-name: String
-next: Node
+Node(id: int, name: String)
+setId(id: int): void
+getId(): int
+setName(name: String) : void
+getName(): String
+setNext(node: Node): void
+getNext(): Node
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
############ Node.java #############
public class Node
{
private int id = 0;
private String name = \"\";
private Node next;
public Node(int id, String name)
{
this.id = id;
this..
Write the following using javaGiven a class ‘Node’ and ‘NodeList’,.pdf
1. Write the following using java
Given a class ‘Node’ and ‘NodeList’, that contains the below information diagram.
-id: int
-name: String
-next: Node
+Node(id: int, name: String)
+setId(id: int): void
+getId(): int
+setName(name: String) : void
+getName(): String
+setNext(node: Node): void
+getNext(): Node
NodeList
-size: int
-root: Node
+add(node: Node): void
+size(): int
+findNode(node: Node): boolean
Implement add(Node), findNode(Node) and size methods in the NodeList class which is
provided to you. The methods should work as:
Using the following main method:
public static void main(String[] args)
{
NodeList list = new NodeList();
Node node = new Node(1, "Book");
Node node2 = new Node(2, "Lappy");
list.add(node);
list.add(node2);
System.out.println("Length : "+list.size());
Node node3 = new Node(3, "Glass");
Node node4 = new Node(4, "Pen");
list.add(node3);
System.out.println("Length : "+list.size());
if(list.findNode(node3)) System.out.println("Node found: "+ node3.getName());
else
2. System.out.println("Node not found: "+ node3.getName());
if(list.findNode(node4)) System.out.println("Node found: "+ node4.getName());
else
System.out.println("Node not found: "+ node4.getName());
}
Then it should return the following output:
Length : 2
Length : 3
Node found: Glass
Node not found: Pen
The given Node class is:
public class Node
{
private int id = 0;
private String name = "";
private Node next;
public Node(int id, String name)
{
this.id = id;
this.name = name;
this.next = null;
}
public Node getNext()
{
return next;
}
public void setNext(Node node)
{
this.next = node
}
public int getId()
{
return id;
{
3. public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String toString()
{
return "ID : "+this.id+" Name : "+this.name;
}
}
The given NodeList class is:
public class NodeList
{
private int size = 0;
private Node root = null;
/*
* It has to take a new Node and add that to the next address of previous Node.
* If the list is empty, assign it as the "root"
* @Param - Node
*/
public void add(Node node)
{
// Implement this method!!!
4. }
/*
* It has to return the size of the NodeList
*
* @return size
*/
public int size()
{
// Implement this method!!!
}
/*
* It has to take a Node and checks if the node is in the list.
* If it finds the node, it returns true, otherwise false
*
* @param - Node
* @return boolean true/false
*/
public boolean findNode(Node node)
{
// Implement this method!!!
}
}Node
-id: int
-name: String
-next: Node
+Node(id: int, name: String)
+setId(id: int): void
+getId(): int
5. +setName(name: String) : void
+getName(): String
+setNext(node: Node): void
+getNext(): Node
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
############ Node.java #############
public class Node
{
private int id = 0;
private String name = "";
private Node next;
public Node(int id, String name)
{
this.id = id;
this.name = name;
this.next = null;
}
public Node getNext()
{
return next;
}
public void setNext(Node node)
{
this.next = node ;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
6. }
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String toString()
{
return "ID : "+this.id+" Name : "+this.name;
}
}
################ NodeList.java ########################
public class NodeList
{
private int size = 0;
private Node root = null;
/*
* It has to take a new Node and add that to the next address of previous Node.
* If the list is empty, assign it as the "root"
* @Param - Node
*/
public void add(Node node)
{
// Implement this method!!!
if(root == null)
root = node;
else{
node.setNext(root); // adding at front
root = node;
}
size++;
}
7. /*
* It has to return the size of the NodeList
*
* @return size
*/
public int size()
{
// Implement this method!!!
return size;
}
/*
* It has to take a Node and checks if the node is in the list.
* If it finds the node, it returns true, otherwise false
*
* @param - Node
* @return boolean true/false
*/
public boolean findNode(Node node)
{
// Implement this method!!!
Node temp = root;
while(temp != null){
if(temp.getId() == node.getId() &&
temp.getName().equalsIgnoreCase(node.getName()))
return true;
temp = temp.getNext();
}
return false;
}
}
################ NodeListTest.java ########################
public class NodeListTest {
public static void main(String[] args)
{
NodeList list = new NodeList();