Today's code and screen capture

0 views
Skip to first unread message

Blake Johnson

unread,
Jun 8, 2011, 2:32:34 PM6/8/11
to byu-cs-jm-14...@googlegroups.com
Code is attached to this email, and the screen recording is at http://students.cs.byu.edu/~bjohn3x/Day18.m4v

Blake
Scratch.java
Scratch2.java
scratch3.java

Jay McCarthy

unread,
Jun 8, 2011, 3:07:18 PM6/8/11
to byu-cs-jm-14...@googlegroups.com, Neil Toronto
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> wrote:

Code is attached to this email, and the screen recording is at http://students.cs.byu.edu/~bjohn3x/Day18.m4v

Blake
<Scratch.java>
<Scratch2.java>
<scratch3.java>

Neil Toronto

unread,
Jun 8, 2011, 3:50:03 PM6/8/11
to byu-cs-jm-14...@googlegroups.com
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 <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>

scratch4.java

Jay McCarthy

unread,
Jun 8, 2011, 3:52:30 PM6/8/11
to byu-cs-jm-14...@googlegroups.com, byu-cs-jm-14...@googlegroups.com
Awesome

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(); } }

Neil Toronto

unread,
Jun 8, 2011, 6:25:16 PM6/8/11
to byu-cs-jm-14...@googlegroups.com
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

isprime.java

Ryan Hess

unread,
Jun 8, 2011, 7:22:19 PM6/8/11
to BYU CS 142 (Spring 2011) [McCarthy]
What do you mean by "Make sure to remove all the matching entries"?
Can people in theBook have the same name? If so, how do you
differentiate between them in lookup()?

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

Diego (TA)

unread,
Jun 8, 2011, 7:35:08 PM6/8/11
to byu-cs-jm-14...@googlegroups.com
Yeah, that means that if the list has multiple entries, with the same name, you will have to remove all of the entries of that name.

Blake Johnson

unread,
Jun 13, 2011, 2:49:20 PM6/13/11
to byu-cs-jm-14...@googlegroups.com
Code attached

screen capture:

http://students.cs.byu.edu/~bjohn3x/Day20.m4v


Blake
Scratch.java
Scratch2.java
scratch3.java

Neil Toronto

unread,
Jun 13, 2011, 3:52:10 PM6/13/11
to byu-cs-jm-14...@googlegroups.com
I've attached more code, demonstrating how to unroll simple for loops
like the ones in your assignment (exercises 1 and 2). You'll need to
unroll those loops to track the store. You *could* turn them into while
loops first, but there's an easier way in this case.

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

scratch4.java
Reply all
Reply to author
Forward
0 new messages