class Book { String author; String title; int numPages; boolean copyR; Book( String _title, String _author, int _numPages, boolean _copyR ) { this.author = _author; this.title = _title; this.numPages = _numPages; this.copyR = _copyR; } static int readTime( Book b ) { return -99; // stub // ... b.author // ... b.title // ... b.numPages // ... b.copyR } public static void main( String[] args ) { Book b1 = new Book( "The Cat in the Hat", "Seuss", 31, true ); new Book( "The Soul of Wit", "Shorty", 0, false ); System.out.println( "Reading time of b1: expected " + 31*2.0/60 + ", got " + readTime(b1) ); System.out.println( "Reading time of other: expected " + 0 + ", got " + readTime(new Book( "The Soul of Wit", "Shorty", 0, false )) ); // Test vandalize: System.out.println( "vandalized version of b1: expected \"The Cat iuss, by Suess (pp.15) (c)\", " + "got \"" + vandalize(b1).toString() + "\""); System.out.println( "vandalized version of other: expected \"The Sourty, by Shorty (pp.0)\", " + "got \"" + vandalize(new Book( "The Soul of Wit", "Shorty", 0, false )) + "\""); } /** Return a vandalized version of a book. * @param b The book to vandalize. * @return a vandalized copy of `b`. * Currently, vandalization means ripping out half the pages * and having a title that is the original title blended with the author's name. * These details might change in a future version. */ static Book vandalize( Book b ) { return new Book( blend(b.title, b.author), b.author, b.numPages/2, b.copyR ); } /** Return a new string that is a linguistic blend of s1,s2. * @param s1 The first String to be blended. * @param s2 The second String to be blended. * @return a new string that is a linguistic blend of s1,s2: * The first half of s1, followed by the second half of s2. */ // N.B. Really, this utility function should be written and tested in a separate class. // static String blend( String s1, String s2 ) { return s1.substring( 0, s1.length()/2 ).concat( s2.substring( s2.length()/2 ) ); } /** @inherit */ public String toString() { return "new Book(" + this.title + ", " + this.author + ", " + this.numPages + ", " + this.copyR + ")"; //return this.title.concat(", by ").concat(this.author).concat(" (pp. ").concat( Integer.toString(this.numPages)).concat(")").concat( this.copyR ? " (c)" : "" ); // Yeah, I'm being obnoxious by using "concat" rather than "+", // but remember that concat is the *only* java library function which gets // it's own custom syntax to be called. } }