15-100 Lecture 29 (Monday April 4, 2005)

Today in class we're going to do a general overview of the mastery exam and then start working on a class that should help review some of the general things that you're going to have to do on the mastery exam.

Mastery Exam

Note the main document this is based on can be found athttp://www.intro.cs.cmu.edu along with a sample exam and more information.

The Mastery Exam

Today we went over information for the mastery exam.

First, you must implement a class. It is just like some of the things that we have written before (problem 1 on exam 2).

It should have proper instance variables. int, char, boolean, and double are all primitives, so you can use >, =, and < on them. Strings are objects, however, so you need .compareTo() or .equals() to compare them.

Your constructors should initialize all variables, you will probably have to set some to a default value (eg. count=0) and assign some values as parameters (eg. this.maxSize = maxSize)

You will have to do standard accessor methods (eg. getDefinition()), which take in no parameters and return an instance variable's value.

You will also have to do standard mutator methods (eg. setPrice()), which take in a parameter and change an instance variable, and return nothing.

You will also have to write a toString() method for your class, which should generally print the string representation of all of the instance variables. Make sure that your code looks exactly like the sample output, so if the exam has a sample that says "Bob weighs 180 pounds and is 19 years old" do this exactly and not a toString() that says "name: Bob, weight: 180, age: 19"

Part 2 should be a simple filter method. You will need to go through and print all objects that meet a certain criteria. The easiest way to do this is almost always a simple brute force search.

Finally, Part 3 is insertion, and part 4 is a remove. There are 7 major categories which these insertions and removes fall into:

1.) Set with "up by one" promotion

Insertion: If the item is already in the array, swap it with the one in front of it using a standard swap technique. If it is already at the front do nothing. If it's not in the array, put it at the end.

Remove: Remove the item from the array, shifting evrything to the left to fill the hole.

2.) Set with "move to front" operations

Insertion: Try to find the thing you're inserting. If it isn't there, put it at the end of the list. If it is there, store that value in a temporary variable, create a hole where the old copy was, shift everything in front of the hole backward, and insert the new item into the front.

Remove: Remove the value from the array, filling in the hole. The order at the end should be the same.

3.) Multiset with ordered values

Insertion: This is an insert in order. One key thing to notice is that if there are multiple things with the same value, the newly inserted item should be IN FRONT OF all of previous inserted (ie to the left of it in the array), so if you start at the end, you will keep shifting while the element before you is >= the value to insert.

Remove: This removes only the first occurrence of the value. Remove it, fill in the hole.

4.) Multiset with counted values

Insertion: This question will only be done with the class we wrote. If we try to add something and it isn't there, we add it to the end. If we find it, we increment the count of that variable. .

Remove: Decrease count by one. If count becomes 0, remove it from the array, and shift everything down.

5.) Priority Queue with searching

Insertion: Insert it to the beginning of the array, so shifts all the elements in the array to the right and inserts the new element at front.

Remove: Remove the item with the highest priority. If there are multiple items that have the same highest priority, remove the one closest to the end of the array (ie furthest to the right.) So if you start at the end, you will only change the index of maxPriority if the element to the left is > the previous max, and if you start at the left, you will change it if the element is <= the previous max.

6.) Priority Queue with sorting

Insertion: This is an insertInOrder, with those with highest priority going in the FRONT of the array. If two vales have the same priority, the new values should be inserted in front (to the right? some confusion).

Remove: Remove the item with the highest priority (the first item in the array). Fill in the hole.

7.) Sequence

Insertion: insertAt will store the value at the specified index of the array, moving all items at and beyond the index back. If you are given an invalid index, print out the specified error message (System.out.println, no exception handling needed).

Remove: Remove the item at the index. If you are given an invalid index, print out the specified error message (System.out.println, no exception handling needed).

Suggestions Get together in STUDY GROUPS! Get started studying early! Come see TAs and Kesden, ask us questions. We will help you.

Writing the Container Class
We've already given you a brief overview of the mastery exam, now we're going to go over actually writing problem 1.

If you read over problem one on the exam you'll see that it can be fairly confusing. This is not because the problem itself is difficult, merely that the explainaition is confusing. Because of this you have to work on it a little differently then you would normally work on one of the other contianer classes, like Textbook or Classroom.

In this case the only that we need to worry about is whether our container class returns thier sample output when used with thier tester, so we're going to be kind of working backwards from the tester. Inorder to do this, quickly read through the problem description first, just to get an idea of the things that are on it.

We're going to begin by writing the constructors first. So go look at the tester and see what constructors it has on it. It will probably have a couple, which take varying sets of parameters. Put these constructors in your class, but don't fill them in yet, because we're going to have to look at the problem description to be able to do that.

Constructors for the mastery exam almost always have an if statement where setting one of the parameters is based on the value of another of the parameters. Look at the problem description and fill in the mastery exam constructors for what you think it might want you to do. Now refer to the provided output from the tester and check if your assumptions are correct. Just keep going back and forth until your constructors are working exactly like thier constructors.

After you do the constructors go and do exactly the same thing for the rest of the accessor and mutator methods. Just be careful because at least one of the mutators will probably have an if statement or something in it. Also make sure that you do a timed practice of one of the Problem 1's. It feels a lot different having to work backwards from output, then being able to write it according to a problem description.