Question
This is my code for Task 1 and Task 2. Can you please help me with Task 3 and 4 Including adding to the current
This is my code for Task 1 and Task 2. Can you please help me with Task 3 and 4 Including adding to the current Demo.
public class ArrayDriver {
public static void main(String[] args) { PileOfBooks
System.out.println("Wow, we need to move this pile of books!"); System.out.println("At the top of the pile we have: "+ pile.peek()); pile.remove(); System.out.println("Next we have: " + pile.peek()); System.out.println("Man, these are heavy! We must be done moving them" + " all now, right? " +pile.isEmpty()); System.out.println("Guess not. We still have: " + pile.peek()); pile.remove(); System.out.println("And then: " + pile.peek()); pile.remove(); System.out.println("Then: " + pile.peek()); pile.remove(); System.out.println("Then: " + pile.peek()); pile.remove(); System.out.println("And I believe the last one: " + pile.peek()); pile.remove(); System.out.println("This pile should be empty: " +pile.isEmpty()); }
} ------------------------------------------------------------------------------------------------------------------------------------------ import java.util.*;
public class PileOfBooks
public PileOfBooks(int desiredCapacity) { if(desiredCapacity
public PileOfBooks() {
}
// This method checks the integrity of the bag.
public void checkIntegrity() { if(!integrityOK) throw new SecurityException("ArrayBag object is corrupt."); }
// This method returns the number of books in the pile.
public int getCurrentSize() { return bag.length; }
// This methods returns true if there are not books in the pile.
public boolean isEmpty() { //return true if top is -1, means null return this.top==-1; }
// This method adds a new book to the pile and doubles the capacity if array is full.
public boolean add (T newEntry) { checkIntegrity(); boolean result = true; if(this.top+1==this.capacity) { doubleCapacity(); } ++this.top; this.bag[this.top]=newEntry; return result; }
// This method removes the book at the top of the pile.
public T remove() { if(isEmpty()) { System.out.println("Stack is empty!"); } int index = this.top; //string looking at is string on top of stack T string = bag[index]; //clear the stack @ that index bag[index]=null; //top is now below the removed item this.top--; //return string we originally looked at return string; }
// This method returns the book title at the top of the pile.
public T peek() { if (isEmpty()) { System.out.println("No books to see here!"); throw new EmptyStackException(); } return this.bag[this.top]; }
// This method checks the capacity of the pile.
private void checkCapacity(int capacity) { if(capacity>MAX_CAPACITY) throw new IllegalStateException("Attempt to create a bag" + " whose capacity exceeds allowed maximum " + "of " + MAX_CAPACITY); }
// This method doubles the capacity of the pile.
private void doubleCapacity() { int newLength = 2*bag.length; checkCapacity(newLength); bag=Arrays.copyOf(bag, newLength); }
// This method clears all books in the pile.
public void clear() { while(!isEmpty()) { remove(); } } }
----------------------------------------------------------------------------------------------------------------------------------------------------- public interface BagInterface
public int getCurrentSize();
// Sees whether this bag is empty public boolean isEmpty();
// Adds new entry to the bag, if possible. public boolean add(T newEntry);
// Removes unspecified entry from the bag, public T remove();
/** * Removes all entries from this bag */ public void clear();
public T peek();
}
Task Description 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 answersStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started