Question

In: Computer Science

write a java code to implement a linked list, called CupList, to hold a list of...

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.

Solutions

Expert Solution

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


Related Solutions

Write a Java program to implement a Single Linked List that will take inputs from a...
Write a Java program to implement a Single Linked List that will take inputs from a user as Student Names. First, add Brian and Larry to the newly created linked list and print the output Add "Kathy" to index 1 of the linked list and print output Now add "Chris" to the start of the list and "Briana" to the end of the list using built-in Java functions. Print the output of the linked list.
Write a Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
write a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
Could you check/edit my code? Design and implement your own linked list class to hold a...
Could you check/edit my code? Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member function for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should member functions to...
write a java program to Implement a Priority Queue using a linked list. Include a main...
write a java program to Implement a Priority Queue using a linked list. Include a main method demonstrating enqueuing and dequeuing several numbers, printing the list contents for each.
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and...
Write an efficient java program to implement an integer doubly linked list Dequeue which insertion and deletion can be than at either end. You have to write 6 methods: add front, delete front, add rear, delete rear, print forward (left to right) and print backward (right to left). After each addition or deletion dequeue elements are printed forward or backward respectively. Your main method should be as follow: public static void main(String args[]) { xxxxx dq = new xxxxx ();...
C++ question: Design and implement your own linked list class to hold a sorted list of...
C++ question: Design and implement your own linked list class to hold a sorted list of integers in ascending order. The class should have member functions for inserting an item in the list, deleting an item from the list, and searching the list for an item. Note: the search function should return the position of the item in the list (first item at position 0) and -1 if not found. In addition, it should have member functions to display the...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language...
IN JAVA LANGUAGE Linked List-Based Queue Implementation Implement Queue using a Linked List. Use the language library LinkedList Queue methods will call the LinkedList methods You can use string as the object Instead of using an array, as the QueueLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : enqueue(), dequeue(), size(), printQueue(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT