In: Computer Science
write a java code to implement a linked list, called CupList, to hold a list of Cups.
1.Define and write a Cup node class, called CupNode, to hold the following information about a cup:
•number (cup number)
•capacity (cup capacity in ml)
•Write a method size() that returns the number of elements in the linkedlist CupList.
•Write a method getNodeAt() that returns the reference to cup node object at a specific position given as a parameter of the method.
•Write a method, insertPos(), to insert a new CupNode object at a specific position given as a parameter of the method.
•Write a method, deletePos(), to remove the CupNode object at a specific position given as a parameter of the method.
•Write a method, swapNodes, to swap two nodes at two positions pos1 and pos2 given as parameters of the method.
Implement the Cup list using double linked list.
•Update CupNode class by adding a link to the previous node in the list. Then update the constructor of the class
•Update the CupList by adding a last attribute, to hold the reference to the last node of the list.
•Implement the following methods in CupList isEmpty(), size(), insertAtFront(), insertAtRear(), insertAt(), removeFirst(), removeLast(), removeAt().
CupNode should have a constructor and methods to manage the above information as well as the link to next node in the list.
2.Define and write the CupList class to hold objects of the class CupNode. This class should define:
a)a constructor,
b)a method isEmpty() to check if the list is empty or not,
c)a method insert() that will allow to insert a new cup node at the end of the list,
d)a method print() that will allow to print the content of the list,
e)A method getCupCapacity() that will return the capacity of a cup given its number. The method should first find out if a cup with that number is in the list.
3.Write a TestCupList class to test the class CupList. This class should have a main method in which you perform the following actions :
a)Create a CupList object,
b)Insert five to six CupNode objects into the created Cup List,
c)Print the content of your cup list,
d)Search for a given cup number in the list and print out its capacity.
Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
CupNode.java
----------
public class CupNode{
private int number;
private int capacity;
private CupNode next;
public CupNode(){
}
public CupNode(int cnum, int cap){
number = cnum;
capacity = cap;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public CupNode getNext() {
return next;
}
public void setNext(CupNode next) {
this.next = next;
}
}
CupList.java
-----------
public class CupList {
private CupNode first , last;
public CupList(){
}
public boolean isEmpty(){
return first == null;
}
public void insert(int cupNum, int cupCap){
CupNode cn = new CupNode(cupNum, cupCap);
if(isEmpty())
first = last = cn;
else
{
last.setNext(cn);
last = cn;
}
}
public void print(){
CupNode curr = first;
while(curr != null){
System.out.println("Cup[ No. = " + curr.getNumber() + ", capacity = " + curr.getCapacity() + "]");
curr = curr.getNext();
}
System.out.println();
}
public int getCupCapacity(int cupNum){
int value = 0;
CupNode curr = first;
while(curr != null){
if(curr.getNumber() == cupNum){
value = curr.getCapacity();
break;
}
curr = curr.getNext();
}
return value;
}
}
TestCupList.java
------------
public class TestCupList {
public static void main(String[] args) {
CupList clist = new CupList();
clist.insert(1, 10);
clist.insert(2, 20);
clist.insert(3, 30);
clist.insert(4, 40);
clist.insert(5, 50);
System.out.println("The cup list contains");
clist.print();
System.out.println("Capacity of cup 3 = " + clist.getCupCapacity(3));
}
}
output
------
The cup list contains
Cup[ No. = 1, capacity = 10]
Cup[ No. = 2, capacity = 20]
Cup[ No. = 3, capacity = 30]
Cup[ No. = 4, capacity = 40]
Cup[ No. = 5, capacity = 50]
Capacity of cup 3 = 30