//--------------------------------------------------------------------------------------------- // CS100 P2: Q1 // // Class Book is a general class describing properties of all books. It has // fields author, title, year, and notes; a constructor; and methods toString and // showReview. Method toString returns a basic description of of the book; // Method showReview prints an error as books at this level are unreviewed. // Class Book is the superclass for classes Trashy and Literary. // // Classes Trashy and Literary introduce the new variables synopsis and review. // They also override Class Book's showReview method, invoking the new variables. // // Class TestBook demonstrates that the classes work appropriately by instantiating // one example each of class Book, Trashy, and Literary and using method describe to // print a description and review for each of the books in turn. // // Author : Alan Renaud (ajr5@cornell.edu) // Date : 15 July 1999 //--------------------------------------------------------------------------------------------- class Book { protected String author; // author's name protected String title; // book title protected String notes; // category, eg., book, trashy, literary protected int year; // year published // constructor for class Book Book (String a, String t, int y, String n) { author = a; title = t; year = y; notes = n; } //--------------------------------------------------------------------------------------------- // toString // // input : none // return : the author, title, date, and notes for the book // // Returns a basic description of the book //--------------------------------------------------------------------------------------------- public String toString() { return author + ", " + title + "\nPublished: " + year + ", " + notes; } //--------------------------------------------------------------------------------------------- // showReview // // input : none // return : none // // Prints an error message since objects of class Book are not reviewed. //--------------------------------------------------------------------------------------------- public void showReview() { System.err.println("Sorry, but objects of class Book are not reviewed."); } } // -------End class Book // -------Start class Literary class Literary extends Book { private String litSumry; // indicates whether book is good or bad private String litRev; // reviewer's notes // constructor for subclass Literary Literary (String a, String t, int y, String n, String s, String r) { super(a, t, y, n); litSumry = s; litRev = r; } //--------------------------------------------------------------------------------------------- // showReview // // input : none // return : none // // Outputs to screen the summary, and the reviewer's notes. //--------------------------------------------------------------------------------------------- public void showReview() { System.out.println(litSumry); System.out.println(litRev); } } // -------End class Literary // -------Start class Trashy class Trashy extends Book { private String trashSumry; // indicates whether book is good or bad private String trashRev; // reviewer's notes // constructor for subclass Trashy Trashy (String a, String t, int y, String n, String s, String r) { super(a, t, y, n); trashSumry = s; trashRev = r; } //--------------------------------------------------------------------------------------------- // showReview // // input : none // return : none // // Outputs to screen the summary, and the reviewer's notes. //--------------------------------------------------------------------------------------------- public void showReview() { System.out.println(trashSumry); System.out.println(trashRev); } } // -------End class Trashy // -------Start class TestBook class TestBook { //--------------------------------------------------------------------------------------------- // describe // // input : object of type Book // return : none // // Prints a description and review of its parameter by calling its toString() and // showReview() methods. //--------------------------------------------------------------------------------------------- static void describe (Book item) { System.out.println("\n\n" + item.toString()); item.showReview(); } public static void main (String[] args) { TokenReader in = new TokenReader(System.in); // Create one object each of type Book, Trashy, and Literary Book loftus = new Book("John Lewis, William Loftus ", "Java Software Solutions", 1997, "Book"); Trashy fabio = new Trashy("Fabio", "Mysterious", 1998, "Trashy", "A sizzling story of suspense and desire.", "\"The dialog is silly, the romance, contrived, and the plot, unrealistic: \nNo one is going to make love in the snow and fall asleep in it. \""); Literary follett = new Literary("Ken Follett", "The Pillars Of The Earth", 1989, "Literary", "The epic story of the building of a cathedral in 12th century England.", "\"A novel of majesty and power...Will hold you, fascinate you, surround you.\""); // Describe the objects describe(loftus); describe(fabio); describe(follett); in.waitUntilEnter(); } }