First, enter the cs190m directory:
$ cd cs190m
Then, download lab13.zip into the cs190m directory. Run the following commands:
$ unzip lab13.zip $ cd lab13
For the sake of convenience, one office floor decided to make the sole bathroom, which was for men, into a unisex bathroom (the nearest women's bathroom was too far away). Obviously there would be problems if they shared the bathroom simultaneously, so a rule was put into place that men and women cannot simultaneously share the bathroom. In addition, only three people may be in the bathroom at once.
Your task is to write a simulation of the unisex bathroom. The logic around the simulation itself is provided; however, you need to write logic for the Bathroom class. In particular, there are four methods you should be concerned with: maleEnter(), maleExit(), femaleEnter(), and femaleExit(). The logic between the two genders is, of course, very similar.
Whenever a person tries to enter the bathroom, he/she must first check to see if people of the other gender occupy it. If so, the person must wait patiently until the bathroom is emptied. Of course, the person must also wait if there are three people in the bathroom, as well. When a person leaves the bathroom, the others in line are "alerted" (by said person walking out the door). The next eligible person may then enter the bathroom. You should keep track of who is in the bathroom with the male and female variables and who is waiting with the maleWaiting and femaleWaiting variables.
Simply running the Lab13 class will start a simulation. The simulation will spawn male and female threads which occupy the bathroom for a certain period of time before leaving. Output can look something like this (the tester is random)
M: 0 F: 0 WM: 0 WF: 0 female joined M: 0 F: 1 WM: 0 WF: 0 M: 0 F: 1 WM: 1 WF: 0 M: 0 F: 1 WM: 2 WF: 0 female joined female exited M: 0 F: 1 WM: 2 WF: 0 M: 0 F: 1 WM: 3 WF: 0 female joined M: 0 F: 2 WM: 3 WF: 0 M: 0 F: 2 WM: 4 WF: 0 female exited M: 0 F: 1 WM: 5 WF: 0 female joined female exited M: 0 F: 1 WM: 5 WF: 0 female joined M: 0 F: 2 WM: 5 WF: 0 M: 0 F: 2 WM: 6 WF: 0 female exited M: 0 F: 1 WM: 7 WF: 0 female exited male joined male joined male joined M: 3 F: 0 WM: 4 WF: 0 M: 3 F: 0 WM: 4 WF: 0 M: 3 F: 0 WM: 4 WF: 1 male exited male joined M: 3 F: 0 WM: 3 WF: 1
The simulation will run indefinitely; of course, the males and females are not particularly courteous to each other, in that one gender can theoretically occupy the bathroom forever.
To turn in your lab:
$ cd ~/cs190m/lab13 $ turnin -v -c cs190m -p lab13 *.java
Many of you will probably run across problems while programming at some point during a lab. If that's the case, here are the resources you should use, in order:
Lab created by: Daniel Tang. This problem was first found in The Little Book of Semaphores.