/*Date: Thu, 10 Mar 2005 15:59:40 +0000 (UTC) From: John B. Clements Newsgroups: ccs.courses.csu213 Subject: class code 2005-03-07 */ /* +-------------------+ | Airport | +-------------------+ | String name | | AirportList dests | +-------------------+ */ // airport class Airport { String name; AirportList dests; Airport(String name, AirportList dests) { this.name = name; this.dests = dests; } boolean same(Object other) { if (other instanceof Airport) return (this.name.equals(((Airport)other).name)) && true; else return false; } void addFlight(Airport newDest){ this.dests = new ALCons(newDest,this.dests); } } /* +-------------+ | AirportList |<------------+ +-------------+ | +-------------+ | / \ | --- | | | ------------------- | | | | +------+ +------------------+ | | ALMT | | ALCons | | +------+ +------------------+ | +------+ | Airport first | | | AirportList rest |-+ | +------------------+ | | | | +--+ */ abstract class AirportList { // is 'this' object extensionally equivalent to 'other'? abstract boolean same(Object other); } class ALMT extends AirportList { ALMT() { } boolean same(Object other) { if (other instanceof ALMT) return true; else return false; } } class ALCons extends AirportList { Airport first; AirportList rest; ALCons(Airport first, AirportList rest) { this.first = first; this.rest = rest; } boolean same(Object other) { if (other instanceof ALCons) return (this.first.same(((ALCons)other).first)) && (this.rest.same(((ALCons)other).rest)) && true; else return false; } } class Examples{ Airport logan = new Airport("Logan",new ALMT()); Airport manchester = new Airport("Manchester",new ALMT()); Airport lax = new Airport("LAX",new ALMT()); Airport denver = new Airport("Denver",new ALMT()); boolean runTests(){ this.denver.addFlight(this.logan); this.logan.addFlight(this.denver); return true; } boolean t1 = runTests(); boolean t2 = this.logan.dests.same(new ALCons(this.denver,new ALMT())); boolean t3 = this.logan.dests.same(new ALCons (new Airport("Denver", new ALMT()), new ALMT())); }