Question 2

Provided code:

public class Book{

    // Title of the book
    private String title;

    // price of the book
    private double price;

    // Creates a new Book with given title and price
    public Book(String bookTitle, double bookPrice){

    }
    // Returns the title of the book
    public String getTitle(){
        return this.title;
    }
    // Returns a string containing the title and price of the Book
    public String getBookInfo(){
        return title + "-" + price;
    }
    // There may be instance variables, constructors, and methods that are not shown.
    
}

Prompt

You will write a class Textbook, which is a subclass of Book.

A Textbook has an edition number, which is a positive integer used to identify different version of the book. The getBookInfo method, when called on a Textbook, returns a string that also includes the edition information, as shown in the example.

Information about the book title and price must be maintained in the book class. Information about the edition must be maintained in the Textbook class.

The Textbook class contains an additional method, canSubstituteFor, which returns true if a Textbook is a valid substitute for the two Textbook objects have the same title and if the edition of the current Textbook is greater than or equal to the edition of the parameter.

Answer

public class Textbook extends Book{

    private int edition;

    public Textbook(String tbTitle, double tbPrice,int tbEdition){
        super(tbTitle, tbPrice);
        edition = tbEdition;
    }
    public int getEdition(){
        return edition;
    }
    public boolean canSubstituteFor(int otherEdition, String otherTitle){
        return otherTitle.equals(getTitle()) && this.edition >= otherEdition;
    }
    public String getBookInfo(){
        return super.getBookInfo() + "–" + edition;
    }
}


Textbook CSP = new Textbook("CSP Book", 99.99, 1);
Textbook CSA = new Textbook("CSA Book", 99.99, 2);

// Testing the canSubstituteFor method
boolean sub = CSA.canSubstituteFor(1, "CSP Book");
System.out.println("Is CSP a valid substitute for CSA: " + sub);
Is CSP a valid substitute for CSA: false

Points

  1. Declares class header 1/1
  2. Declares constructor header 1/1
  3. Constructor class super as the first line with appropriate parameters 1/1
  4. Declares appropriate private instance variable and uses appropriate parameter to initialize it 1/1
  5. Declares at least one required method and all declared headers are correct 1/1
  6. getEdition returns value of instance variable 0/1 – fixed 1/1
    • did not correctly read directions
  7. canSubstituteFor determines whether true or false should be returned based on comparison of book titles and editions 1/1
  8. getBookInfo calls super.getBookInfo 1/1
  9. Constructs information string 1/1

Initial score: 8/9

Takeaways

  • ‘extends’ creates a subclass
  • use constructors to define variables
  • use super to get variable from super class

Ryan 0.9

  • good talking volume
  • could have more in depth descriptions