6.170 Laboratory in Software Engineering
Spring 2002
Problem Set 2: Implementing an Abstract Data Type
Due: Tuesday, February 19, 2002 at 4pm

Handout P2

Quick links:

Contents:

We recommend that you read the entire problem set before you begin work.


Introduction

In this problem set, you will practice reading and interpreting specifications, as well as reading and writing Java source code.  You will implement a pair of classes that will complete the implementation of a poker game, and answer questions about both the code you are given and the code you have written.

To complete this problem set, you will need to know

  1. How to read procedural specifications (requires, modifies, effects) and representation invariants
  2. How to read and write basic Java code
  3. How to run a Java compiler such as javac to create .class files
  4. How to run the Java Virtual Machine (JVM) such as java to execute both your code and staff-provided libraries


Problems

Problem 1: CardValue and CardSuit (20 points)

Read the specifications for CardSuit and CardValue, classes for playing card suits and numbers. Then read over the staff-provided implementations in CardSuit.java and CardValue.java. You may find it helpful to peruse the code in CardSuitTest.java and CardValueTest.java to see example usages of the classes (albeit in the context of a test driver, rather than application code).

Answer the following questions, writing your answers in the file problem1.txt.

  1. Why are CLUBS, DIAMONDS, HEARTS, and SPADES in CardSuit static?

  2. Why have a separate class for card suits, instead of just having them be integer constants in some other class? I.e., why not write:
    public static final int CLUBS = 1;
    public static final int DIAMONDS = 2;
    public static final int HEARTS = 3;
    public static final int SPADES = 4;

  3. Notice that CardSuit constructor checks to see if the parameter suitName is null before using it. Why isn't this also checked against null before it is used? (this is dereferenced implicitly when accessing, say, smallSuitIcon.)

  4. What is the point of having the CardValue and CardSuit constructors be private?

  5. Why doesn't CardValue need to override Object.equals?

  6. What are the advantages of having CardValue implement the Comparable interface, as opposed to just creating a method with a different name and signature like boolean isLessThan(CardValue v)? (It might be helpful to follow the link to the Java API docs for Comparable.)

Problem 2: Card (15 points)

Read over the specifications for the Card class. Make sure that you understand the overview and specifications.

Read through the provided skeletal implementation of Card in Card.java. The most significant parts of the provided file are the comments describing how you are to use the provided fields to implement this class.

Fill in an implementation for the methods in the specification of Card. You may define new private helper methods if you want to, but you probably won't need to.

Also, we have provided a fairly rigorous test suite in CardTest.java. You can run the given test suite with JUnit as you program and evaluate your progress and the correctness of your code.

To run the Card test suite, type the following command:

athena% java junit.swingui.TestRunner ps2.playingcards.CardTest
For a non-graphical alternative method of running JUnit, use junit.textui.TestRunner instead of junit.swingui.TestRunner, as stated in the Hints.

Indicate clearly as a comment in your source code whether or not your code passes all the tests. We will assume that your code fails if you say nothing.

Problem 3: Deck (20 points)

Follow the same procedure given in Problem 2, but this time fill in the blanks for Deck. You can find the skeleton implementation in Deck.java. The same rules apply here (you may add private helper methods as you like).

We have provided a test suite in DeckTest.java. You can run the given test suite with JUnit as you program and evaluate your progress and the correctness of your code.

Indicate clearly as a comment in your source code whether or not your code passes all the tests. We will assume that your code fails if you say nothing.

Problem 4: TwoPair and Straight (30 points)

Now that you have completed the implementation of cards and decks, there are just a couple of pieces left to finish the programming of the PokerGame application. The class PokerRanking is used to compare hands to one another. You must fill in the methods evaluateHand and compareSamePokerRankings in TwoPair.java and Straight.java, which are two sub-classes of PokerRanking. You will need to look at the documentation for the Hand class. You will also probably find it helpful to look at the documentation for the PokerRanking class, since it contains helper methods like findNOfAKinds, removeNCardsWithValue, and isValidHand that could be useful.

