Markov Chain Programming Assignment Code Examples

Markov documents:   index   overview    markov    justification   turnin-instructions    code examples   partners     importing classes     MultiSet Documentation

FixedLengthQueue

Usage:

FixedLengthQueue prefixQueue = new FixedLengthQueue(4);
System.out.println("Queue with 0 elements: "+prefixQueue);
prefixQueue.add("element-one");
System.out.println("Queue with 1 element: "+prefixQueue);
prefixQueue.add("element-two");
System.out.println("Queue with 2 elements: "+prefixQueue);
prefixQueue.add("element-three");
System.out.println("Queue with 3 elements: "+prefixQueue);
prefixQueue.add("element-four");
System.out.println("Queue with 4 elements: "+prefixQueue);
prefixQueue.add("element-five");
System.out.println("Queue with 4 elements: "+prefixQueue);
prefixQueue.add("element-six");
System.out.println("Queue with 4 elements: "+prefixQueue);

The output of the above would be:

Queue with 0 elements:
Queue with 1 element: element-one
Queue with 2 elements: element-one element-two
Queue with 3 elements: element-one element-two element-three
Queue with 4 elements: element-one element-two element-three element-four
Queue with 4 elements: element-two element-three element-four element-five
Queue with 4 elements: element-three element-four element-five element-six

MultiSet

Note that this data structure does not guarantee any specified ordering of values. What it does guarantee is that if findKth(i) is called for each i in the range  0 <= i < size(), and an object obj has multiplicity m in this MultiSet, m of those calls to <code>findKth</code> will return obj.

public class TestMultiSet
{
  public static void main(String[] args)
  {
    MultiSet t = new MultiSet();
    t.add("Washington G.");
    t.add("Jefferson T.");
    t.add("Lincoln A.");
    t.add("Roosevelt T.");
    t.add("Reagan R.");
    System.out.println("Items in order:");
    for (int i = 0; i < t.size(); i++) {
    	System.out.println(t.findKth(i));
    }
    System.out.println();
    t.add("Washington G.");
    t.add("Jefferson T.");
    t.add("Lincoln A.");
    t.add("Roosevelt T.");
    t.add("Reagan R.");
    System.out.println("Items in order:");
    for (int i = 0; i < t.size(); i++) {
    	System.out.println(t.findKth(i));
    }
    System.out.println();
    for (int i = 0; i < 50; i++) {
    	t.add("Lincoln A.");
    }
    //adding an item more than once at a time probably won't
    //be useful for the Markov Chain project, but a data structure
    //like MultiSet should probably include a method for doing so
    t.add("Lincoln A.",50);
    System.out.println("Number of items stored:");
    System.out.println(t.numEntries());
    System.out.println();
    ArrayList a = t.values();
    System.out.println("Items actually stored:");
    for (int i = 0; i < a.size(); i++) {
    	System.out.println(a.get(i)+" has multiplicity "+t.getMultiplicity(a.get(i)));
    }
    System.out.println();
    System.out.println("Out of bounds items k=-1 and k=1001:");
    System.out.println(t.findKth(-1));
    System.out.println(t.findKth(1001));
  }
}

String.split()

As an alternative to the word-by-word Scanner, you may read in a line at a time and use the String.split() method to parse input.

String text = "This text\t  contains \n\t whitespace\r\r\r\r\r.";
String[] textSplit = text.split("\\s+");
for (int i = 0; i < textSplit.length; i++) {
	System.out.println(textSplit[i]);
}
produces the following output:
This
text
contains
whitespace
.
Recall that \\ parses as a single backslash since backslash is Java's escape character; the argument given to split is a regular expression that means match (as a delimiter) one or more ("+") whitespace characters ("\s").