6.170 Laboratory in Software Engineering
Spring 2004
Problem Set 1: Implementing an Abstract Data Type
Due: Thursday, February 12, 2004 at 9pm

Handout P1

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 four 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)
  2. How to read and write basic Java code
  3. How to compile Java code and how to run Java programs.

Getting started

Make sure you have followed the instructions on the tools handout relevant to directory setup before you begin development. Furthermore, be sure you have read the problem set procedure handout before you begin.

You can get the source files for problem set 1 by using CVS. Follow the problem set checkout instructions and checkout the ps1 module from your repository.

Your code MUST work on Athena, so if you choose to work on a non-Athena machine, please take a few minutes before submitting your solution to ensure that it runs correctly on Athena. Once everything is in order, read the Problem Set Submission instructions for how to run a final check of your code and turn it in.




Problems

Problem 1: CardValue and CardSuit (20 points)

Read the specifications for ps1.playingcards.CardSuit and ps1.playingcards.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 ~/6.170/workspace/ps1/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 nullbefore 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 don't CardValue or CardSuit need to override Object.equals?
  6. What are the advantages of having CardValue and CardSuit 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.)
  7. (for Problem 3,4) Look at the specifications for ps1.hand.Hand and ps1.playingcards.Deck. Both classes represent sets of playing cards. Why might the designer have chosen an unsorted collection to implement one of the classes (Deck), and choose a sorted collection to implement the other (Hand)?

If you are using Eclipse, refresh your ps1 project after creating problem1.txt (right-click on ps1 in the package explorer and select 'refresh'). problem1.txt should now appear in the package explorer along with your source files. If you are not using Eclipse, remember you will need to explicitly add problem1.txt to your CVS repository using the cvs command line.


Problem 2: Card (20 points)

Read over the specifications for the ps1.playingcards.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.

You'll notice that all of the methods that you need to implement currently have this method body:

throw new RuntimeException("Method is not yet implemented!");

Try compiling your code and running the tests (see below). You'll notice that each of the tests that calls one of these methods prints out the error message "Method is not yet implemented", and then shows a backtrace. The backtrace shows the filenames and line numbers of the statement that threw the exception, the statement that called the method that contained the statement that threw the exception, and so on up to the main method that kicked everything off. When you implement a method, you should be sure to remove the throw new RuntimeException statement.

Now fill in an implementation for each of 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.

We have also 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. For this problem set you do not need to write your own test cases.

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.

Running the Tests using Eclipse

JUnit is integrated with Eclipse, so you can run the test suite from within the IDE.

Running the Tests from the Command Line

If you are not using Eclipse, you can invoke JUnit from the command line. To run the Card test suite, type the following command:

athena% java junit.swingui.TestRunner ps1.playingcards.CardTest

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

Remember that in order for this to work, ps1-lib.jar, junit.jar and the directory containing your class files must all be in the Java VM's classpath (i.e. part of the CLASSPATH environment variable or you may need to explicity use the -classpath parameter for the Java VM).


Problem 3: Deck (25 points)

Follow the same procedure given in Problem 2, but this time fill in the blanks for ps1.playingcards.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 again provided a test suite, in DeckTest.java. You can run the given test suite with JUnit as you program, to 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: Hand (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 ps1.hand.Hand is used represent the cards that you currently have. You must fill in all unimplemented methods in Hand.java.

We have provided test cases in HandTest.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: Playing the Game (5 points)

Now that you have implemented all parts of the poker application, you are ready to play the game.

Using Eclipse

Follow the instructions below: