Question: Please do task 4, i will provide code for task 2 and 3 Interface public interface TheStackofBooks { void addBook(T newBook); T removeBook() throws Exception;

Please do task 4, i will provide code for task 2 and 3

Please do task 4, i will provide code for task 2 and

Interface

public interface TheStackofBooks { void addBook(T newBook); T removeBook() throws Exception; T topBook(); boolean isEmpty(); void clear(); int size(); }

Task 2:

public class PileofBooks implements TheStackofBooks { private T[] books; private static final int default_capacity=25; private int numberofbooks; PileofBooks() { books=(T[]) new Object[default_capacity]; numberofbooks=0; } public void addBook(T newBook) { books[numberofbooks]=newBook; numberofbooks++; } public T removeBook() { T result=null; if(!isEmpty()) { result=books[--numberofbooks]; books[numberofbooks]=null; } return null; } public T topBook() { T result=null; if(!isEmpty()) { result=books[numberofbooks-1]; } return null; } public boolean isEmpty() { return numberofbooks==0; } public void clear() { while(!isEmpty()) removeBook(); } public int size() { return numberofbooks; } public String toString() { String result=""; for(int i=numberofbooks-1; i>=0;i--) result +=books[i] + " "; return result; } } 

Task 3:

public class PileofBooks2 implements TheStackofBooks { static class Node { T data; Node next; public Node(T data, Node next) { this.data = data; this.next = next; } } private Node books; private int numberofbooks; PileofBooks2() { books = null; numberofbooks = 0; } public void addBook(T newBook) { books = new Node(newBook, books); numberofbooks++; } public T removeBook() { if(books == null) { return null; } else { T book = books.data; books = books.next; numberofbooks--; return book; } } public T topBook() { if(books == null) { return null; } else { return books.data; } } public boolean isEmpty() { return books == null; } public void clear() { books = null; numberofbooks = 0; } public int size() { return numberofbooks; } public String toString() { String result=""; Node temp = books; while (temp != null) { result += temp.data + " "; temp = temp.next; } return result; } }

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!