/*********************************************** * CS2510 Spring 2011 * Lecture #13 * Mutation and Assignment ***********************************************/ import tester.*; // Represents a Person class Person{ String name; String phone; Person(String name, String phone){ this.name = name; this.phone = phone; } // Is this the same Person as that? boolean samePerson(Person that){ return (this.name.equals(that.name) && this.phone.equals(that.phone)); } // Update this person's Phone Number Person updatePhone(String phone){ return new Person(this.name, phone); } // EFFECT : Changes the phone number for this person // Nothing (void) is returned... void changePhone(String phone){ this.phone = phone; } } // Represents a list of Person(s) interface ILoP{ // Does this list contain the given person? boolean contains(Person p); // Update the phone for the given name ILoP updatePhone(String name, String phone); // Change the phone for the given name void changePhone(String name, String phone); } // Represents the empty list of Person(s) class MtLoP implements ILoP{ // Does this empty list contain the given person? public boolean contains(Person p){ return false; } // Update the phone for the given name public ILoP updatePhone(String name, String phone){ throw new RuntimeException("Name not found: "+name); } // Update the phone for the given name public void changePhone(String name, String phone){ } } // Represents a nonempty list of Person(s) class ConsLoP implements ILoP{ Person first; ILoP rest; ConsLoP(Person first, ILoP rest){ this.first = first; this.rest = rest; } // Does this list contain the given person? public boolean contains(Person p){ return (this.first.samePerson(p) || this.rest.contains(p)); } // Update the phone for the given name public ILoP updatePhone(String name, String phone){ if(this.first.name.equals(name)){ return new ConsLoP(this.first.updatePhone(phone), this.rest); }else{ return new ConsLoP(this.first, this.rest.updatePhone(name, phone)); } } // Change the phone for the given name public void changePhone(String name, String phone){ if(this.first.name.equals(name)){ this.first.changePhone(phone); this.rest.changePhone(name, phone); }else{ this.rest.changePhone(name, phone); } } } // Examples... Tests class LectureExamples{ LectureExamples(){} Person bryan = new Person("Bryan", "867-5309"); Person chadwick = this.bryan; // Test person equality boolean testPerson(Tester t){ return (t.checkExpect(this.bryan, this.chadwick) && t.checkExpect(this.bryan.samePerson(this.chadwick))); } // Test person update and equality (no mutation) boolean testUpdate(Tester t){ return (t.checkExpect(this.bryan.updatePhone("617-6262"), new Person("Bryan", "617-6262")) && t.checkExpect(this.bryan.samePerson(this.chadwick))); } // Test our list/update methods boolean testUpdateList(Tester t){ return (t.checkExpect(this.friend.updatePhone("Bryan", "617-6262"), new ConsLoP(this.bryan.updatePhone("617-6262"), new ConsLoP(this.chadwick, this.mt)))); } // Reset instances to known/expected values to start void reset(){ this.bryan = new Person("Bryan", "867-5309"); this.chadwick = this.bryan; } // Must reset before testing the mustation... void testChangePhone1(Tester t){ this.reset(); this.bryan.changePhone("617-6262"); t.checkExpect(this.bryan, new Person("Bryan", "617-6262")); t.checkExpect(this.chadwick, new Person("Bryan", "617-6262")); } // Here we just test to make sure nothing changed... void testChangePhone2(Tester t){ this.reset(); t.checkExpect(this.bryan, new Person("Bryan", "867-5309")); } // A few list examples... ILoP mt = new MtLoP(); ILoP friend = new ConsLoP(this.bryan, new ConsLoP(this.chadwick, this.mt)); ILoP stalker = new ConsLoP(this.bryan, new ConsLoP(this.chadwick, this.mt)); }