This week's book giveaway is in the Cloud/Virtualizaton forum.We're giving away four copies of Mesos in Action and have Roger Ignazio on-line!See this thread for details.
Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!

# Story Problem

Jared Knowlton
Greenhorn
Posts: 17
Steve, Jon, Jennifer, and Kelly own dogs and take their dogs to the park between 12:00 noon and 4pm. There are four activities the owners can do while at the park. These are as follows: Play catch with the dog, clean the dogs poop, walk the dog, or a fourth activity. The owners can only be doing one activity at a time, and the order that they do activities should be random. The park has two tennis balls for playing catch and one pooper-scooper. This means that only two owners can be playing catch at once, and only one owner can be cleaning poop at a time. If an owner tries to play catch or clean poop and can't because the tennis balls or the pooper-scooper are being used they should do their fourth activity until a ball or the pooper-scooper becomes available. If there are two or more owners that are waiting for the same object then the owner that started to wait first should get the object first (FIFO). Then they should start the activity they originally tried to do. The time the owners take to do each activity varies. The owner activity lengths, in minutes, are as follows:
Owner Catch Clean Walk
Steve 5 2 10
Jon 8 2 4
Jennifer 10 4 8
Kelly 1 2 6
Each owner also has a fourth activity they can do but the activity and activity lengths vary from owner to owner. The lengths should be random whole numbers. Steve reads between 2 and 8 minutes, Jon plays game-boy between 4 and 6 minutes, Jennifer cross-stitches between 1 and 5 minutes, and Kelly crochets between 6 and 10 minutes. If the owner does their fourth activity because it was randomly chosen then their time should be random as well, according to the above description. If the owner does their fourth activity because they are waiting for a ball or the pooper-scooper, then they should do the activity until the object becomes free and then start doing that object's activity. You should record what owner begins which activity at what time. You should save all this information to one file. Each record in the file should be on a separate line and each record should be in the format 'Owner:Time:Activity.'

Jeroen Wenting
Ranch Hand
Posts: 5093
Sounds like something that calls for a multithreaded solution, with a thread for each dog/owner created from the main application.

Robert Paris
Ranch Hand
Posts: 585
Look into mutexes and semaphores. That's what you want. But we're not going to do your homework for you. :roll:

Jared Knowlton
Greenhorn
Posts: 17
I appreciate the tips you have given me and its not homework.

Layne Lund
Ranch Hand
Posts: 3061
Since it's not homework, that is even more of a reason to let you do most of it yourself. Have you run into any particular problems with your solution yet? The more specific your questions the better we are able to give you specific answers.

Jared Knowlton
Greenhorn
Posts: 17
Let me just give you some background. I applied for a Software QA job and at the end of the interview the gave me this story problem and told me I had until Friday to do it. I guess you could call it homework but instead of me paying tuition to be given a problem like this it may result in me getting paid.
My skills and experience as a java programmer are lacking. I have found it hard to really grasp java without having real life experience and I have found it hard to get a job without that experience.
Having given you that background let me tell you I have wrote 2 classes and I found some code for the random number generation. I don't know if you want me to e-mail you the code or if you want me to post it here.
I will say this again and again - your help is very much appreciated.

Robert Paris
Ranch Hand
Posts: 585
If you've got an entire day to spend at barnes and noble (or at home if you buy it), there's some fantastic code in: Multithreaded Programming with JAVA™ Technology
http://www.amazon.com/exec/obidos/tg/detail/-/0130170070/qid=1077168677//ref=sr_8_xs_ap_i1_xgl14/002-2943192-8906469?v=glance&s=books&n=507846
You can use their code (with almost no alterations) to handle all the control of access to the pooper-scooper and tennis balls.
I believe (for a 14-day trial) you can read the whole book for free on PDF here: http://safari.informit.com/?XmlId=0-13-017007-0

The source code is here (combined with code from another book):
http://www.lambdacs.com/classes/programs_6_Oct_02.tar.gz

sever oon
Ranch Hand
Posts: 268
I'm thinking you won't have this job for long if you can't knock out a solution to this problem in a couple of days. Please understand, I'm not being flip here, but when an employer hands out a problem like this to a candidate, it's usually because you need to have the skills the problem requires of you to be a success on the job.
All I can say is study up on your multithreading. Each dog/owner pair gets its own thread and the ball/pooper scooper represent resources to which exclusive access must be had by the using thread. That'll help you home in on what you need to study...
Have fun!
sev