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.
a default constructor
a constructor that takes in a single Card object
a copy constructor that takes in a single parameter of type Hand. This method shall create a deep copy, meaning you don't want the references to Card objects in the new Hand object to be simply be copies of the references to the Card objects of the original Hand. You must create new Card objects for the new Hand object. (Java normally uses the clone method and Cloneable interface to support the ability to create a copy of an object. You will create this simple copy constructor to avoid having to deal with some of the complexities of implementing the clone method.)
a method that returns the number of Cards in the Hand
a toString method
an equals method to compare two Hands and return true if they have the same number of Cards and the Cards have a one for one match in the other Hand with suit and value.
a method to add a Card to the Hand. A single Card parameter is sent and the Card is added to the Hand. You do not need to create a copy of this Card, but can simply use the same reference.
a method to remove a Card from the Hand. The Card to be removed should be specified with an integer parameter.
a method to retrieve a Card from the Hand. This method would need some parameter to specify which Card to remove, most likely an integer.
a method that combines two Hands and returns a new, third Hand. This method shall have a single parameter, a Hand object. It will combine the calling object with the object sent as a parameter in a new Hand object. Note, this new Hand shall contain deep copies of the two old Hands.
a method that returns if a Card is in the Hand. The method shall accept a single Card objects as a parameter and return a boolean if a Card equal to the parameter is in the Hand.
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