COMP1406 - Winter 2014 - Tutorial 2 Working with Classes è Download the GumBall class from the tutorial website. Read through this class and see what attributes and methods it has. The intention of this class is to represent gumballs (which simply have a colour) and provide some methods to create arrays of gumballs. è Download the the GumBallMachine class from the tutorial website. A gumball machine will store some gumballs and dispense them when someone asks for one. The class is mostly completed. Your task is to write the body of the getNextGumBall() method, which outputs gumballs. The gumball machine class simply stores an array of GumBall objects and a pointer to which of these is the next one to be dispensed (returned) when the getNextGumBall() method is called. The class also has a main() method to test your getNextGumBall() method. The main() method prints out all gumballs in the machine: first by iterating over the data structure that stores them, and second by retrieving them on-by-one using the getNextGumBall() method. If these two lists are not identical, your getNextGumBall() method is not working properly. è Download the Kid class from the tutorial webpage. This class models a kid that likes to eat gumballs, but cannot eat too many of them in a single day (otherwise the kid gets sick). Each kid also has a favourite gumball colour and is happy when they can eat that gumball on a given day. Your task is to write the code for the eat() and wantsMore() methods, which are described in the Kid.java file. Write a main() method to test your methods. è Download the Simulation class from the tutorial webpage. This is a program that simulates a kid eating gumballs each day for some number of days. The main() method provided is a skeleton that you need to complete for the simulation. At the end of the simulation, a summary of what happened should be displayed to standard output. The simulation summary should display how many gumballs the kid ate each day, whether or not they got their favourite colour each day, and whether or not they got sick on each 1 day. The totals for entire period should also be displayed. You can collect this data either directly in the Simulation class or you can modify your Kid class to be able to collect and store this data. For the simulation itself (what the kid does each day), implement each of these one at a time (building the simulation up). 1. Have the kid eat as many gumballs in each day as possible. That is, the kid should keep eating gumballs until they are happy (having eaten a gumball of their favourite colour), until they have gotten sick, or until all the gumball machines are empty. 2. At the start of each new day, the kid should use the gumball machine that they last received their favourite coloured gumball from. If they have not received one in the simulation yet, choose a random machine each day. 3. Modify the GumBallMachine class by adding a method that displays how many gumballs are currently in the machine that are available to be dispensed. That is, how many gumballs are left in the machine. Once you make this change, modify the simulation to that the total number of available gumballs are counted at the start of the day and the end of the day. Include this information in the summary at the end. After completing the rest of the tutorial (play computer and bugs), if you have time come back and make the following modifications to the simulation. 1. The GummBall class has a method randomGumBalls(int, double). This method creates an array of random gumball objects, but all the colours are not valid. The second argument of the function specifies the probability that any of the gumballs has an invalid color. Use this method to fill the gumball machines. Modify the eat(GumBall) method in the Kid class so that a kid will get sick from eating a gumball with an invalid colour (in addition to eating too many gumballs). 2. It is likely that the kid in our simulation is not the only kid taking (apparently free!) gumballs from these machines. Modify your simulation so that after each gumball that the kid takes from a machine, someone takes a gumball from one of the machines with some fixed probability (say 15%). The machine that this other person takes a gumball from can be chosen randomly. 3. Now that other people are also taking gum from the machines, it is more likely that some of the machines will run out of gumballs before the kid finds their favourite colour. In the GumBallMachine class, there is a method called requestMoreGumBalls(double). This method will add more gumballs to the machine with probability specified by the input to the method. Modify your simulation so that you request more gumballs when you come across an empty machine. 2
© Copyright 2026 Paperzz