We have provided test suites in TwoPairTest.java and StraightTest.java (you may also need to refer to the test suites' superclass GeneralRankingTest.java).

Indicate clearly as a comment in your source code whether or not your code passes all the tests. We will assume that your code fails if you say nothing.

Problem 5: Module Dependency Diagram (10 points)

Draw a module dependency diagram showing the classes in the ps2.playingcards package, with their depends and meets relationships. You need not include the classes of the poker application that we have not revealed to you, but you should make a reasonable judgment about which Java library classes and interfaces to include.

You (and your TA) may find it useful if you use Visio to generate the MDD's. Refer to the Tools Handout for more information about Visio.

Problem 6: Playing the Game (5 points)

Now that you have implemented all parts of the poker application, you can run the game by typing the following command:
athena% java ps2.PokerGame
At the beginning of the game, you have a bankroll of $1000. Play against the computer until you have a bankroll of at least $1500. Provide a screen shot of the application at this point. You can create screen shots on Athena by using the xv Grab command:
athena% add graphics
athena% man xv
Or, if on a Windows machine, you can use Alt-PrtScn.

End of Problem Set 2. Please see Problem Set Submission instructions.


Provided classes

The following classes are all provided for you, in compiled form, by the staff:

With source code also provided:
ps2.playingcards.CardValue
ps2.playingcards.CardValueTest
ps2.playingcards.CardSuit
ps2.playingcards.CardSuitTest
ps2.playingcards.CardTest
ps2.playingcards.DeckTest
ps2.hand.TwoPairTest
ps2.hand.StraightTest
ps2.hand.GeneralRankingTest

With source code not provided:
other files in the ps2 package for the poker game

Getting started

Make sure you have followed the instructions on the tools handout relevant to directory setup and using Java before you begin development.

If you are doing the problem set on Athena, then you can easily get all the source files by running the command get-ps2-files.

If you are not doing the problem set on Athena, then you will have to get the files yourself from /mit/6.170/www/psets/ps2/src. Some of the provided code is in the package ps2.playingcards, and some is in ps2.hand. Java requires that the directory structure follows the package structure. So, when you copy the files, you will need to put them in the following directory structure on your machine:


    ps2/
      playingcards/
        CardSuit.java
        CardSuitTest.java
        CardValue.java
        CardValueTest.java
        Card.java
        CardTest.java
        Deck.java
        DeckTest.java
      hand/
        TwoPair.java
        TwoPairTest.java
        Straight.java
        StraightTest.java
	GeneralRankingTest.java
Fill in the skeleton source files, and test your implementation. You do not need to compile the other sourcecode files that we have provided; compiled classfiles are already ready for your use in the 6.170 locker.

See the specifications for the classes you will implement and those that are provided for you.

By the end of the problem set, you should have the following files in the ~/6.170/ps2/ directory on Athena, ready to submit:

Hints

See the problem set guidelines and advice. Think before you code!

JUnit reloads all of your classes each time you run a test, so you don't need to restart the JUnit application after making changes to your Java code (though you do need to recompile your code for the changes to take effect).

For a non-graphical alternative method of running JUnit, use junit.textui.TestRunner instead of junit.swingui.TestRunner .

The provided test suites in problem set 2 are the same ones we will be using to grade your implementation; in later problem sets the staff will not provide such a thorough set of test cases to run on your implementations, but for this problem set you can consider the provided set of tests to be rigorous enough that you do not need to write your own tests.

Errata

All announcements (MOTDs) related to PS2 are periodically added here.

Saturday, February 16, 2002

The StraightFlush.class file was recently added to "ps2-lib.jar", the absence of which was causing various errors being reported by students. If you are on Athena, you should be all set. If you manually copied "ps2-lib.jar" to your local machine or directory, please get the updated jar file.


Friday, February 15, 2002

As pointed out by one of your classmates, CardSuitTest did not work because the CardSuit.compareTo() was not throwing a NullPointerException when it received a null input. The error has been fixed. Download the new CardSuit.java file. If you are on Athena, simply copy the file "/mit/6.170/www/psets/ps2/src/CardSuit.java".


Thursday, February 14, 2002

Important Update on PS2:

There were a number of small issues with Problem Set 2 that have now been cleared up:

If you have already started the problem set, we recommend you back up your work and run the script "get-ps2-files" again. You can then copy any code you have already implemented over to the new, fully-specified files (don't worry, none of your code should go to waste!)

To avoid reading an older, cached version of the Problem Set, make sure you Refresh/Reload the relevant pages in your web browser.


Q & A

This section will list clarifications and answers to common questions about problem sets. We'll try to keep it as up-to-date as possible, so this should be the first place to look (after carefully rereading the problem set handout and the specifications) when you have a problem.

Q: I experienced a problem with Visio 2000 popping up an error dialog on starup that says, "The procedure entry point GBL could not be located in the dynamic link library VISLIB32.DLL."? I am installing Visio 2000 on a Windows 2000 machine.

A: Refer to Microsoft Support: Visio2000: Error Message: The Procedure Entry Point GBL Could Not Be Located in the Dynamic Link Library Vislib32.dll.


Q: Is it possible to do Problem Set 2 on my home (not Athena) machine?

A: Yes, but you must download the appropriate jars from the 6.170 course locker to your local machine. For Problem Set 2, you will need the junit.jar and ps2-lib.jar from the /mit/6.170/lib directory. However, your code MUST work on Athena, so please take a few minutes before submission of your exercises to ensure that it runs correctly on Athena.
[Download ps2-lib.jar]
[Download junit.jar]


Q: Where can I find the JUnit API?

A: The JUnit API can be found here.


Back to the Problem Sets.
For problems or questions regarding this page, contact: 6.170-webmaster@mit.edu.

$Id: ps2.html,v 1.19 2002/02/16 07:48:17 kalpak Exp $