The idea is to create a program that will race horses across a window in Java. The program will be an application that will instantiate multiple instances of Horse threads. The application will run in a window that will contain three buttons:

  • [run race]
  • [reset]
  • [quit]

A total of 6 horses will run the race.

When the application starts, the horses will be lined up at the starting line. When the user clicks the [run race] button, the horses run. Your program will animate their running across the window, ultimately declaring a winner. Notice there is literally a race condition in this program: You must ensure that the horse that crosses the finish line first detects that it has won, declares itself the winner, and ensures that the other horses do not declare themselves winners, all as an atomic operation. Your program will need a lock for the critical section of code. It might have a shared memory condition variable (keepRunning or some such) that tells the other horses when to stop running. You might use the interrupted exception to stop the horses from running if you wish. You may use whichever method seems the most direct to you. Certainly, you will not use the thread.stop() method in the Java program to stop the other horses from running (for reasons to be described in class).

When a horse crosses the finish line, you will declare a winner via a dialog box. The dialog box will also report the winning time. You will use a timer as was demonstrated in the selection and merge sort programs. The timer returns milliseconds which you will convert into seconds and report in the dialog box.

The user will click the dialog to close it. If the user then presses the [reset] button the horses should be redrawn back at the starting line. If the user clicks the [quit] button, the program should terminate. All the buttons should work properly. Some interface design in alerting the user how to use the interface will be needed. Be sure your program does not generate runtime errors if the buttons are clicked in an incorrect pattern.

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.