The main task is to write some extra classes for the program you wrote for the first assessed exercise. You are also required to document the entire program. In addition, you should provide a main method to make your program runnable in a stand-alone manner, without Blue). The specific tasks are as follows:

  • Write and get working the Student, Pocket and SnackBar classes described below.
  • Go through all the classes and add javadoc style comments to them. It does not matter for this whether or not you have used the sample answer provided earlier, or your own classes. The important thing is that the whole of your final submission is properly documented.
  • Use javadoc from within BlueJ to generate standard Java-style documentation for the whole project.
  • Add a main method to the SnackBar class so that you can run your program without using Blue)

For this assignment you will need to create a Blue) project containing the PackOfCrisps, Penny and Snack Machine classes. You will need to add three new classes: Pocket, Student and SnackBar. An instance of the SnackBar class simulates activity within a campus snack-bar which has a Snack Machine full of crisps and a number of hungry Students, each with a Pocket full of Penny coins. Within the SnackBar, time advances in discrete steps. At each step a random student becomes active, concentrates on snacking for a bit (i.e. buys a PackOfCrisps from the Snack Machine using a number of Penny coins, or eats a crisp from the packet they already have) and then goes back into a state of inactivity. Students may eventually run out of money, or the machine may run out of their favourite flavour of crisps, but they keep on trying to buy packets from time to time anyway. Full details of the behaviour required from the Pocket, Student and SnackBar classes, and the fields and methods needed to achieve this, are given next.

class Pocket

Fields: Each Pocket object should have the following (private) instance variable:

  • a field of type HashSet< Penny> to store the collection of Penny objects in the pocket.

Constructor Method: The Pocket class should provide a constructor method with a single int parameter representing the size of the pocket (i.e. the number of Penny objects it initially contains). The constructor should therefore initialise the HashSet field to be a new HashSet of Penny objects and then fill it with the right number of Penny objects using some kind of loop.

Other Methods: The Pocket class should provide the following additional methods:

  • a method with signature public int pennyCount() which should return the number of Penny objects remaining in the pocket.
  • a method with signature public Penny removePenny() which removes a Penny from the collection in the pocket if this is possible (there must be at least one Penny object left) and returns it. If there are no pennies left, then the method should simply return null.

class Student

Static (class) variables: The Student class should have one private static int variable STUDENT_ID, which should be assigned the initial value 0.

Fields: Each Student object should have the following private instance variables:

  • a field of class String, which stores the student's favourite crisp flavour
  • a field of class Snack Machine to store the SnackMachine object that the Student will use to buy packets of crisps.
  • a field of class String, which stores the student's 'identity number'
  • a field of class Pocket, which represents the student's pocket-full of coins
  • a field of class PackOfCrisps to store a packet of crisps bought from the snack machine. It will have the value null when the student does not have a packet.

Constructor Method: The Student class should provide a constructor method with the following signature:

public Student(String flavour, Snack Machine machine)

where the parameter flavour represents the student's favourite crisp flavour (e.g. "ready-salted") and machine is a Snack Machine object, representing the machine that a student may use to buy crisps of that flavour.

The constructor sets the values of the class and instance variables (fields) as follows:

  • the class variable STUDENT_ID is simply incremented by one.
  • the first parameter is used to set the value of the String field representing the student's favourite crisp flavour.
  • the second parameter is used to set the value of the Snack Machine field representing the machine that the student may use to buy packets of crisps.
  • the field representing the student's 'id number' is assigned a unique String incorporating STUDENT_ID. For example, if STUDENT_ID has the value 3, then the lid number' might be something like "student3".
  • the field of type Pocket is assigned a new Pocket object initially containing 20 Penny objects.
  • the PackOfCrisps field is initially assigned the value null (i.e. the student does not have a pack of crisps to start with).

Other Methods: The Student class should define the following additional methods:

1. a method with signature private void buyCrisps(), which allows a student to buy a packet of crisps of their favourite flavour. The method has the following behaviour:

  • if the student doesn't have enough money to buy a packet of crisps or the machine has run out of packets of the student's favourite flavour, then the method simply prints an appropriate warning message: e.g. "student3 doesn't have enough money to buy a pack!" or "The machine has run out of student3's favourite ready-salted crisps!";
  • otherwise, the method inserts the right number of coins into the machine and buys a packet of crisps and the PackOfCrisps object that is returned is assigned to the field of type PackOfCrisps.

a method with signature public void snackTime(), which has the following behaviour:

  • if the student does not currently have a PackOfCrisps (i.e. the relevant field is null) then the method prints out a message such as "student3 is buying a pack of crisps" and calls the buyCrisps() method;
  • if the student does have a pack, but it is closed, the the method prints out a message such as "student3 is opening the packet" and opens the packet;
  • if the student has an empty pack of crisps, then the pack is "thrown away' (i.e. the PackOfCrisps field is reset to null) and the method prints a message such as "student3 has finished the packet!"
  • otherwise, the method prints out a message such as "student3 is eating a favourite ready-salted crisp" and eats a crisp from the packet.

class SnackBar

Fields: Each SnackBar object should have the following instance variables:

  • a field of type Random
  • a field of type String[]
  • a field of type Snack Machine
  • a field of type ArrayList< Student>

Constructor Method: The SnackBar class should provide a constructor method with three int parameters: the first parameter represents the number of Students in theSnackBar, the second represent the number of PacketOfCrisps objects in the Snack Machine, and the third represents the cost (in Penny coins) of a packet of crisps from the machine.

The constructor should initialise the fields as follows:

  • the Random field should be assigned a new Random object
  • the String[] field should be initialised as an array of Strings representing flavours of crisps (four or five different flavours is fine). NB: you may want to look up array initialisation.
  • the SnackMachine field should be initialised as a newSnack Machine object with the number of crisps in the machine and the price given by the second and third parameters of the SnackBar constructor. The machine then needs to be stocked with the right number of PackOfCrisps objects of random flavours (hint: for this, you will probably want to use the random Flavour() method described below)
  • the ArrayList< Student> field should be assigned a new ArrayList of Student objects. This will then need to be filled with the right number of new Student objects (as determined by the first int parameter of the constructor method). Each new Student should be constructed with a random favourite flavour of crisps (hint: use the random Flavour() method) and the Snack Machine object.

Other Methods: The SnackBar class should define the following additional methods:

  • a method with signature private String random Flavour(),which uses the Random object to index a random crisp flavour from the selection of flavours stored in the String[] field and returns it.
  • a method public void describe() which prints out a simple description of the state of the SnackBar, such as:
The SnackBar has 10 hungry students.
The Snack Machine has:
7 packets of plain crisps,
2 packets of salt and vinegar,
0 packets of cheese and onion crisps
3 packets of prawn cocktail crisps.
  • a method with signature public void runSnackBar(int nSteps), which repeatedly carries out the following actions (exactly nStep times):
    • prints out the number of the step being carried out (e.g."Time Step 12")
    • describes the state of the SnackBar (i.e. invokes the describe() method)
    • chooses a Student at random, and 4. invokes the Student's snackTime() method.

Making Your Program Stand-alone

The final part of the assignment is to add a main method with signature public static void main(String[] args) to SnackBar class that would then allow the whole project to be run without Blue). As a minimum, this method should first construct aSnackBar object with some fixed integer values for the parameters of the constructor. It should then invoke the runSnackBar(...) method for some specified number of steps. (Note that you will need to choose sensible numbers for the various parameters.) Credit will be given for extending the main method to allow the various parameter values (especially the number of steps to be run) to be specified as command line parameters using the String[] args parameter of the main method. For example, you might invoke the project with the command:

java SnackBar 10 100

meaning, set up a SnackBar with 10 Students and then run for 100 steps.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.