CPSC 124 | Introduction to Programming | Spring 2024 |
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.
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.
API documentation for the provided classes can be found below:
For all exercises:
Unless otherwise indicated in the problem, if specific output is shown to indicate what your program should do, be sure your program's output matches what is shown exactly.
Employ good programming style, including choosing descriptive variable names and autoformatting your code. Use blank lines and comments to group and label statements that together perform a distinct task.
Write Javadoc-style comments for the program as a whole and for each subroutine and function. Also identify and check preconditions, throwing an IllegalArgumentException if a precondition is violated.
Include your name and a description of the program in each program.
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.
Write a program called CardSorter to sort a deck of cards into different piles by suit. In particular, your program should:
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.
Write a program called CardBingo to play a card game version of Bingo for two players. It should work as follows:
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:
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.)
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.
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.
Write a program called DiceGame for the following single-player game:
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:
Use the provided FreezeDice class — do not use Math.random() to generate random numbers to simulate dice rolls like we've done in the past. (See the API for FreezeDice to figure out how to use this class.)
An array to store the five FreezeDice objects. (Use what you know about working with arrays and what you know about working with objects to figure out working with arrays of objects — remember that you need to both create the array compartments and put a value into each slot, and that you'll need to create a new FreezeDice to put into each slot.)
A subroutine printDice which takes an array of FreezeDice as a parameter, and prints out the current value of each die in the array.
A main program which carries out the game, using the printDice subroutine as appropriate.
You may create additional subroutines/functions if you find it useful to do so, but this is not required.
Make a copy of the provided AnimationStarter.java file, naming the new program Banners.
Fill in/modify the parts marked with *** comments to display at least three animated banners with different messages, starting positions, colors, and speeds. The window title should be "banners".
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.
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.