0% found this document useful (0 votes)
79 views

CCP 1711: Assignment Defibrillator

The document provides instructions to create a simplified simulation of a bus terminal with 6 tasks: 1. Create a Ticket class to represent tickets purchased from the ticket machine. 2. Create a TicketMachine class with methods to purchase and print tickets, applying mutex locking to simulate only one machine operating at a time. 3. Create a WaitingArea class with a semaphore to control access and methods to enter and leave the area, simulating its maximum capacity. 4. Create a TicketScanner class with a semaphore to control access and a method to scan tickets, simulating a maximum number operating simultaneously. 5. Create a Customer class to purchase a ticket, enter

Uploaded by

Mohamed Ismael
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

CCP 1711: Assignment Defibrillator

The document provides instructions to create a simplified simulation of a bus terminal with 6 tasks: 1. Create a Ticket class to represent tickets purchased from the ticket machine. 2. Create a TicketMachine class with methods to purchase and print tickets, applying mutex locking to simulate only one machine operating at a time. 3. Create a WaitingArea class with a semaphore to control access and methods to enter and leave the area, simulating its maximum capacity. 4. Create a TicketScanner class with a semaphore to control access and a method to scan tickets, simulating a maximum number operating simultaneously. 5. Create a Customer class to purchase a ticket, enter

Uploaded by

Mohamed Ismael
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

CCP 1711: Assignment Defibrillator

For this lab session your assignment some much needed CPR! To do so, will we aim to create a
(very) simplified version of the Bus Terminal, in which a customer’s steps to getting on a bus are as
follows:

1. Enter the terminal


2. get a ticket from the ticket machine
3. Go to the waiting area
4. Validate ticket at the ticket scanner
5. Leave the waiting area

To set this up we will focus on creating a single runnable class, the Customer, and a Terminal
Thread. Please note that the purpose of this lab session is to help to make a start to the assignment.
What we do during this exercise is not a good solution and will require (a lot) further refinement
and refactoring (especially if you wish to get an A!). The solution does not consider things like
sequence/order when accessing a locked resource (ie is prone to race conditions), and may
potentially lead to other issues. [So please do not simply submit this!!]

Task 1: Create ticket class

The ticket class will be instantiated by the ticket machine. It will contain a boolean value
‘isValidated’ and methods to check and set the value to true.

Task 2: Create the ticket machine

The ticket machine will comprise two methods: payForTicket and printTicket, both of which are
synchronised and receive an int value as a parameter (custID). The payforTicket method takes 2
seconds to complete (use a sleep call), whilst the printTicket takes 1 second. The printTicket
returns a ticket object.

We will apply mutex to this class, as there is only one ticket machine available at only one time.
Add a boolean value ‘inUSe’ to the class. This should be check by the payforTicket method and a
wait() call made if its value is true.

Waiting objects will be unblocked by the printTicket method, which calls notify() prior to returning
a ticket (and sets the value of inUse to true).

Task 3: Create the waiting area

As the waiting area has a capacity of 10 we will use a private semaphore to control access to the
waiting area object. Create the waiting area class and instantiate a private semaphore with 10
permits (ie set the constructor parameter to 10).

The waiting area will have 2 methods, enterWaitingArea and leaveWaitingArea, both of which
receive an int parameter (custID).

enterWaitingArea will check to see the available permits and print out that the customer has to wait
if the available permits are less than 1. It will then call the semaphore’s acquire method before
printing out the message “customer [customer ID] has entered the waiting area.
The leaveWaitingArea method simply releases the permit (ie [nameOfSemaphore].release). Add
appropriate print statements so as to clearly display what is happening.

Task 4: Create the ticket scanner

Let’s assume there are 5 ticket scanners available. Use a semaphore to control access to the object
(as above). The ticket scanner class will consist of a single method: scanTicket(). This method
receives a customer object, and changes the status of the customer’s ticket to valid (ie it sets the
boolean isValid to true).

Again, insert appropriate print statements to indicate whats happening.

Task 5: Create the customer class

The penultimate step is to create the customer class. The customer class will have references to the
above classes, therefore declare a ticket, waitingArea, ticketScanner, waitingArea, as well as an
custID of type int. Set the four user classes via the customer’s constructor.

The customer class is a runnable, therefore add the run method. In the run method will will set out
our basic algorithm outlined at the beginning of this document. [Remember to add appropriate print
statements in between the following steps]

1. So, firstly the customer gets a ticket: call the ticketmachine’s payForTicket method. (to
slow things down a little we will sleep for 100ms between each of these method calls.
2. Get a printed ticket: call the ticketmahine’s retrieveTicket.
3. Go to the waiting area: call the waitingArea’s enter method.
4. Go go the scanner: call the scanner’s scanTicket method and assign the return value to
customer’s ticket
5. Leave the waiting area: check to see if the isValid is true, then call the waitingArea’s
leaveWaitingArea method.

For now we will add sleep for 20 seconds between steps 3 and 4 to simulate waiting for the bus’
arrival.

Task 6: Create our (main) terminal class.

Finally lets get our code up and running by creating a terminal class which will provide our
customer objects. The terminal is a Thread, so in the run method create a customer object, passing
in an id, ticketMachine, waitingArea, and ticketScanner constructor parameter objects. Pass the
customer object to a thread, start it, and loop infinitely.

In the terminal class’ main method instantiate a terminal object and start the thread.

Run and test your code.

You might also like