task number 2 please. we are using ADT Bag. the first two pictures are for task
ID: 3754328 • Letter: T
Question
task number 2 please. we are using ADT Bag. the first two pictures are for task 1. Thank you.
public interface Pilelnterface k* Adds a new book to the pile of books @param book is the object to be added to pile public void addBook(T book); kx * Removes a book from the file * @return the top book from the pile public T removeBook0; * Gets reference to the top book of this pile kx *@return top book of the pile public T topBook0; *checks if is empty kx * areturn true if the pile contains no elements and false otherwiseExplanation / Answer
import java.util.*;
class PileOfBooks implements PileInteface{
ArrayList<T> pileOfBooks;
public PileOfBooks(){
pileOfBooks = new ArrayList<>();
}
public void addBook(T book){
pileOfBooks.add(book);
}
public T removeBook(){
//if there is no book in the pile
if(isEmpty()){
System.out.println("Pile is empty.")
return null;
}
T bookRemoved = pileOfBooks.get(0);
pileOfBooks.remove(0);
return bookRemoved;
}
public T topBook(){
//if there is no book in the pile
if(isEmpty()){
System.out.println("Pile is empty.")
return null;
}
return pileOfBooks.get(0);
}
public boolean isEmpty(){
if(size() == 0){
return true;
} else {
return false;
}
}
public int size(){
return pileOfBooks.size();
}
public String toString(){
String contents = "";
for(int i = 0; i < size(); i++){
contents = contents + pileOfBooks.get(i).getNameOfBooks() + ", ";
}
return contents;
}
}
=================================================
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.