This assignment has you write a program that has smileys run a simple lap race and computes statistics about their running times. The assignment gives you practice with simple uses of ArrayLists, and additional practice with boolean expressions, if statments, loops and nested structures
This program begins with smiley racers, on the left edge of the graphics display, facing right; we see them in profile. They each have a name displayed, in color, in the middle of their faces, so we can root for our favorite. The race begins when the user presses the Go! button; these racers then begin moving to the right. When they hit the right wall, theyve completed a lap. They reverse direction, their profiles face left, their speed is adjusted as discussed below, and they head for the left wall. When a racer hits the left wall, again, the profile and direction are reversed, the speed is adjusted, and the racer heads for the right wall; another lap has been completed.
When a racer completes the race; that is, runs the number of laps that constitue a race, the smiley stops. When all racers finish the race, the fastest time and the name of the racer with that time, the slowest time and the racer with that time, and the average racer time, along with a title introducing these statistics, appear in the small window at the bottom of the race track. The program then stops; the window remains open so the user can read the statistics. The user clicks on the close box to close the window and end the program; if the close box is clicked while the race is still running, it still shuts down the program.
For this assignment, weve again helped you get started by providing a significant amount of code and lots of comments and documentation. All your work is done in the SmileyAnimation and SmileyRacer classes. We provide several already-written class files; one of these, SmileyFace, has several public methods and constants you may need to employ, so we included the text file SmileyFace.txt describing those public items. (They are the same as in Assignment 2; we are reusing the SmileyFace class yet again, saving you a lot of work!)
You also use one method from the SmileyDisplay classrepaint()which works just as it did in Assignment 2, and one public constant from that class, SmileyDisplay.BACKGROUND_COLOR. (Since we need just these two simple-to-use items, weve not included a SmileyDisplay.txt documentation file for this assignment.)
These files are stored together in the archive file Lab3.zip. Download Lab3.zip to your computer and unzip it into its own folder. Replace all spaces marked // your code goes here with your own Java code so that a complete, correctly working program results.
Some additional technical details:
Each smiley racer is a smiley face with additional characteristics; each has all the attributes of a smiley face plus its race name, the names display color, and information to track its status while it races, such as which lap it is on, which direction it is moving, and whether it has finished the race. So, the SmileyRacer class is extended from the SmileyFace class; it gets all the characteristicsall the fields, constants and methodsof the SmileyFace class. We then added additional fields, constants and methods to turn the smiley into a racer.
The upshot of this is that all the public methods available to manipulate or access smiley faces and their face parts are available for manipulating and accessing smiley racers and their face parts. You will not see these methods explicitly listed in the SmileyRacer class, but they are a part of it.
Thus, to construct a racer, you first construct a smiley face, and set its attributes as needed (see below). You then pass this smiley face into the smiley racer constructor, along with the racers name and name color, to make a smiley racer; the constructor will use the smiley faces attributes to set the same attributes in the smiley racer.
You can also pass an exisiting smiley racer into the smiley racer constructor (along with a new name and color for it). This works because a smiley racer is (an extended version of a) smiley face, and so the contructor will treat it as a smiley face.
To avoid having to write new methods to make the smiley appear in profile, make use of a couple of tricks:
Position your smileys so that, when they move left to right and right to left, they will not collide. You do not have to worry about constructing smiley faces so that, when the race starts, their left sides touch the left-hand side of the race track; the racer constructor will translate them therewe dont force the user to construct faces this precisely when it is just not needed.
We measure time in ticks. A tick is just an arbitrary time unit that corresponds roughly to the time between one frame of animation and the next, kind of like saying 24 frames per second means that the time between one frame of film and the next is 1/24th of a second; the 1/24th of a second would be a tick. The length of a tick is set by a constant in the SmileyAnimation class (see the code for details).
The racers base speed is in units of pixels per tick, and is determined when the racer is constructed. Also determined then is the racers racing strategy. A racer can run the race at a constant speed, or it can decrease its speed the same amount each lap (it peaks early) or it increases its speed the same amount each lap (it paces itself and gradually pushes harder). Of course, when decreasing its speed, it cannot go to zero and still finish the race, so the racer must move at least 1 pixel per tick. The fastest a racer can go is double its starting speed.
Your program should work for zero racers. All that happens is that, when the progam is run, the stats screen immediately displays a message that there was no race. In particular, do not display any statistics for a non-existent race! Your program should also handle a large number of (relatively) small-sized racers, at least into the 10 to 20 range. The animation may slow down when the number of racers is large, and the racers names may go outside the smileys face; that is acceptable.
As before,when compiling the program, it is best to use the command javac *.java. You run the program with the command java Smiley.
Remember to test your program incrementally, as described in Assignment 2; it will very likely save you a lot of time and frustration!
And again, a significant failure to follow class style standards on a lab exam can result in a not pass for that exam. In particular, do not use a break statement other than as the last line of a case block in a switch statement.
Lab exam #3 will be very similar, but perhaps not identical, to the program you are to code for this exercise.
The lab exam will have you code one or more of the following methods from the SmileyAnimation or SmileyRacer classes:
Remember to read over the lab exams program comments before beginning; they will tell you what you are to complete and give any particulars (such as, for example, the number of racers to construct, the characteristics of a racer you are to set, such as its name, or the kind of behavior it should exhibit when running the race).