Homework 2

Homework 2

Homework Reflection SurveyGradescopeStarter Code
Due: Thursday September 25, 2025 5:00PM
Goals:
  • Practice with arrays.
  • Practice creating objects, accessing fields, and calling methods.
  • Practice with conditionals and for loops.
  • Practice some more with strings.
Submit these files:FaceUpHand.java,FaceUpCard.java

In this assignment we will implement a made-up card game we'll call FaceUp. Along the way, you'll practice what you learned about Java this week, including using arrays and creating objects!

FaceUp Card Game

When the FaceUp game starts, you deal five cards face down. Your goal is to achieve as high a score as possible. Your score only includes cards that are face up. Red cards (Hearts and Diamonds) award positive points, while black cards (Clubs and Spades) award negative points. Cards 2-10 have points worth their face value. Cards Jack, Queen, and King are worth 10 points, and Ace is worth 11 points.

The game is played by flipping over cards, either from face-down to face-up or from face-up to face-down. As you play, you are told your total score (i.e. total of the face-up cards) and the total score of the cards that are face-down. The challenge is that you only get up to a fixed number of flips and then the game is over.

Here is an example of the output from playing the game:

FACE-DOWN | FACE-DOWN | FACE-DOWN | FACE-DOWN | FACE-DOWN
Face up total: 0
Face down total: -5
Number of flips left: 5
Pick a card to flip between 1 and 5 (-1 to end game): 1

Queen of Hearts | FACE-DOWN | FACE-DOWN | FACE-DOWN | FACE-DOWN
Face up total: 10
Face down total: -15
Number of flips left: 4
Pick a card to flip between 1 and 5 (-1 to end game): 2

Queen of Hearts | Jack of Clubs | FACE-DOWN | FACE-DOWN | FACE-DOWN
Face up total: 0
Face down total: -5
Number of flips left: 3
Pick a card to flip between 1 and 5 (-1 to end game): 2

Queen of Hearts | FACE-DOWN | FACE-DOWN | FACE-DOWN | FACE-DOWN
Face up total: 10
Face down total: -15
Number of flips left: 2
Pick a card to flip between 1 and 5 (-1 to end game): 10

10 is not a valid card
Queen of Hearts | FACE-DOWN | FACE-DOWN | FACE-DOWN | FACE-DOWN
Face up total: 10
Face down total: -15
Number of flips left: 2
Pick a card to flip between 1 and 5 (-1 to end game): 0

0 is not a valid card
Queen of Hearts | FACE-DOWN | FACE-DOWN | FACE-DOWN | FACE-DOWN
Face up total: 10
Face down total: -15
Number of flips left: 2
Pick a card to flip between 1 and 5 (-1 to end game): 5

Queen of Hearts | FACE-DOWN | FACE-DOWN | FACE-DOWN | 3 of Hearts
Face up total: 13
Face down total: -18
Number of flips left: 1
Pick a card to flip between 1 and 5 (-1 to end game): -1

----------------------
Your score: 13
Best possible score: 15

At each point where the program asks the user to pick a card, the game waits for user input.

Code

We’ll implement the game using four classes, two of which you’ll leave unchanged, and two you will modify and submit. Note that the FaceUpCard.java and CardDealer.java files have some pieces that might help you with Lab 2. Download the starter code using the link at the top of the page. As we have been doing, move the extracted hw2 folder to your cs201 folder and open this folder in a new VS Code window.

  • FaceUpCard: This class is very similar to the Card class we use in Lab 2. You will complete two methods (getCardPoints and isRedCard) here which are needed for the FaceUp game.
  • FaceUpHand: This class keeps track of the state of the game. Most of the methods you will implement are here.
  • CardDealer: This is a modification of the Deck class you use in Lab 2. Leave this code unchanged.
  • FaceUpGame: This class implements the game interface. It gets data from the user, displays the state of the game, and repeats the process. Leave this code unchanged.

The TODO comments in FaceUpCard.java and FaceUpHand.java mark what you need to implement in this assignment.

Note

We won’t check for style yet, but try to stick to the guidelines we introduced in class.

How to proceed

You may fill in the details of the FaceUpCard and FaceUpHand classes however you’d like, but here’s what we recommend.

  1. Complete the two methods (marked as TODO) to the FaceUpCard class (described in comments in the code). Add a main method to this class and test the two methods to make sure they work. There are more details below on how to write comprehensive tests.
  2. Read through the other two new classes FaceUpHand and FaceUpGame. Make sure you understand what all of the methods in FaceUpHand are supposed to do and that you understand how they’re used in the FaceUpGame class.
  3. Implement the required methods (marked as TODO) in the FaceUpHand class incrementally. Write one method and then test it. Again, add a main method and write a small test or two to make sure it behaves like you’d expect (see some examples in the next section). If you try to implement all of the methods at once and then test it by jumping to running FaceUpGame, 1) it’s unlikely to get everything working the first time and then 2) it’s going to be very hard to debug where the problem is.
  4. Once you have things working, play the game!

Testing your implementation

Like the previous homework, you should write comprehensive test cases for each class in the main method of FaceUpHand.java and FaceUpCard.java. These are the requirements you must meet in your test cases:

FaceUpCard.main

  • card object is initialized
  • tests isRed on a red card
  • tests isRed on a black card
  • tests getCardPoints on a card where points != value

FaceUpHand.main

  • hand object is initialized1
  • calls flipCard on hand
  • calls calculateOptimal on hand
  • calls faceDownTotal on hand
  • calls faceUpTotal on hand

Although it’s not strictly required, we recommend that you test FaceUpHand on multiple hands. You can explicitly create some cards in the hand, call the methods you implemented and then ensure the number of points match the expected values.

Here are some example hands (each an instance of FaceUpHand), and their optimal scores:

Ace of Spades, 3 of Hearts, 6 of Hearts, King of Hearts, 3 of Diamonds
Best possible score: 22

4 of Clubs, 8 of Hearts, Jack of Diamonds, 8 of Clubs, 6 of Diamonds
Best possible score: 24

3 of Spades, 5 of Diamonds, 3 of Diamonds, 7 of Spades, 5 of Spades
Best possible score: 8

Ace of Diamonds, 10 of Spades, 4 of Diamonds, 8 of Clubs, 9 of Spades
Best possible score: 15

3 of Clubs, 3 of Spades, 2 of Hearts, 4 of Spades, Jack of Diamonds
Best possible score: 12

9 of Spades, 5 of Clubs, Jack of Clubs, 2 of Clubs, Jack of Diamonds
Best possible score: 10

King of Diamonds, 3 of Diamonds, King of Hearts, 7 of Clubs, Ace of Hearts
Best possible score: 34

7 of Clubs, 6 of Spades, 4 of Clubs, 3 of Hearts, 3 of Spades
Best possible score: 3

Jack of Clubs, 6 of Clubs, 8 of Clubs, 7 of Spades, Queen of Spades
Best possible score: 0

10 of Hearts, Ace of Diamonds, Queen of Spades, Jack of Clubs, Queen of Clubs
Best possible score: 21

Footnotes

  1. We recommend using the constructor that takes in cards for reproducible testing!↩︎