Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a book class which is as follows class Book { private int numberOfPages;

ID: 3640693 • Letter: I

Question

I have a book class which is as follows

class Book
{

private int numberOfPages;
private boolean hardBack;
private String title;
private double price;

//constructors
Book()
{
  title = "";
  numberOfPages = 0;
  hardBack = true;
  price = 0.0;
}

Book(int n, boolean h, String t, double p)
{
  numberOfPages = n;
  hardBack = h;
  title = t;
  price = p;
}

int getPages()
{
  return numberOfPages;
}

void setPage(int n)
{
  numberOfPages = n;
}

boolean getBack()
{
  return hardBack;
}

void setBack(boolean h)
{
  hardBack = h;
}

String getTitle()
{
  return title;
}

void setTitle(String t)
{
  title = t;
}

double getPrice()
{
  return price;
}

void setPrice( double p)
{
  price = p;
}


@Override
public boolean equals(Object obj)
  {
   if (obj == this)
    {
     return true;
    }
   if (obj == null || obj.getClass() != this.getClass())
    {
     return false;
    }
   Book book = (Book) obj;
   return (book.getPages() == this.getPages()
      && book.getPrice() == this.getPrice()
      && book.getBack() == this.getBack() && book.getTitle()
      .equalsIgnoreCase(this.getTitle()));
  }

@Override
public String toString()
  {
   return ("No Of Pages: " + this.getPages() + " " + "Price of Book: "
       + this.getPrice() + " " + "Hard Back: " + this.getBack()
    + " " + "Book Title: " + this.getTitle());
  }


}

Now using this book class with a weel-defined equals method, create a Stack that holds at least 5 Book Objects. use each of the following Stack methods:

1) empty

2)peek

3)pop(demonstrate proper exception handling )

4)push

5)search

Can someone please help me with this ?

Explanation / Answer

I wrote this main function that should well present the stack behaviour. I hope this is what you requested. if not, give me a shout: public static void main(String[] args) { Stack stack = new Stack(); Book b1 = new Book(100, false, "Lord of the Rings", 120); Book b2 = new Book(200, false, "Book 2", 34); Book b3 = new Book(300, true, "Title 3", 55); Book b4 = new Book(400, false, "Name 4", 87); Book b5 = new Book(500, true, "Anything...", 12); System.out.println("Testing of stack begins!"); // empty method System.out.println("Is stack empty? " + stack.isEmpty()); //push methods System.out.println("pushing objects on the stack..."); stack.push(b1); stack.push(b2); stack.push(b3); stack.push(b4); stack.push(b5); //size method - as a bonus System.out.println("Stack size is now: " + stack.size()); //seach System.out.println(b5 + " is " + stack.search(b5) + " places from the top of the stack"); //pop try{ Book pop =stack.pop(); System.out.println("Popped: " + pop); } catch(EmptyStackException e){ System.out.println("Stack was empty before popping..."); } //peek System.out.println("Our current top is: " + stack.peek()); System.out.println("AGAIN, Our current top is: " + stack.peek()); }