Java

OOP GUIDE / WORK

 

BOOK & TEXTBOOK CLASSES

 

Topics

  • Inheritance

 

Source

  • This question was part of the 2022 AP exam free response questions.  You can find all four of the original questions on the AP Free Response site.

 

 

TASK – PART 1 – QUESTION

 

Click here for a PDF containing the question relating to the Book and Textbook classes.  Before implementing your solution, do Part 2.

 

 

TASK – PART 2 – BOOK CLASS

Copy the following Book class code so that you can implement the Textbook class.

public class Book

{

     private String title;

     private double price;

    

     public Book(String bookTitle, double bookPrice)

     {

           title = bookTitle;

           price = bookPrice;

     }

 

     public String getTitle()

     {

           return title;

     }

    

     public String getBookInfo()

     {

           return title + "-" + price;

     }

}

 

 

 

 

TASK – PART 3 – IMPLEMENT THE TEXTBOOK CLASS

 

Go implement the Textbook class.

 

TASK – PART 4 – TESTING

Copy and paste the following code that will test your Textbook class.  Note that the program below matches the example method calls in the PDF document.  You should compare your output to the output in the PDF document to see if they match.

 

 

public class TextbookTester

{

     public static void main(String[] args)

     {

           Textbook bio2015 = new Textbook("Biology", 49.75, 2);

           Textbook bio2019 = new Textbook("Biology", 39.75, 3);

           System.out.println(bio2019.getEdition());

           System.out.println(bio2019.getBookInfo());

           System.out.println(bio2019.canSubstituteFor(bio2015));

           System.out.println(bio2015.canSubstituteFor(bio2019));

           Textbook math = new Textbook("Calculus", 45.25, 1);

           System.out.println(bio2015.canSubstituteFor(math));

     }

}