Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please use ADT Bag in Java Task 1: Imagine a pile ofbooks on your desk. Each boo

ID: 3753209 • Letter: P

Question

Please use ADT Bag in Java

Task 1: Imagine a pile ofbooks on your desk. Each book is so large and heavy that you can remove only the top one from the pile. You cannot remove a book from under another one. Likewise, you cannot add a book beneath another one. You can add another book to the pile only by placing it on the top of the pile If you represent books by their titles alone, design a class that you can use to track the books in the pile on your desk. Specify each operation by stating its purpose, by describing its parameters, and by writing a pseudocode version of its header. Then write a Java interface for the pile's methods. Please follow the code examples in our lecture slides to comment your code.

Explanation / Answer

Explaination is present in the comments.

>> ADTBag class

public class ADTBag implements ADTBagInterface {

/*

* Node class is used to store all the book titles

* Each Node stores a title

* and the address of its next node (book beneath the current book)

*/

private class Node {

String title;

Node next;

}

/*

* The stack of books is analogous to the stack DataStructure

* except that we can keep track of all the books

* Head: the book present at top

* size: no of books present at the desk

*/

private Node head;

private int size;

/*

* @see adtBag.ADTBagInterface#display()

* display method is used to print all the books

* present on table from top to bottom

*

* Iterate over all the nodes and print its data

* Dashes are used as separators for clarity

*/

@Override

public void display() {

System.out.println("-------------------");

Node temp = this.head;

while (temp != null) {

System.out.println(temp.title + ", ");

temp = temp.next;

}

System.out.println("-------------------");

}

/*

* @see adtBag.ADTBagInterface#isEmpty()

* Returns if the ADTBag is empty

*

* ADTbag is empty when head points to null

*/

@Override

public boolean isEmpty() {

return this.head == null;

}

/*

* @see adtBag.ADTBagInterface#size()

* Returns the number of books on desk

*

* Size is stored in a private variable -> size

*/

@Override

public int size() {

return size;

}

/*

* @see adtBag.ADTBagInterface#add(java.lang.String)

* add method adds a new book on the top of the stack

* since we can't add books in between

*

* PSEUDOCODE

* Create a new node

* If nodes are already present, attach it to the previous head

* Declare that current node is head node by changing this.head = nn

* Increment the size of ADTBag

*/

@Override

public void add(String name) {

// create a new node

Node nn = new Node();

nn.title = name;

nn.next = null;

// attach

if (this.size >= 1) {

nn.next = this.head;

}

// summary

this.head = nn;

this.size++;

}

/*

* @see adtBag.ADTBagInterface#removeFirst()

* RemoveFirst method removes the book at the top

* since we can't remove from in between

*

* PSEUDOCODE

* If no node is present (size == 0) then throw an error

* else store the title of book in a variable

* make the book present beneath the current book as the head node

* decrement the size

* return the name of current book

*

* Why is the name returned?

* Because you may want to remove a book from top

* then add some books

* then replace the removed book at the top.

* In such a case you may want to store the name of the topmost book in a variable

*/

@Override

public String removeFirst() throws Exception {

if (size == 0) {

throw new Exception("Linked List is Empty");

}

String rv = this.head.title;

this.head = this.head.next;

size--;

return rv;

}

/*

* @see adtBag.ADTBagInterface#getAt(int)

* getAt method returns the name of any book that you ask

* This is done to keep track of all the books

*

* PSEUDOCODE

* If there are no books (size == 0) then throw error

* If user asks for a book that is not present (index < 0 || index >= this.size)

* then throw error

*

* else iterate to the index

* then return the title of the book

*/

@Override

public String getAt(int index) throws Exception {

if (this.size == 0) {

throw new Exception("Linked List is Empty");

}

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

throw new Exception("Index out of bound");

}

// book can be accessed

Node temp = this.head;

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

temp = temp.next;

}

return temp.title;

}

}

>> INTERFACE -

package adtBag;

public interface ADTBagInterface {

public void display();

boolean isEmpty();

int size();

void add(String item);

String removeFirst() throws Exception;

String getAt(int index) throws Exception;

}

>> CLIENT CLASS

public class ADTBagClient {

public static void main(String[] args) throws Exception {

// make a new bag

ADTBag bag = new ADTBag();

// add a book

bag.add("abc");

// add another book on top

bag.add("def");

// display the books from top to bottom

bag.display();

// add some more books

bag.add("ghi");

bag.add("jkl");

// number of books present on desk

System.out.println("After adding " + bag.size() + " books - ");

// display

bag.display();

// remove a book from top

bag.remove();

// number of books present on desk

System.out.println("No of books - " + bag.size());

// display

bag.display();

}

}

>> OUTPUT

-------------------
def,
abc,
-------------------
After adding 4 books -
-------------------
jkl,
ghi,
def,
abc,
-------------------
No of books - 3
-------------------
ghi,
def,
abc,
-------------------