CS 307 - Fall 2001 - Assignment 2, Creating Classes on your own
Complex Numbers and Cards

Handed Out:    September 17, 2001    
Due:
   Before 11:59 p.m. September 24, 2001

Purpose:
To give you practice completing entire classes
Provided classes: Card.java, StandardDeck.java

Part 1 - Complex Numbers

Complete problem 3.17 on page 88 of the book.  The problem states "Implement a Complex number class. Recall that a complex number consists of a real part and an imaginary part. Support the same operations as the Rational class (described in problem 3.15 on page 87) when meaningful (for instance compareTo is not meaningful). Add accessor methods to extract the real and imaginary parts." The methods you need to implement then are a reasonable set of constructors, add, subtract, multiply, divide, toString, equals, and the accessors previously mentioned. Name your class ComplexNumber

You can refresh your memory on complex numbers at any of a number of websites such as

http://www.ping.be/math/complget.htm or http://www.clarku.edu/~djoyce/complex/ or http://www-ccrma.stanford.edu/~jos/r320/Complex_Numbers.html

You will test your class, but do not need to turn in your test harness or testing code.

Part 2 - Cards

Introduction: Card games are a wonderful example of the power of Object Oriented Programming. We won't be completing any games with this assignment, but rather building the classes we could use over and over for many different card games. The provided classes are used to model a Card and a StandardDeck of cards.  A standard deck of cards contains  52 cards with 13 cards of each suit and the values going from 2 to Ace. You will create a Hand class that represents a collection of Cards. Look at the Card class and StandardDeck classes to see what methods are available with these classes.

Instructions: Complete a Hand class. A hand represents a collection of zero or more cards. Cards may be added and removed from the Hand.  You must provide the following minimum methods for the Hand class.  All of these methods shall be overloaded.

Implementation: I leave most of the details of the implementation to you. You decide what instance variables and private methods you need to implement the above methods. The one requirement is you must use a native array of Card objects as an instance variable  to store the objects that currently make up the Hand. This is so you get practice with using the native arrays of Java. You also may not make any changes to the Card or StandardDeck classes.

Comments and Coding Standards: One of the keys of a good implementation is good documentation and style so that a class may be easily understood and changed later.  You ability to follow the coding standards will be part of the grade on this assignment. As for comments you must include a comment at the beginning of each method with an explanation of what the method accomplishes, and the pre and post conditions of the method. A precondition is something the caller of the method must ensure is true prior to calling the method.  For example the combine method of the Hand class has one parameter of type hand.  A precondition would be that this parameter is not null:

/* combines the calling object and parameter into a new Hand which is a deep copy.
    returns a reference to this new Hand.
    pre: otherHand != null
    post: returns a new Hand object with all of the cards from the 
    original Hands (copies of the same card possible.) The new hand 
    could have 2 or more Ace of Spades.
*/
public Hand combine(Hand otherHand)

Postconditions are things you guarantee to be true after the method is complete if the preconditions were met. You can assume the preconditions are met and do not need to do "defensive programming" if you have clearly explained your expectations via the preconditions.

Testing: I will leave the testing of your Hand class to you. You may either write a test Harness or if you are using BlueJ make use of the unit test feature.

When you are done turn in your Hand. java and ComplexNumber.java files via email to me, scottm@cs.utexas.edu