CPSC 124 Introduction to Programming Spring 2024

Lab 8
Using Objects

Due: Tue 4/2 at the start of lab


Introduction

The bundling of subroutines and variables into modules known as objects is at the heart of object-oriented programming. In this lab you'll gain experience writing programs that use objects other than strings. You'll also get some practice with figuring out how to use new classes — something that comes up often in object-oriented programming.

Collaboration

Labs and projects are for practice and learning. While it can be very productive to work on problems with your peers, it is also easy to underestimate how much you yourself understand and can do in such situations — so often something looks easy when someone else does it! With this in mind, you should always make the first attempt on a problem or programming task yourself using only the resources provided for the course (textbook, slides and examples posted on the schedule page, other resources linked on the course webpages). After that point, you are encouraged to come to office hours and/or go to Teaching Fellows. You may not collaboratively write solutions or code, and you may not copy solutions or code written by others, even if you contributed ideas.

You can discuss specific exercises with other students in general terms — such as how you might get started on that problem, or how or when to use various programming constructs, or strategies for debugging — but how to use a particular programming construct to solve a specific problem or debugging a particular program should only be discussed in office hours or with the Teaching Fellows.


Setup


API Documentation

API documentation for the provided classes can be found below:


Exercises

For all exercises:

Continue to write pseudocode (and to practice stepwise refinement in the development of your pseudocode) before writing Java. Also continue to practice incremental development — test as you go (using placeholder variables and subroutines as needed) rather than writing the whole program before you try to compile or run it for the first time.


Exercise #1

Four constants have been defined for the four suits: Card.HEARTS, Card.SPADES, Card.DIAMONDS, Card.CLUBS. Compare these values to each card's suit to determine which pile to put the card into, much like you compared the key the user pressed to the values KeyCode.LEFT, KeyCode.RIGHT, etc in Minesweeper to determine what the key was.


Exercise #2

Note that this program doesn't actually ask the user for any input — the user will just sit back and watch the program run. The program should, however, print out information so the user can see the game being played. In particular, as the dealer deals each card from the second deck, the program should print which card is dealt and who scores the point (if one is scored). At the end, the winner should be identified. For example:

The dealer deals the Queen of Hearts.
  Player 1 scores a point.
The dealer deals the 2 of Diamonds.
  No one scores.
The dealer deals the 6 of Spades.
  Player 2 scores a point.
...

Player 1 wins!

Your program must have the following components:

  1. A function called findCard which takes a hand and a card as parameters, and which returns the position of an equivalent card in the hand if there is one or -1 if the hand does not contain an equivalent card. Two cards are equivalent if they have the same suit and value.

    Note that "taking a hand and a card as parameters" means that the function should have two parameters — one an object of type Hand and the other an object of type Card.

    This function will be very similar to the findFirstVowel function from the Pig Latin program in lab 7 — but instead of finding a vowel in a string, you're finding a card in a hand.

    Be careful when checking if two cards are equivalent! a == b compares the contents of the boxes a and b — which, for objects, means comparing the addresses where the objects are stored in memory. However, two different Card objects can have the same suit and value and thus be equivalent. Be sure to compare the suits and values of the two cards instead of the cards themselves! (Unlike String, Card does not have an equals method to check if two different Card objects have equivalent values.)

  2. A main program which carries out the game described with two players, using findCard as appropriate to determine when a player scores a point.

You may create additional subroutines/functions if you find it useful to do so, but this is not required.


Exercise #3

In many dice games, players have several throws on each turn to try to achieve a high-scoring combination of dice. All of the dice are rolled on the first throw. After each throw, some dice may be frozen; frozen dice are not rolled again on that turn. After the last throw, all of the dice (including those that have been frozen) are scored.

You should print out enough information so the player can play the game, such as printing the dice values before each time the player is prompted, the points earned on a turn, and the final score.

Your program must also have the following elements/components:

You may create additional subroutines/functions if you find it useful to do so, but this is not required.


Exercise #4

Use the provided TextBanner for your banners — you will need three (or more) TextBanner animation variables. Initialize them where indicated; drawing a frame means drawing each banner and updating the animation variables means moving each banner.

Also remember that with JavaFX programs you need to use jxfc and jxf to compile and run, respectively.


Handin and Grading

Don't forget to hand in your work when you are done!

As in lab 2 (and all labs), grading will be based on both functionality and style. Style means following the good programming style conventions outlined in lab 2, as well as producing reasonably compact and elegant solutions. "Reasonably compact and elegant" means that your program is not significantly longer or more complex than it needs to be. In particular, you must use loops when loops are appropriate — achieving repetition by copying-and-pasting a bunch of code will not earn credit.