/** * Example of a generic Book Class * @author Brad Rippe * @version 1.0 */ public class Book { public static final int MAXPAGES = 500; // static variables belong to the class // all Books will have the same MAXPAGES private String preface; // instance variable private String contents; // (attributes) private String[] bodyPages; private String glossary; /** * Default constructor - creates a book * Takes zero parameters and initializes all instance variables * to there default values */ public Book() { // default constructor - no parameters preface = null; // if we don't initialize the variable contents = null; // the jvm will do it for us. bodyPages = null; // all primitive ints will be initalized to 0 glossary = null; // all boolean type will be initalized to false } // all objects will be initialized to null /** * Overloaded constructor - Overloaded methods have the * same name and return type as a previously defined method * with a different parameter-list. * takes one parameter * Constructs a book object and initialize all variables to their * default vaults, except the Book's bodyPages. BodyPages are set to * the pages passed to the constructor * @param bodyPages the contents of the book. */ public Book( String[] bodyPages ) { // overload the this.bodyPages = bodyPages; // constructor } /** * Overloaded constructor - constructs a Book object * takes four parameters * @param preface text of the preface * @param contents the table of contents * @param pages the pages in the body of the book * @param glossary the book's glossary */ public Book( String preface, String contents, String[] pages, String glossary) { this.preface = preface; this.contents = contents; this.bodyPages = pages; this.glossary = glossary; } /** * Accessors or getters return values of private data. * Return the preface. * @return the book's preface */ public String getPreface() { return preface; } /** * Return the pages * @return book's pages */ public String[] getBodyPages() { return bodyPages; } /** * Return the glossary * @return the book's glossary */ public String getGlossary() { return glossary; } /** * Mutators or Setters * The key word "this" reference to the current object instance. * Thus we have two variables in the method body, one variable is * a local variable (method scope) and on is an instance variable * (class scope). * Sets the book's preface * @param preface new preface */ public void setPreface(String preface) { this.preface = preface; } /** * Sets the book's pages. * @param bodyPages new body pages */ public void setBodyPages(String[] bodyPages) { this.bodyPages = bodyPages; } /** * Sets the book's glossary. * @param glossary new glossary */ public void setGlossary(String glossary) { this.glossary = glossary; } } // Book object