Question
I have completed the code but it doesn't seem to work. I need help fixing the code and making it work. Task1: /** An interface
I have completed the code but it doesn't seem to work. I need help fixing the code and making it work.
Task1:
/** An interface that describes the operations of a pile of books. */ public interface PileOfBooksInterface
Task 2:
import java.util.Arrays;
public class PileOfBooks
public PileOfBooks() { this(BOOK_CAPACITY); } public PileOfBooks(int capacity) { integrityOK = false; checkCapacity(capacity); @SuppressWarnings("unchecked") T[] tempPile = (T[]) new Object[capacity]; books = tempPile; numberOfBooks = -1; integrityOK = true; } private void checkIntegrity() { if(!integrityOK) throw new SecurityException("ArrayBooks object is corrupt"); } public int getCurrentSize() { return numberOfBooks; } public boolean isEmpty() { return numberOfBooks == 0; }
//Throws an exception if the client requests a capacity that is too large 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); }//end checkCapacity // Doubles the size of the array bag. // Precondition: checkIntegrity has been called. private void doubleCapacity() { int newLength = 2 * books.length; checkCapacity(newLength); books = Arrays.copyOf(books, newLength); } // end doubleCapacity /** See whether this bag is full. * @return true if the bag is full, or false if not */ public boolean isFull() { return numberOfBooks == books.length; }//end isFull public boolean add(T newBook) { checkIntegrity(); boolean result = true; if(isFull()) { doubleCapacity(); } books[numberOfBooks] = newBook; numberOfBooks++; return true; }
public T remove() { T result = null; if(!isEmpty()) { result = books[numberOfBooks--]; books[numberOfBooks] = null; } return result; } /**Removes all books from the pile*/ public void clear() { while(!isEmpty()) remove(); }//end clear public T bookOnTop() { T result = null; if(!isEmpty()) { result = books[numberOfBooks - 1]; } return null; } public String toString() { String result = ""; for(int i = numberOfBooks - 1; i >= 0; i--) result += books[i] + " "; return result; } }
Task 3:
public class PileOfBook
private class Node
public boolean add(T newBook) { Node newNode = new Node(newBook, null); newNode.next = firstNode; firstNode = newNode; numberOfBooks++; return true; }
public T remove() { T result = null; if(firstNode != null) { result = firstNode.getData(); firstNode = firstNode.getNextNode(); numberOfBooks--; } return result; }
public T bookOnTop() { if(firstNode == null) { return null; } else { return firstNode.data; } }
public boolean isEmpty() { return numberOfBooks == 0; }
public void clear() { while(!isEmpty()) remove(); }
public int getCurrentSize() { return numberOfBooks; }
public String toString() { String result=""; Node
}
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 arrayStep 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