import tester.*; /** * HtDC Lectures * Lecture 4: Methods for simple classes * * Copyright 2013 Viera K. Proulx * This program is distributed under the terms of the * GNU Lesser General Public License (LGPL) * * @since 29 August 2013 */ /* +---------------+ | Book | +---------------+ | String title | | String author | | int price | +---------------+ */ // to represent a book in a bookstore class Book { String title; String author; int price; // the constructor Book(String title, String author, int price) { this.title = title; this.author = author; this.price = price; } /* TEMPLATE: ... this.title ... -- String ... this.author ... -- String ... this.price ... -- int Methods: ... this.salePrice(int) ... -- int */ // compute the sale price of this book given today's discount // in percent of the priginal price int salePrice(int discount) { return this.price - (this.price * discount) / 100; } } // examples and tests for the class hierarchy that represents // books and authors class ExamplesBooks { ExamplesBooks() {} // examples of books Book htdp = new Book("HtDP", "FFK", 60); Book beaches = new Book("Beaches", "PC", 20); // test the method salePrice for the class Book boolean testSalePrice(Tester t) { return t.checkExpect(this.htdp.salePrice(30), 42) && t.checkExpect(this.beaches.salePrice(20), 16); } }