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

Task 3 test program Task 1: Imagine a pile of books on your desk. Each book is s

ID: 3754791 • Letter: T

Question

Task 3 test program

Task 1: Imagine a pile of books 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. Task 2: Define a class PileOfBooks that implements the interface described in Task 1. Use a resizable array in your implementation. Then write a program that adequately demonstrates your implementation. Task 3: Repeat Task 2, but use a chain of linked nodes instead of an array. Task 4: Using Big Oh notation, indicate the time complexity of each method defined (in Task 2 and Task 3) in the best case and the worst case. Please provide explanations for your answers.

Explanation / Answer

PART 2) CODE

============================

public class PileOfBooks<T> implements TheStackofBooks<T> {

   private ArrayList<T> stack = null;

   public PileOfBooks(){

       stack = new ArrayList<T>();

   }

   @Override

   public void addBook(T newBook) {

       stack.add(newBook);

   }

   @Override

   public T removeBook() throws Exception {

       if(!isEmpty()){

           return stack.remove(stack.size() - 1);

       }else{

           System.out.println("No book in the stack!!");

           return null;

       }

   }

   @Override

   public T topBook() {

       if(!isEmpty()){

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

       }else{

           System.out.println("No book in the stack!!");

           return null;

       }

   }

   @Override

   public boolean isEmpty() {

       return stack.size() == 0;

   }

   @Override

   public void clear() {

       stack = new ArrayList<T>();

   }

   @Override

   public int size() {

       return stack.size();

   }

}

PART 3) CODE

========================

class BookNode<T>

{

   protected T data;

   protected BookNode<T> link;

   /* Constructor */

   public BookNode()

   {

       link = null;

       data = null;

   }

   /* Constructor */

   public BookNode(T d, BookNode<T> n)

   {

       data = d;

       link = n;

   }

   /* Function to set link to next Node */

   public void setLink(BookNode<T> n)

   {

       link = n;

   }

   /* Function to set data to current Node */

   public void setData(T d)

   {

       data = d;

   }

   /* Function to get link to next node */

   public BookNode<T> getLink()

   {

       return link;

   }

   /* Function to get data from current Node */

   public T getData()

   {

       return data;

   }

}

class PileOfBooks<T> implements TheStackofBooks<T> {

   protected BookNode<T> top ;

   protected int size ;

   /* Constructor */

   public PileOfBooks()

   {

       top = null;

       size = 0;

   }

   /* Function to check if stack is empty */

   public boolean isEmpty()

   {

       return top == null;

   }

   /* Function to get the size of the stack */

   public int size()

   {

       return size;

   }

   /* Function to push an element to the stack */

   public void addBook(T data)

   {

       BookNode<T> nptr = new BookNode<T>(data, null);

       if (top == null)

           top = nptr;

       else

       {

           nptr.setLink(top);

           top = nptr;

       }

       size++ ;

   }

   /* Function to pop an element from the stack */

   public T removeBook() throws Exception {

       if (isEmpty() )

           throw new NoSuchElementException("Underflow Exception") ;

       BookNode<T> ptr = top;

       top = ptr.getLink();

       size-- ;

       return ptr.getData();

   }

  

   /* Function to check the top element of the stack */

   public T topBook()

   {

       if (isEmpty() )

           throw new NoSuchElementException("Underflow Exception") ;

       return top.getData();

   }

  

   public void clear() {

       top = null;

       size = 0;

   }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote