/* Csu213, Wednesday 2/14/07 We continue the examples from last time... Last time we covered constructors for convenience and input format conversion. They are also useful for input validation. We will see this example fully worked out next week, when we move to full Java using the Eclipse IDE. */ /********************************************************************** // Represents a simple clock time class ClockTime{ int hours; int minutes; // Simple Constructor with data verification ClockTime(int hours, int minutes){ if((0 <= hours && hours <= 23) && (0 <= minutes && minutes <= 59)){ this.hours = hours; this.minutes = minutes; }else throw new IllegalArgumentException("Illegal Time value"); } // Uses the above constructor for verification ClockTime(int justMin){ this(justMin/60, justMin%60); } } class Examples{ ClockTime t1 = new ClockTime(9, 15); ClockTime t2 = new ClockTime(9*60+15); ClockTime t3 = new ClockTime(10, 20); ClockTime t4 = new ClockTime(10*60+20); boolean test = ((this.t1.hours == this.t2.hours) && (this.t1.minutes == this.t2.minutes) && (this.t3.hours == this.t4.hours) && (this.t3.minutes == this.t4.minutes)); } ***********************************************************************/ /* Circularly Referential Data Definitions We modify our Library example... */ // Represents a Library Book class Book{ String title; int year; LoA authors; Book(String title, LoA authors, int year){ this.title = title; this.authors = authors; this.year = year; } // Is this book the same as that one boolean sameBook(Book that){ return ((this.title.equals(that.title)) && (this.year == that.year) && (this.authors.same(that.authors))); } } // Represents a List of Books abstract class LoB{ // Is this list the same as the given list? abstract boolean same(LoB that); abstract boolean sameMt(MtLoB that); abstract boolean sameCons(ConsLoB that); } // Represents an empty List of Books class MtLoB extends LoB{ MtLoB(){} // Is this list the same as the given list? boolean same(LoB that){ return that.sameMt(this); } boolean sameMt(MtLoB that){ return true; } boolean sameCons(ConsLoB that){ return false; } } // Represents a non-empty List of Books class ConsLoB extends LoB{ Book first; LoB rest; ConsLoB(Book first, LoB rest){ this.first = first; this.rest = rest; } // Is this list the same as the given list? boolean same(LoB that){ return that.sameCons(this); } boolean sameMt(MtLoB that){ return false; } boolean sameCons(ConsLoB that){ return ((this.first.sameBook(that.first)) && (this.rest.same(that.rest))); } } // Represents a Book Author class Author{ String name; int dob; LoB books; Author(String name, int dob, LoB books){ this.name = name; this.dob = dob; this.books = books; } Author(String name, int dob){ this(name, dob, new MtLoB()); } // Add a book to this Author's List of Books // EFFECT: this.books will have a new book added void addBook(Book b){ this.books = new ConsLoB(b, this.books); } // Is this author the same as that one boolean sameAuthor(Author that){ return ((this.name.equals(that.name)) && (this.dob == that.dob) // ** This would cause an infinite // loop, so we don't compare the books list // && (this.books.same(that.books)) ); } } // Represents a List of Authors abstract class LoA{ // Is this list the same as the given list? abstract boolean same(LoA that); abstract boolean sameMt(MtLoA that); abstract boolean sameCons(ConsLoA that); // Add the given Book to each Author in this List of Authors abstract void addBook(Book b); } // Represents an empty List of Authors class MtLoA extends LoA{ MtLoA(){} // Add the given Book to each Author in this empty List of Authors void addBook(Book b){ // Nothing to do here... } // Is this list the same as the given list? boolean same(LoA that){ return that.sameMt(this); } boolean sameMt(MtLoA that){ return true; } boolean sameCons(ConsLoA that){ return false; } } // Represents a non-empty List of Authors class ConsLoA extends LoA{ Book first; LoA rest; ConsLoA(Book first, LoA rest){ this.first = first; this.rest = rest; } // Add the given Book to each Author in this non-empty List of Authors void addBook(Book b){ this.first.addBook(b); this.rest.addBook(b); } // Is this list the same as the given list? boolean same(LoA that){ return that.sameCons(this); } boolean sameMt(MtLoA that){ return false; } boolean sameCons(ConsLoA that){ return ((this.first.sameAuthor(that.first)) && (this.rest.same(that.rest))); } } // We assume that Authors come first... then they write books. class Examples2{ Author mf = new Author("MF",1930); Author df = new Author("DF",1945); LoA loa = new ConsLoA(mf, new ConsLoA(df, new MtLoA())); Book ll = new ; } public class notes021407{ }