15-100 Lecture 22 (Monday March 22, 2004)

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 many of the things we have written before (textbook, wine, etc.). 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 a specified instance variable's value.

You will also have to do standard mutator methods (eg. setPrice()), which take in a parameter and changes an instane variable, returning 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 (info on sheet is wrong). You will need to through and print all objects that meet a certain criteria, which would probably just be through brute force search.

Part 3 is insertion, part 4 is a remove. There are 4 major policies:

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.