Java

OOP GUIDE / WORK

 

BOOK & TEXTBOOK CLASSES - SOLUTIONS

 

 

TASK – PART 1 – QUESTION

 

No solution needed.

 

 

TASK – PART 2 – BOOK CLASS

No solution needed.


 

TASK – PART 3 – IMPLEMENT THE TEXTBOOK CLASS

 

Here is my solution:

 

public class Textbook extends Book

{

     private int edition;

    

     public Textbook(String title, double price, int edition)

     {

           super(title, price);

           this.edition = edition;

     }

    

     //getBookInfo() NOTE:

     //The Book class doesn't have a getPrice() method.  Sneaky!

     //So you must call super's getBookInfo() method.

    

     public String getBookInfo()

     {

           return super.getBookInfo() + "-" + edition; 

     }

    

     public boolean canSubstituteFor(Textbook other)

     {

           if (this.getTitle().equals(other.getTitle()))

           {

                if (edition >= other.getEdition())

                {

                      return true;

                }

           }

           return false;

     }

    

     //getEdition() NOTE

     //The description of the work doesn't mention getEdition()

     //but it is found in the example work.  So you must include it.

    

     public int getEdition()

     {

           return edition;

     }

}

 

 

 

TASK – PART 4 – TESTING

No solution needed.