CPSC 124 Introduction to Programming Spring 2024

Lab 10
Collections of Varying Size

Due: Tue 4/23 at the start of lab


Introduction

Arrays are useful for keeping track of a collection of things...and sometimes (actually, quite often) the number of things in the collection varies. Furthermore, you may not know how many elements there will be in advance so you cannot guarantee the array will be big enough when you create it.

In this lab you'll modify a program for the casino game Red Dog so it can work with a variable number of players.

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:

You can find API documentation for ArrayList below:


Exercises

For all exercises:

Also, you are once again working with multiple files and thus need to make sure that every file needed for the program is compiled. Since you will only be making changes to the main program, it should be sufficient to only compile that file (Java will automatically compile the others needed). Refer back to lab 9 for more on working with multiple files.


Exercise #1

In this exercise, you will modify RedDogArray.java so that new players can be added, players can leave the game, and players who run out of money are eliminated.

The first step is to modify the program so that it works with partially-full arrays. Recall that with partially-full arrays, you need to keep track of the number of elements in the array in addition to the array itself:

You should now be able to compile and run the program and have it work just as it did originally.

Next, write three functions to handle removing players, as described below. Note that all three functions should return the number of players removed — this is necessary because the main program will need to update numplayers (a function cannot update a variable declared in the main program).

Now, update the main program:

You should now be able to compile and run the program. Test that you can remove players, that players who go bankrupt are removed from the game, and that the game ends when all of the players have been removed/eliminated.

Adding new players means that the array may need to grow:

Then:

You should now be able to compile and run the program. Test that you can add players without the program crashing and that the new players are incorporated into the next round of the game.


Exercise #2

In this exercise, you will modify RedDogArrayList.java so that new players can be added, players can leave the game, and players who run out of money are eliminated...in other words, the same things you just did in exercise #1. The difference here is that you'll use an ArrayList instead of an array, greatly simplifying the tasks of inserting and removing players — you can just call the appropriate method of ArrayList instead of having to shift elements or grow the array.

The first step is to modify the program so that it works with ArrayList:

You should now be able to compile and run the program and have it work just as it did originally.

Next, write two subroutines to handle removing players, as described below:

Then:

You should now be able to compile and run the program. Test that you can remove players, that players who go bankrupt are removed from the game, and that the game ends when all of the players have been removed/eliminated.

Adding players:

You should now be able to compile and run the program. Test that the new players are incorporated into the next round of the game.


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.