Lab 1: Woolie War - Linear Data Structures

Copyright RIT 2008
$Id: writeup.xml,v 1.17 2010/06/10 23:51:57 vcss233 Exp $


Goal


Saving Private Woolie

The program you will be completing today is a battle simulation using those adorable Woolies we have all grown to love. The main class is WoolieWar and it is responsible for conducting the entire war. Nested inside this class are two types of threads - WoolieWar.Recruiter and WoolieWar.Warmonger There are multiple recruiters that are responsible for enlisting Woolies into the war by placing them into the BattleFront. The BattleFront is a priority queue (created by WoolieWar) which keeps the Woolies sorted based on their military rank and id (from lowest to highest). There is one warmonger who is responsible for fighting the war, and therefore takes Woolies off the BattleFront (from the front), and places them into the war (which sadly translates into an untimely death for each unfortunate Woolie). Once the death toll passes an acceptable limit, the WoolieWar class declares a truce and all the the threads naturally terminate - followed by a report of the final war statistics.

Pre - Lab Work

  1. Review your class notes on linked data structures, especially queues and linked list insertion.

  2. If you are unfamiliar with Java's enumerated data type, read the tutorial at http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html. Also review the javadoc for the Enum class.

  3. This lab requires you write a generic class. If you need to review this topic check out the tutorial at http://java.sun.com/docs/books/tutorial/java/generics/index.html.

  4. Download and unpack the jar file that contains the code you will need to complete this lab.

  5. Study the supplied classes and become familiar with the class documentation, ./docs, and the following class diagram.


    WoolieWar Class Diagram

In-Lab Activities

Activity #1 : Register for this lab

Using your schedule, register with the try database.

How To Submit

To register with try, open a terminal window and from a Unix prompt type the following command:


                try grd-233 register /dev/null
                

If you make a mistake entering either your lecture or lab section, notify your lab instructor right away. They can correct the database before you submit any future work.

Activity #2 : Implement the Woolie class

For this activity, you must write the Woolie class from scratch and test it. The javadoc contains all the information you need to write the class. Make sure to review the Java coding standard, http://www.cs.rit.edu/~fyj/java-coding-standard.html, and refamiliarize yourself with RCS, http://www.cs.rit.edu/~vcss232/Labs/lab01/Auxiliary/rcs.html

Each Woolie has a unique id (starting at 1), and a military ranking. The military ranking is represented by the enumerated type Woolie.Ranking. The ranks from lowest to highest are: PRIVATE, CORPORAL, SERGEANT, LIEUTENANT, CAPTAIN, MAJOR, COLONEL, GENERAL. Internally, each rank has an integral value (starting at 0) which indicates its position in the declaration. Using the Enum's compareTo method you can compare the ranks to determine a Woolie's natural ordering.

The javadocs can be confusing regarding what you need to write to make the Ranking Enum. It should just be defined inside the Woolie class as:


public enum Ranking {
	PRIVATE, CORPORAL, SERGEANT, LIEUTENANT, CAPTAIN, MAJOR,
	COLONEL, GENERAL
};
	    

You should make a test program for your Woolie class that creates various Woolies of differing ranks, and makes sure they display and compare correctly (based on the javadocs). Please write your test code in the main method of the Woolie class. Part of your grade will be based on how well your code tests the class.

How To Submit

When you have completed the Woolie class, tested it, and are sure that your code works properly, submit your work by typing the following command:


try grd-233 lab1-1 Woolie.java
	        

Activity #3 : Implement the BattleFront class

For this activity, you must write the BattleFront class from scratch and test it. Pay careful attention to the javadoc for this class and especially its declaration:


public class BattleFront<T extends Comparable<T>> implements IQueue<T> { ... }
	    

Here T is the generic type the collection holds. It implementes the generic IQueue interface. Also, because the enqueue method places each element in the queue based on its natural ordering, it is necessary that we tell the compiler that class T must implement the generic Comparable interface.

Internally the collection must be represented as a series of generic Node structures. Remember that you always dequeue from the front of the queue, and you enqueue by traversing through the queue and finding the location where the element follows the rules of natural ordering (i.e. all predecessors must compare less than (or equal) to this element, and all successors must compare greater than (or equal) to this element.

Because multiple threads might access this collection at the same time, it is important that you synchronize the enqueue and dequeue methods. Otherwise it is possible the collection could get corrupted. Note that it is not necessary to synchronize the other methods because they do not modify the collection and the information a thread gets back is correct for when it was invoked.

Remember that your output will be different each time you run the WoolieWar program (including the number of Woolies that get created) because of the threading and randomization. Take a look at this ./Auxiliary/output.txt sample run. You can tell it is working correctly because of the following:

  • The last Woolie made in this run of the program is 55, i.e.: RECRUITER 0: enlisting COLONEL Woolie(55)
  • If you sum up each recruiter retire message they total: 17 + 17 + 21 = 55.
  • If you sum up the Woolies who die and survive they total: 50 + 5 = 55.
  • If you look at all the output you should be able to determine that the counts for each of the deceased is correct, and that the survivors are also correct.
  • How To Submit

    When you have completed the BattleFront class, tested it, and are sure that your code works properly, submit your work by typing the following command:

    
    try grd-233 lab1-2 BattleFront.java
    	        

    The try program will run your program and save its output each time you submit. Because of this you will see a significant delay (usually around a minute) between when you initiate the try command and when it completes.


    Grade Computation

    Grade Breakdown: