Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.image text in transcribed

Task1:

/** An interface that describes the operations of a pile of books. */ public interface PileOfBooksInterface { /** Gets the current number of books in this pile * @return The integer number of books currently in this pile */ public int getCurrentSize(); /** Checks if the pile is empty or not * @return true if the pile is empty and false if it is not */ public boolean isEmpty(); /** Adds a new entry to this bag * @param newBook The book that is added * @return true if the book is successfully added and false if not */ public boolean add(T newBook); /** Remove one unspecified entry from this pile * @return the removed entry if the function was successful or returns null */ public T remove(); /** Removes all books from the pile */ public void clear(); /** Gets the top book in the pile * @return the top book, else returns null * */ public T bookOnTop(); }

Task 2:

import java.util.Arrays;

public class PileOfBooks implements PileOfBooksInterface { private T[] books; //Array of books private int numberOfBooks; //Total number of books private boolean integrityOK = true; private static final int BOOK_CAPACITY = 50; private static final int MAX_CAPACITY = 10000;

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 implements PileOfBooksInterface { private Node firstNode; //reference to first node private int numberOfBooks; public PileOfBook() { firstNode = null; numberOfBooks = 0; }

private class Node { private T data; private Node next; private Node(T dataPortion) { this(dataPortion, null); } private Node(T dataPortion, Node nextNode) { this.data = dataPortion; this.next = nextNode; } private T getData() { return data; } private void setData(T newData) { data = newData; } private Node getNextNode() { return next; } private void setNextNode(Node nextNode) { next = nextNode; } }

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 temp = firstNode; while (temp != null) { result += temp.data + " "; temp = temp.next; } return result; }

}

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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications Dexa 2022 Workshops 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 In Computer And Information Science 33

Authors: Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil ,Bernhard Moser ,Alfred Taudes ,Atif Mashkoor ,Johannes Sametinger ,Jorge Martinez-Gil ,Florian Sobieczky ,Lukas Fischer ,Rudolf Ramler ,Maqbool Khan ,Gerald Czech

1st Edition

3031143426, 978-3031143427

More Books

Students also viewed these Databases questions

Question

5. Understand how cultural values influence conflict behavior.

Answered: 1 week ago

Question

8. Explain the relationship between communication and context.

Answered: 1 week ago