Draw an UML class diagram for the following code: import java.util.EmptyStackException;   class Node {    int data;    Node next;      public Node(int data) {        this.data = data;        this.next = null;    } }   class List {    private Node head;    private int size;      public List() {        head = null;        size = 0;    }      public void add(int data) {        Node newNode = new Node(data);        if (head == null) {            head = newNode;        } else {            Node current = head;            while (current.next != null) {                current = current.next;            }            current.next = newNode;        }        size++;    }      public int get(int index) {        if (index < 0 || index >= size) {            throw new IndexOutOfBoundsException("Index out of range");        }          Node current = head;        for (int i = 0; i < index; i++) {            current = current.next;        }          return current.data;    }      public void remove(int index) {        if (index < 0 || index >= size) {            throw new IndexOutOfBoundsException("Index out of range");        }          if (index == 0) {            head = head.next;        } else {            Node prev = null;            Node current = head;            for (int i = 0; i < index; i++) {                prev = current;                current = current.next;            }            prev.next = current.next;        }        size--;    }      public boolean isEmpty() {        return size == 0;    }      public int size() {        return size;    }      public void removeAll() {        head = null;        size = 0;    }      public void printList() {        Node current = head;        while (current != null) {            System.out.print(current.data + " ");            current = current.next;        }        System.out.println();    }      public int indexOf(int item) {        Node current = head;        int index = 0;        while (current != null) {            if (current.data == item) {                return index;            }            current = current.next;            index++;        }        return -1;    } }   class Stack {    private List list;      public Stack() {        list = new List();    }      public boolean empty() {        return list.isEmpty();    }      public int peek() throws EmptyStackException {        if (empty()) {            throw new EmptyStackException();        }        return list.get(list.size() - 1);    }      public int pop() throws EmptyStackException {        if (empty()) {            throw new EmptyStackException();        }        int poppedItem = list.get(list.size() - 1);        list.remove(list.size() - 1);        return poppedItem;    }      public void push(int item) {        list.add(item);    }      public int search(int item) {        return list.indexOf(item);    } }   public class Main {    public static void main(String[] args) {        Stack stack = new Stack();          if (stack.empty()) {            System.out.println("The stack is empty");        }          for (int i = 0; i < 5; i++) {            stack.push(i);        }          System.out.println("Element at the top: " + stack.peek());          while (!stack.empty()) {            System.out.print(stack.pop() + " ");        }        System.out.println();          List list = new List();        list.add(10);        list.add(20);        list.add(30);        list.add(40);        list.add(50);          System.out.println("Elements after adding: ");        list.printList();          System.out.println("Element at index 2: " + list.get(2));          list.remove(1);        System.out.println("Elements after removing: ");        list.printList();          System.out.println("Is the list empty? " + list.isEmpty());        System.out.println("Size of the list: " + list.size());          list.removeAll();          System.out.println("Is the list empty after removeAll? " + list.isEmpty());    } }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Draw an UML class diagram for the following code:

import java.util.EmptyStackException;

 

class Node {

   int data;

   Node next;

 

   public Node(int data) {

       this.data = data;

       this.next = null;

   }

}

 

class List {

   private Node head;

   private int size;

 

   public List() {

       head = null;

       size = 0;

   }

 

   public void add(int data) {

       Node newNode = new Node(data);

       if (head == null) {

           head = newNode;

       } else {

           Node current = head;

           while (current.next != null) {

               current = current.next;

           }

           current.next = newNode;

       }

       size++;

   }

 

   public int get(int index) {

       if (index < 0 || index >= size) {

           throw new IndexOutOfBoundsException("Index out of range");

       }

 

       Node current = head;

       for (int i = 0; i < index; i++) {

           current = current.next;

       }

 

       return current.data;

   }

 

   public void remove(int index) {

       if (index < 0 || index >= size) {

           throw new IndexOutOfBoundsException("Index out of range");

       }

 

       if (index == 0) {

           head = head.next;

       } else {

           Node prev = null;

           Node current = head;

           for (int i = 0; i < index; i++) {

               prev = current;

               current = current.next;

           }

           prev.next = current.next;

       }

       size--;

   }

 

   public boolean isEmpty() {

       return size == 0;

   }

 

   public int size() {

       return size;

   }

 

   public void removeAll() {

       head = null;

       size = 0;

   }

 

   public void printList() {

       Node current = head;

       while (current != null) {

           System.out.print(current.data + " ");

           current = current.next;

       }

       System.out.println();

   }

 

   public int indexOf(int item) {

       Node current = head;

       int index = 0;

       while (current != null) {

           if (current.data == item) {

               return index;

           }

           current = current.next;

           index++;

       }

       return -1;

   }

}

 

class Stack {

   private List list;

 

   public Stack() {

       list = new List();

   }

 

   public boolean empty() {

       return list.isEmpty();

   }

 

   public int peek() throws EmptyStackException {

       if (empty()) {

           throw new EmptyStackException();

       }

       return list.get(list.size() - 1);

   }

 

   public int pop() throws EmptyStackException {

       if (empty()) {

           throw new EmptyStackException();

       }

       int poppedItem = list.get(list.size() - 1);

       list.remove(list.size() - 1);

       return poppedItem;

   }

 

   public void push(int item) {

       list.add(item);

   }

 

   public int search(int item) {

       return list.indexOf(item);

   }

}

 

public class Main {

   public static void main(String[] args) {

       Stack stack = new Stack();

 

       if (stack.empty()) {

           System.out.println("The stack is empty");

       }

 

       for (int i = 0; i < 5; i++) {

           stack.push(i);

       }

 

       System.out.println("Element at the top: " + stack.peek());

 

       while (!stack.empty()) {

           System.out.print(stack.pop() + " ");

       }

       System.out.println();

 

       List list = new List();

       list.add(10);

       list.add(20);

       list.add(30);

       list.add(40);

       list.add(50);

 

       System.out.println("Elements after adding: ");

       list.printList();

 

       System.out.println("Element at index 2: " + list.get(2));

 

       list.remove(1);

       System.out.println("Elements after removing: ");

       list.printList();

 

       System.out.println("Is the list empty? " + list.isEmpty());

       System.out.println("Size of the list: " + list.size());

 

       list.removeAll();

 

       System.out.println("Is the list empty after removeAll? " + list.isEmpty());

   }

}

Expert Solution
steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Knowledge Booster
Predicate
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education