Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Fall 2014
Thursday Lab #9: class definitions
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes

Due by 2:59 pm on Friday, November 14

# Bonus Lab Assignment

We are just beginning our study of class definitions, you have a project due Saturday and an exam on Monday. Hence, this lab assignment is optional. If you choose to complete it (by tomorrow's deadline), it can be used to replace your lowest lab assignment score for the semester.

# Object-Oriented Program Design

Previous work in this class—Lab #6 (link) and Project #3 (link)—involved a representation of playing card data with the integers 1 ... 52. For one assignment, you wrote a program, printDeck, which read a sequence of integers (each between 1 and 52) and printed out a list of the corresponding names of the playing cards, in the order their numbers appear. In the richer version, cardShuffle, the program reads a list of card deck permutations ("shuffles") along with a list of applications of those permutations, applies them to a sorted deck of cards, and prints the result.

Here is a solution to the printDeck problem: http://pastie.org/private/9og4vs6hn9z7hivg5ffzrw

And here is my version of cardShuffle: http://pastie.org/private/cxdfkqrtshgvsvd5w8htyq

In both programs, we adopt a fairly simple representation of a playing card, as an integer between 1 (the 2 of Clubs) and 52 (Ace of Spades). With this simple representation, we are then able to perform various "playing card-related" tasks. In particular, both programs perform a change of representation by displaying to the user a String containing the card's proper name (e.g. "2 of Clubs" for card 1, "Ace of Spades" for card 52, and so on).

In class this week, we saw how to restructure both programs to use an object-based representation of a playing card, which encapsulates both the low-level representation elements of a card and the common tasks associated with playing card values. You job in this lab is to perform this restructuring yourself:

• Define a Card class. This class should have:

1. instance and/or class variables sufficient to represent a playing card
2. a constructor that allows one to construct a new Card object from a positive integer (e.g. "new Card(17)" would create a Card object representing the 5 of Diamonds)
3. a definition of the special method toString(), which returns a String representation of a card object's value—the signature of this method should be "public String toString()", so that it is implicitly called whenever a Card object is used in a context calling for a String
4. appropriate access modifiers for every instance and class variable, every constructor, and every instance and class method (i.e. some, all, or none of the modifiers public, private, final, and static)
• Restructure both the printDeck.java and cardShuffle.java source files to make use of your Card object:

1. Replace all int declarations of playing card values in these programs with Card declarations: Think about this carefully! There are some uses of int (and arrays) in these programs that do not represent playing cards directly.

2. Replace all card-related tasks (such as printing out a String representation of a card's value) with appropriate calls to a card object's method or methods.

3. Remove all variable declarations and statements that are rendered unnecessary by the use of Card objects instead of raw int representations.

Even if you were perfectly happy with your own solutions to these assignments, please use the source files linked above for this lab. Your transformed source code should behave identically to the original, non-object-oriented versions.

# Standards

### Formatting Requirements

See the Style Guide, available from the General Notes section of our course web site. All elements of beautiful, clear code style described there will be expected.

In the header comments of both printDeck.java and cardShuffle.java, add the line:

`Modified by:  YOUR NAME`

### General

• Your code must be syntactically and semantically correct, which means that it has to compile successfully. Any program that cannot be successfully compiled with javac will receive no credit.

• Naturally, your code must also be behaviorally correct, which means that it should print the right output in the right format. However, partial credit will be given for partial solutions. At a minimum, be sure to test on the sample input, above

# Turn In

• A printed copy of your three source files: Card.java, printDeck.java, and cardShuffle.java.

• An electronic copy of your work should be copied to your class turn-in folder, inside of a folder named lab8.

John H. E. Lasseter