/*********************************************** * CS2510 Fall 2011 * Lecture #13 * Review and prep for state change ***********************************************/ import tester.Tester; /* +-----------+ | ILoPerson |<------------+ +-----------+ | +-----------+ | / \ | --- | | | ------------------- | | | | +------------+ +----------------+ | | MTLoPerson | | ConsLoPerson | | +------------+ +----------------+ | +------------+ +-| Person first | | | | ILoPerson rest |-+ | +----------------+ v +-------------+ | IPerson |<----------------------+ +-------------+ | +-------------+ | / \ | --- | | | +-------------+ | | APerson | | +-------------+ | | String name | | | int phone | | +-------------+ | / \ | --- | | | ----------------------- | | | | +------------+ +---------------------+ | | Student | | Superhero | | +------------+ +---------------------+ | | double gpa | | String power | | +------------+ | boolean invincibleP | | | IPerson secretID |-+ +---------------------+ */ // IPerson: A union for types of people interface IPerson { // revealSecretID: Returns the secret identity person for this person IPerson revealSecretID(); // revealSecretName: Returns the string name for the secret id String revealSecretName(); // getName: Returns the string representation of the name String getName(); } // APerson: Holds common fields and methods for all people abstract class APerson implements IPerson{ String name; int phone; APerson(String name, int phone) { this.name = name; this.phone = phone; } // getName: Returns the name of the person as a String public String getName() { return this.name; } } // Student: Information and methods for Students class Student extends APerson { double gpa; Student(String name, int phone, double gpa) { super(name, phone); this.gpa = gpa; } // revealSecretID: Error, students have no secret ID public IPerson revealSecretID() { throw new RuntimeException("No one loves you."); } // revealSecretName: Error, students have no secret Name public String revealSecretName() { throw new RuntimeException("No one loves you still."); } } // Superhero: Holds information and the secret identitiy for the superhero class Superhero extends APerson { String power; boolean invincibleP; IPerson secretID; Superhero(String name, int phone, String power, boolean invincibleP, IPerson secretID) { super(name,phone); this.power = power; this.invincibleP = invincibleP; this.secretID = secretID; } // revealSecretID: Returns the IPerson for th secret identity public IPerson revealSecretID() { return this.secretID; } // revealSecretName: Returns the string for the name of the secret identity public String revealSecretName() { return this.secretID.getName(); } } // ILoP: Contains a list union for people interface ILoP { // Returns a list of all people's secret identities public ILoP revealAll(); } // MtLoP: The empty list element for people class MtLoP implements ILoP { MtLoP() { } // revealAll: Returns am empty list as there is nothing to reveal public ILoP revealAll() { return new MtLoP(); } } // ConsLoP: The cons list element containing one person class ConsLoP implements ILoP { IPerson first; ILoP rest; ConsLoP(IPerson first, ILoP rest) { this.first = first; this.rest = rest; } // revealAll: Returns the secret ID for the first elements with the secret IDs for the rest public ILoP revealAll() { return new ConsLoP(first.revealSecretID(),rest.revealAll()); } } // Examples of people class PersonExamples { IPerson john = new Student("John",7,6.3); IPerson stevie = new Student("Stevie Ray", 5551212, 3.2); IPerson peterp = new Student("Peter Parker", 1234, 2.8); IPerson spiderman = new Superhero("Spiderman",3,"Spidey power",false,peterp); IPerson captnfalcon = new Superhero("Captain Falcon",99999999,"Falcon Punch",true,spiderman); ILoP mtl = new MtLoP(); ILoP students = new ConsLoP(john,new ConsLoP(stevie,new ConsLoP(peterp,mtl))); ILoP sheros = new ConsLoP(spiderman,new ConsLoP(captnfalcon,mtl)); ILoP mixlist = new ConsLoP(spiderman,students); ILoP sherossecret = new ConsLoP(peterp,new ConsLoP(spiderman,mtl)); // Tests for revealSecretID boolean testRevealSecretID(Tester t) { return t.checkExpect(spiderman.revealSecretID(), peterp) && t.checkExpect(captnfalcon.revealSecretID(), spiderman) && t.checkException(new RuntimeException("No one loves you."), john, "revealSecretID") && t.checkException(new RuntimeException("No one loves you."), peterp, "revealSecretID"); } // Tests for revealSecretName boolean testRevealSecretName(Tester t) { return t.checkExpect(spiderman.revealSecretName(), peterp.getName()) && t.checkExpect(captnfalcon.revealSecretName(), spiderman.getName()) && t.checkException(new RuntimeException("No one loves you still."), john, "revealSecretName") && t.checkException(new RuntimeException("No one loves you still."), peterp, "revealSecretName"); } // Tests for getName boolean testGetName(Tester t) { return t.checkExpect(john.getName(),"John") && t.checkExpect(stevie.getName(),"Stevie Ray") && t.checkExpect(spiderman.getName(), "Spiderman"); } // Tests for revealAll boolean testRevealAll(Tester t) { return t.checkExpect(sheros.revealAll(),sherossecret) && t.checkExpect(mtl.revealAll(),mtl) && t.checkException(new RuntimeException("No one loves you."),students,"revealAll"); } }