/** * CS U213 Assignment for week 12. * * @author William D Clinger * @version 0 * * 7 April 2004 * * */ import java.util.*; //////////////////////////////////////////////////////////////// // // Week12 // //////////////////////////////////////////////////////////////// public class Week12 { public static void main (String[] args) { new TestPerson().go(); new TestMovie().go(); new StudentTests().run(); } } //////////////////////////////////////////////////////////////// // // Tests for the Person, Actor, and Actress classes // //////////////////////////////////////////////////////////////// class TestPerson implements Testable { TestPerson () { } // run this batch of tests void go () { Tester tester = new Tester (); tester.runTests (this); } // the tests that will be run by the given Tester public void tests (Tester t) { Person marlowe = new Person ("Philip Marlowe", true); Person vivian = new Person ("Vivian Rutledge", false); Actor bogart = new Actor ("Humphrey Bogart"); Actress bacall = new Actress ("Lauren Bacall"); t.test (marlowe.name(), "Philip Marlowe"); t.test (vivian.name(), "Vivian Rutledge"); t.test (bogart.name(), "Humphrey Bogart"); t.test (bacall.name(), "Lauren Bacall"); t.test (marlowe.isMale(), true); t.test (marlowe.isFemale(), false); t.test (vivian.isMale(), false); t.test (vivian.isFemale(), true); t.test (bogart.isMale(), true); t.test (bogart.isFemale(), false); t.test (bacall.isMale(), false); t.test (bacall.isFemale(), true); t.test (marlowe.compareTo (vivian) < 0, true); t.test (marlowe.compareTo (bogart) > 0, true); t.test (marlowe.compareTo (bacall) > 0, true); t.test (bogart.compareTo (vivian) < 0, true); t.test (bogart.compareTo (bogart) == 0, true); t.test (bogart.compareTo (bacall) < 0, true); t.test (bacall.compareTo (marlowe) < 0, true); t.test (bacall.compareTo (bogart) > 0, true); t.test (bacall.compareTo (bacall) == 0, true); t.test (vivian.equals (marlowe), false); t.test (vivian.equals (vivian), true); t.test (vivian.equals (bacall), false); t.test (bogart.equals (vivian), false); t.test (bogart.equals (new Person ("Humphrey Bogart", true)), true); t.test (bacall.equals (vivian), false); t.test (bacall.equals (bacall), true); // Although the hashCode() method isn't guaranteed to return // anything in particular, we do want it to return something // without blowing up, and it should consistently return the // same thing. t.test (marlowe.hashCode(), marlowe.hashCode()); t.test (bogart.hashCode(), bogart.hashCode()); t.test (bacall.hashCode(), bacall.hashCode()); t.test (vivian.toString(), vivian.toString()); t.test (bogart.toString(), bogart.toString()); t.test (bacall.toString(), bacall.toString()); } } //////////////////////////////////////////////////////////////// // // Tests for the Movie and Role classes // //////////////////////////////////////////////////////////////// class TestMovie implements Testable { TestMovie () { } // run this batch of tests void go () { Tester tester = new Tester (); tester.runTests (this); } public void tests (Tester t) { Person algar = new Person ("James Algar", true); Movie fantasia = new SimpleMovie ("Fantasia", 1940, algar); Person hawks = new Person ("Howard Hawks", true); Map sleepy = new HashMap (); Movie sleep = new StarMovie ("The Big Sleep", 1946, hawks, sleepy); Actor bogart = new Actor ("Humphrey Bogart"); Actress bacall = new Actress ("Lauren Bacall"); Role marlowe = new Role ("Philip Marlowe", sleep); Role vivian = new Role ("Vivian Rutledge", sleep); sleepy.put (bogart, marlowe); sleepy.put (bacall, vivian); Person curtiz = new Person ("Michael Curtiz", true); Map cast2 = new TreeMap (); Movie casablanca = new StarMovie ("Casablanca", 1942, curtiz, cast2); Actress bergman = new Actress ("Ingrid Bergman"); Role rick = new Role ("Rick Blaine", casablanca); Role ilsa = new Role ("Ilsa Lund Laszlo", casablanca); cast2.put (bogart, rick); cast2.put (bergman, ilsa); t.test (fantasia.title(), "Fantasia"); t.test (sleep.title(), "The Big Sleep"); t.test (fantasia.year(), 1940); t.test (sleep.year(), 1946); t.test (fantasia.director().equals(algar), true); t.test (sleep.director().equals(hawks), true); t.test (fantasia.cast().size(), 0); t.test (sleep.cast().equals(sleepy)); t.test (marlowe.name(), "Philip Marlowe"); t.test (vivian.movie().equals (sleep), true); t.test (sleep.cast().size(), 2); t.test (sleep.cast().get(bogart).equals(marlowe), true); } } //////////////////////////////////////////////////////////////// // // Tester // //////////////////////////////////////////////////////////////// interface Testable { void tests (Tester t); } class Tester { int n; int errors; Tester () { this.n = 1; this.errors = 0; } void runTests (Testable f) { n = 1; try { f.tests (this); } catch (Throwable e) { System.out.println ("Threw exception during test " + n); System.out.println (e); } finally { done(); } } void test (boolean b1, boolean b2) { test (b1 == b2); } void test (int i, int j) { test (i == j); } void test (Object s1, String s2) { test (s2.equals (s1)); } void test (boolean b) { if (! b) { System.out.println ("***** Failed test " + n + " *****"); errors = errors + 1; } n = n + 1; } void done () { if (errors > 0) System.out.print ("Failed " + errors + " out of "); else System.out.print ("Passed all "); System.out.println (n + " tests."); } } //////////////////////////////////////////////////////////////// // // Person // //////////////////////////////////////////////////////////////// class Person implements Comparable { private String name; private boolean male; Person (String name, boolean male) { this.name = name; this.male = male; } // returns the name of this person String name () { return this.name; } // is this Person male? boolean isMale () { return this.male; } // is this Person female? boolean isFemale () { return ! (isMale()); } // how does this Person's name compare with another's? public int compareTo (Object x) { Person a = (Person) x; return this.name().compareTo (a.name()); } // is this Person the same as x? public boolean equals (Object x) { if (x instanceof Person) return this.name.equals (((Person) x).name); else return false; } // returns a hash code for this Actor public int hashCode () { return this.name.hashCode(); } // returns a string representation of this Person public String toString () { return this.name; } } //////////////////////////////////////////////////////////////// // // Actor and Actress // //////////////////////////////////////////////////////////////// class Actor extends Person { Actor (String name) { super (name, true); } Actor (String name, boolean male) { super (name, male); } } class Actress extends Actor { Actress (String name) { super (name, false); } } //////////////////////////////////////////////////////////////// // // Movie // //////////////////////////////////////////////////////////////// interface Movie extends Comparable { // returns the title of this movie String title(); // returns the year this movie was made int year(); // returns the Person who directed this movie Person director(); // the cast of this movie maps Actor objects to Role objects Map cast(); } // A class for movies whose cast is unknown. class SimpleMovie implements Movie { private String name; // title of movie private int date; // year it was made private Person directed; // who directed it Map cast; // who played what role SimpleMovie (String name, int date, Person directed) { this.name = name; this.date = date; this.directed = directed; this.cast = new HashMap(); } // returns the title of this movie public String title() { return this.name; } // returns the year this movie was made public int year() { return this.date; } // returns the Person who directed this movie public Person director() {return this.directed; } // returns the cast of this movie as a map from actors to roles public Map cast() { return this.cast; } // is this Movie the same as that one? public boolean equals (Object that) { if (that instanceof Movie) { Movie m = (Movie) that; return this.title().equals (m.title()) && this.year() == m.year() && this.director().equals (m.director()) && this.cast() == m.cast(); // avoid circularity! } else return false; } // returns a hash code for this Movie public int hashCode () { return this.title().hashCode() + this.year(); } // alphabetical ordering on Movie titles; not case-sensitive public int compareTo (Object that) { Movie m = (Movie) that; return this.title().compareToIgnoreCase (m.title()); } } // A class for movies whose cast is known. class StarMovie extends SimpleMovie implements Movie { StarMovie (String name, int date, Person directed, Map stars) { super (name, date, directed); this.cast = stars; } } //////////////////////////////////////////////////////////////// // // Role // //////////////////////////////////////////////////////////////// class Role { private String name; // the name of the character private Movie movie; // the movie in which this Role appears Role (String name, Movie movie) { this.name = name; this.movie = movie; } // the name of this Role String name () { return this.name; } // the movie in which this Role appears Movie movie () { return this.movie; } // is this Role the same as that one? public boolean equals (Object that) { if (that instanceof Role) { Role r = (Role) that; return this.name.equals (r.name) && this.movie.equals (r.movie); } else return false; } // returns a hash code for this Role public int hashCode () { return this.name.hashCode(); } // returns a string representation of this Role public String toString () { return this.name; } }