Code is attached to this email, and the screen recording is at http://students.cs.byu.edu/~bjohn3x/Day18.m4vBlake
<Scratch.java>
<Scratch2.java>
<scratch3.java>
Exercise 18.1 corrections and clarification:
'Write the remove [function], which takes a name and modifies the
address book [meaning theBook] such that looking up the name in the
future will always return ["Not Found"].'
Don't forget to remove *all* the matching entries.
Neil
Jay McCarthy wrote:
> Exercise #1 relies in an example that was not done in class. I will post
> the example tonight if someone else does not and I have Internet access.
> Right now I'm at the Palmyra Temple; it's very pretty, but humid. :)
>
> <3
>
> iPhoneから送信
>
> On 2011/06/08, at 14:32, Blake Johnson <bjo...@gmail.com
> <mailto:bjo...@gmail.com>> wrote:
>
>> Code is attached to this email, and the screen recording is at
>> <http://students.cs.byu.edu/~bjohn3x/Day18.m4v>http://students.cs.byu.edu/~bjohn3x/Day18.m4v
>>
>> Blake
>> <Scratch.java>
>> <Scratch2.java>
>> <scratch3.java>
iPhoneから送信
> // A phone book Entry is // new Entry(name, number) // where name and number are strings class Entry { public String name; public String number; public Entry(String name, String number) { this.name = name; this.number = number; } public String toString() { return String.format("(%s, %s)", this.name, this.number); } } // A PhoneBook is // new EmptyLOE() // new ConsLOE(e, b) // where e is an Entry and b is a PhoneBook interface PhoneBook { // lookup : ListOfEntry name -> number // Returns the number of the given person, or "Not Found" if the person // isn't in the PhoneBook public String lookup(String name); } class EmptyLOE implements PhoneBook { public String lookup(String name) { // System.out.format("Answer is %s, should be %s%n", empty.lookup("Bob"), "Not Found"); return "Not Found"; } public String toString() { return "!"; } } class ConsLOE implements PhoneBook { public Entry first; public PhoneBook rest; public ConsLOE(Entry first, PhoneBook rest) { this.first = first; this.rest = rest; } public String toString() { return String.format("%s : %s", this.first, this.rest); } public String lookup(String name) { // System.out.format("Answer is %s, should be %s%n", l1.lookup("Bob"), "111-1111"); // System.out.format("Answer is %s, should be %s%n", l2.lookup("Joe"), "222-2222"); // If the first person is who we're looking for... if (this.first.name == name) { // Return that person's number return this.first.number; } else { // System.out.format("Answer is %s, should be %s%n", l2.lookup("Bob"), "111-1111"); // Otherwise keep looking return this.rest.lookup(name); } } } class scratch4 { // addEntry : String String PhoneBook -> PhoneBook // Returns a new PhoneBook with a new entry for `name' in the front public static PhoneBook addEntry(String name, String number, PhoneBook book) { return new ConsLOE(new Entry(name, number), book); } // nonMutationTests : -> void // Prints results of tests, returns nothing public static void nonMutationTests() { PhoneBook empty = new EmptyLOE(); PhoneBook l1 = new ConsLOE(new Entry("Bob", "111-1111"), empty); PhoneBook l2 = new ConsLOE(new Entry("Joe", "222-2222"), l1); // Bob is not in the empty book: System.out.format("Answer is %s, should be %s%n", empty.lookup("Bob"), "Not Found"); System.out.format("Answer is %s, should be %s%n", l1.lookup("Bob"), "111-1111"); System.out.format("Answer is %s, should be %s%n", l2.lookup("Joe"), "222-2222"); System.out.format("Answer is %s, should be %s%n", l2.lookup("Bob"), "111-1111"); PhoneBook l3 = addEntry("Tim", "333-3333", l2); System.out.format("Answer is %s, should be %s%n", l3.lookup("Tim"), "333-3333"); System.out.format("Answer is %s, should be %s%n", l3.lookup("Bob"), "111-1111"); } // ------------------------------------------------------------------------- // The ONE AND ONLY PHONE BOOK IN TEH WORLD (starts empty) public static PhoneBook theBook = new EmptyLOE(); // This PhoneBook is permanently part of the store // addEntry_mutant : String String -> void // Adds a new entry to THE ONE AND ONLY PHONE BOOK IN TEH WORLD // Demonstrates EXTERNAL mutation. Dangerous. Do not taunt. public static void addEntry_mutant(String name, String number) { // Mutate theBook to add an entry: theBook = addEntry(name, number, theBook); } public static void mutationTests() { // store: theBook = ! System.out.format("Answer is %s, should be %s%n", theBook.lookup("Bob"), "Not Found"); addEntry_mutant("Bob", "111-1111"); // store: theBook = ("Bob", "111-1111") : ! System.out.format("Answer is %s, should be %s%n", theBook, new ConsLOE(new Entry("Bob", "111-1111"), new EmptyLOE())); System.out.format("Answer is %s, should be %s%n", theBook.lookup("Bob"), "111-1111"); addEntry_mutant("Joe", "222-2222"); // store: theBook = ("Joe", "222-2222") : ("Bob", "111-1111") : ! System.out.format("Answer is %s, should be %s%n", theBook.lookup("Joe"), "222-2222"); // What if the above addEntry_mutant lines were in another file? A file // in another directory? What if another file REMOVED lines from theBook? // We'd have to track the store through those files, too! addEntry_mutant("Tim", "333-3333"); // store: theBook = ("Tim", "333-3333") : ("Joe", "222-2222") : ("Bob", "111-1111") : ! System.out.format("Answer is %s, should be %s%n", theBook.lookup("Tim"), "333-3333"); System.out.format("Answer is %s, should be %s%n", theBook.lookup("Bob"), "111-1111"); // This is dangerous, EXTERNAL mutation: users of addEntry_mutant can't // reason just by substitution - they also have to track any part of the // store that addEntry_mutant affects (in this case, theBook) // We also have to reason about the whole program instead of just the // parts that we can see } // ------------------------------------------------------------------------- public static void main(String[] args) { nonMutationTests(); mutationTests(); } }
On Jun 8, 4:25 pm, Neil Toronto <neil.toro...@gmail.com> wrote:
> More code is attached. It occurred to us that many students would not
> have a working isPrime in accumulator style, which is required for
> exercise 2.
>
> Neil
>
>
>
>
>
>
>
>
>
> Jay McCarthy wrote:
> > Awesome
>
> > iPhoneから送信
>
> > On 2011/06/08, at 15:50, Neil Toronto <neil.toro...@gmail.com> wrote:
>
> >> I've attached an example, all 145 lovely lines of it. It would be shorter, but the comments near the end are meant to be instructional, and it does store-tracking.
>
> >> Exercise 18.1 corrections and clarification:
>
> >> 'Write the remove [function], which takes a name and modifies the address book [meaning theBook] such that looking up the name in the future will always return ["Not Found"].'
>
> >> Don't forget to remove *all* the matching entries.
>
> >> Neil
>
> >> Jay McCarthy wrote:
> >>> Exercise #1 relies in an example that was not done in class. I will post the example tonight if someone else does not and I have Internet access. Right now I'm at the Palmyra Temple; it's very pretty, but humid. :)
> >>> <3
> >>> iPhoneから送信
> >>> On 2011/06/08, at 14:32, Blake Johnson <bjoh...@gmail.com <mailto:bjoh...@gmail.com>> wrote:
> >>>> Code is attached to this email, and the screen recording is at <http://students.cs.byu.edu/~bjohn3x/Day18.m4v>http://students.cs.byu.edu/~bjohn3x/Day18.m4v
>
> >>>> Blake
> >>>> <Scratch.java>
> >>>> <Scratch2.java>
> >>>> <scratch3.java>
> >> // A phone book Entry is // new Entry(name, number) // where name and number are strings class Entry { public String name; public String number; public Entry(String name, String number) { this.name = name; this.number = number; } public String toString() { return String.format("(%s, %s)", this.name, this.number); } } // A PhoneBook is // new EmptyLOE() // new ConsLOE(e, b) // where e is an Entry and b is a PhoneBook interface PhoneBook { // lookup : ListOfEntry name -> number // Returns the number of the given person, or "Not Found" if the person // isn't in the PhoneBook public String lookup(String name); } class EmptyLOE implements PhoneBook { public String lookup(String name) { // System.out.format("Answer is %s, should be %s%n", empty.lookup("Bob"), "Not Found"); return "Not Found"; } public String toString() { return "!"; } } class ConsLOE implements PhoneBook { public Entry first; public PhoneBook rest; public ConsLOE(Entry first, PhoneBook rest) { this.first = first
>
> ; this.rest = rest; } public String toString() { return String.format("%s : %s", this.first, this.rest); } public String lookup(String name) { // System.out.format("Answer is %s, should be %s%n", l1.lookup("Bob"), "111-1111"); // System.out.format("Answer is %s, should be %s%n", l2.lookup("Joe"), "222-2222"); // If the first person is who we're looking for... if (this.first.name == name) { // Return that person's number return this.first.number; } else { // System.out.format("Answer is %s, should be %s%n", l2.lookup("Bob"), "111-1111"); // Otherwise keep looking return this.rest.lookup(name); } } } class scratch4 { // addEntry : String String PhoneBook -> PhoneBook // Returns a new PhoneBook with a new entry for `name' in the front public static PhoneBook addEntry(String name, String number, PhoneBook book) { return new ConsLOE(new Entry(name, number), book); } // nonMutationTests : -> void // Prints results of tests, returns nothing public static void nonMutationTest
> s() { PhoneBook empty = new EmptyLOE(); PhoneBook l1 = new ConsLOE(new Entry("Bob", "111-1111"), empty); PhoneBook l2 = new ConsLOE(new Entry("Joe", "222-2222"), l1); // Bob is not in the empty book: System.out.format("Answer is %s, should be %s%n", empty.lookup("Bob"), "Not Found"); System.out.format("Answer is %s, should be %s%n", l1.lookup("Bob"), "111-1111"); System.out.format("Answer is %s, should be %s%n", l2.lookup("Joe"), "222-2222"); System.out.format("Answer is %s, should be %s%n", l2.lookup("Bob"), "111-1111"); PhoneBook l3 = addEntry("Tim", "333-3333", l2); System.out.format("Answer is %s, should be %s%n", l3.lookup("Tim"), "333-3333"); System.out.format("Answer is %s, should be %s%n", l3.lookup("Bob"), "111-1111"); } // ------------------------------------------------------------------------- // The ONE AND ONLY PHONE BOOK IN TEH WORLD (starts empty) public static PhoneBook theBook = new EmptyLOE(); // This PhoneBook is permanently part of the store // addEnt
> ry_mutant : String String -> void // Adds a new entry to THE ONE AND ONLY PHONE BOOK IN TEH WORLD // Demonstrates EXTERNAL mutation. Dangerous. Do not taunt. public static void addEntry_mutant(String name, String number) { // Mutate theBook to add an entry: theBook = addEntry(name, number, theBook); } public static void mutationTests() { // store: theBook = ! System.out.format("Answer is %s, should be %s%n", theBook.lookup("Bob"), "Not Found"); addEntry_mutant("Bob", "111-1111"); // store: theBook = ("Bob", "111-1111") : ! System.out.format("Answer is %s, should be %s%n", theBook, new ConsLOE(new Entry("Bob", "111-1111"), new EmptyLOE())); System.out.format("Answer is %s, should be %s%n", theBook.lookup("Bob"), "111-1111"); addEntry_mutant("Joe", "222-2222"); // store: theBook = ("Joe", "222-2222") : ("Bob", "111-1111") : ! System.out.format("Answer is %s, should be %s%n", theBook.lookup("Joe"), "222-2222"); // What if the above addEntry_mutan
> t lines were in another file? A file // in another directory? What if another file REMOVED lines from theBook? // We'd have to track the store through those files, too! addEntry_mutant("Tim", "333-3333"); // store: theBook = ("Tim", "333-3333") : ("Joe", "222-2222") : ("Bob", "111-1111") : ! System.out.format("Answer is %s, should be %s%n", theBook.lookup("Tim"), "333-3333"); System.out.format("Answer is %s, should be %s%n", theBook.lookup("Bob"), "111-1111"); // This is dangerous, EXTERNAL mutation: users of addEntry_mutant can't // reason just by substitution - they also have to track any part of the // store that addEntry_mutant affects (in this case, theBook) // We also have to reason about the whole program instead of just the // parts that we can see } // ------------------------------------------------------------------------- public static void main(String[] args) { nonMutationTests(); mutationTests(); } }
>
> isprime.java
> 1KViewDownload
The code also demonstrates how to track the store when the for loop
can't be unrolled. Yes, it changes the loop into a while loop first.
Neil