CS 112 - Introduction to Computer Science II
Homework #6

Due: At the beginning of class 18.

NOTE: This work is to be done in pairs.  I strongly recommend the practice of Pair Programming described simply here.  Although team members are permitted to divide work, each team member should be able to informally present all work of his/her teammate.

The first two exercises will be based on the Java generics MyLinkedList code we will develop in class.  The test code we create for MyLinkedList can be easily adapted to test your exercise work.

NOTE: Do not use class MyLinkedListNode directly when implementing the classes of the assignment.  Only your MyLinkedList class will refer to MyLinkedListNode.  Do not use anything beyond the public methods of MyLinkedList that we defined together.

1. MyStack: Implement MyStack.java according to this specification.

2. MyQueue: Implement MyQueue.java according to this specification.

3. MahFravrit: Implement MahFravrit.java according to this specification.  This class essentially allows a generic set of items to be collected and returns a favorite ("fravrit") item from the set much like a priority queue, but with a difference: You'll implement this without relying on an efficient priority queue implementation.  Your efficiency requirement is to ensure that a favorite item is found in time linearly proportional to the number of items. 

Notes:

An explanation of the meme this assignment draws its strange vocabulary from may be found by clicking on the image below or watching this erudite video explanation.

Ermahgerd! Here is some test code to illustrate the use of MahFravrit class!

	public static void main(String[] args) {
		// In this first example, we use random integers and prefer larger integers as fravrits (favorites).
		int size = 20;
		MahFravrit<Integer> intFravrit = new MahFravrit<Integer>(new Comparator<Integer>() 
		{
			public int compare(Integer v1, Integer v2) {
				return v1 - v2;
			}
		});
		// Add random integers, possibly with repetition.
		for (int i = 0; i < size; i++) {
			int item = (int) (size * Math.random());
			System.out.print(item + " ");
			intFravrit.add(item);
		}
		System.out.println();
		// Successively, remove integers, greatest to least as we've defined our preference with the Comparator above.
		for (int i = 0; i < size; i++) 
			System.out.print(intFravrit.getFravrit() + " ");
		System.out.println();

		// In this next example, we use Strings with book names and prefer later Strings lexicographically, 
		// except for "Gersberms", which are MOST preferred because "ERMAHGERD! GERSBERMS ... MAH FRAVRIT BERKS!" 
		// http://knowyourmeme.com/memes/ermahgerd
		MahFravrit<String> berkFravrit = new MahFravrit<String>(new Comparator<String>() 
		{
			public int compare(String v1, String v2) {
				if (v1.equals("Gersberms"))
					return 1;
				else if (v2.equals("Gersberms"))
					return -1;
				else
					return v1.compareTo(v2);
			}
		});
		berkFravrit.add("The Hobbit");
		berkFravrit.add("Gersberms");
		berkFravrit.add("Diary of a Wimpy Kid");
		System.out.println(berkFravrit.getFravrit());
		System.out.println(berkFravrit.getFravrit());
		System.out.println(berkFravrit.getFravrit());
	}

Sample output:

12 11 1 4 18 11 13 0 2 2 6 2 1 11 3 8 1 16 8 15 
18 16 15 13 12 11 11 11 8 8 6 4 3 2 2 2 1 1 1 0 
Gersberms
The Hobbit
Diary of a Wimpy Kid

Your random integers will be different but ordered high to low in the second line.

Rubric: (20 points total)