Today's lab will be about:
To start with we have provided a lot of code; this week the code is in several packages.
Let's look at the htdp.DrScheme class. We use it to simulate a homework assignment a student might be given in their first programming course. We want to process these homeworks and eventually grade them and check if they are cheating.
The first thing DrScheme provides is:
public static ISet keywords()
We'll be seeing a lot of static methods in this lab. These
methods belong to a class rather than to a specific object. To call this
one, you would write
What does this produce? According to its purpose statement, all the keywords in the Beginning Student language level. These are the words we can pay the most or the least attention to on a student's homework, depending on how we want to evaluate it.
But what the heck is an ISet? You should look at this
interface in package
types.structures. Between the purpose
statements, method headers, and any other interfaces that are mentioned,
you should be able to piece together how it works.
Ask a TA if you're unclear of what part of the interface means. Take your time.
Let's look at the second thing in DrScheme:
public static IList tokenList( String program )
This turns a program text into a list of tokens (our Str class). It
produces an IList, another interface
types.structures. This one should be more familiar, but
it doesn't have any
voids in it
java.util.List. Instead, this interface works more like a
Scheme list or an IRange.
Now that you've seen ISet, IList, and DrScheme, let's write a program.
Now we're going to strike out on our own. We want to represent homework
submissions now. Each homework submission has to have a student's name
(or possibly a list of teammates?) and a program. For this we'll want a
Next we get to using IMaps. We want to have a link between each submitter and their program. The class DrScheme needs a new method:
// Given a list of Homeworks, produces an IMap
// from submission names to ILists of tokens.
public static IMap handins( IList homeworks )
What does this mean? Start by reading the interface
for IMap. To produce your result, use the
SimpleMap in package
new SimpleMap() creates a new, empty map.)
Moving right along. Let's write that IMap code.
handinsto DrScheme. You may want to look up
algorithms.ListAlgorithms.foldLeftfor this, but if another way seems more natural to you go for it.
handinson the resulting IList and print out the result (all of our classes have toString() implemented).
You may have noticed a couple things by now. (Or not.) One of the things we have in mind is that SimpleMaps print out their keys and values in very bizarre orders. If you build big enough maps, the keys and values change order as you add elements! (Guilty admission: it didn't have to be that way with these SimpleMaps. But it is that way with Java HashMaps, and we wanted you to see the phenomenon. So we faked it.) You may have also seen a HashCollisionException raised at some point if you picked unlucky test data. This also could have been avoided if we got really clever with our coding - but the first problem is real. Let's fix it!
The problem is that SimpleMap doesn't know anything about the order our
keys should go in, so it picks something arbitrary. We want to use
OrderedMap instead. But to construct OrderedMap, we need an ICompare
(remember that interface? look it up in
to sort our keys (in this case, names on homeworks).
Let's fix our unordered mess of homeworks.
ICompareto compare your IMap keys.
We've got these homeworks, now what are we going to do with them? There are two interesting options: we can grade them to find the good ones, or we can catch cheaters to find the bad ones. There's a number of ways to go about each.
To grade the homeworks, we can give someone a grade on several metrics.
To catch cheaters we have a few options as well.
Using one of the above options, write either of:
// Convert a names->homeworks mapping to a names->grades (Int) mapping.
public static IMap grades( IMap homeworks )
// Tells whether two homeworks are too similar.
public static boolean cheating( Homework h1, Homework h2 )
As always, test your code in Examples and Interactions.
Because we couldn't let you leave just yet!
Oh, what the heck. While you're here, implement the other method from above!