For this lab, you will do two exercises involving for loops. In the first exercise, you will write a trivial, unrealistic simulation of a baseball game. For the second exercise, you will write a GUI program that makes some random "art" in the style of Piet Mondrian. We looked at some of his works in class yesterday.
To get started, create a directory named lab4. Copy Mondrian.java from /classes/cs124 into your lab4. You will need this file as a starting point for Exercise 2. (You do not need TextIO.java for this lab, since neither of the exercises uses input.)
This lab is due at the beginning of next Thrusday's lab. Also remember that Project 1 (from Lab 3) is due by 2:00 PM on Friday, September 30, the day after next week's lab.
Exercise 1: Write a program that simulates a baseball game in the following sense. In each inning, each team scores some random number of runs. Add the number of runs to the team's score. Report the score after each inning. Use a for loop to run the game for nine innings. At the end, say which team won. If the scores are the same at the end of nine innings, you can say that the game is a tie. (Of course, that's not the way baseball really works, but it will do for this simulation.) The output should look pretty much like this:
Score after inning 1: Yankees 1 Mets 2 Score after inning 2: Yankees 5 Mets 2 . . . Score after inning 9: Yankees 12 Mets 7 The Yankees win.
You should think about the best way to give a team a "random number of runs." Just picking a random number between, say, 0 and 5, is a possibility, but not very realistic. You might think about trying for something more realistic. For example, the most likely number of runs in an inning is zero. There are other ways to increase the realism. For example, the second team doesn't bat in the ninth inning if they are ahead at the middle of the inning. Extra credit might be given for particularly nice approaches to increasing the realism of the simulation, including playing extra innings in the event of a tie.
(By the way, a much more realistic simulation of some kind of Fantasy Sports League would not be a bad idea as a final project for the course.)
Exercise 2: Many paintings by Piet Mondrian show rectangular areas bounded by vertical and horizontal bars. For this exercise, you will draw random, vaguely Mondrian-like artworks that show randomly colored vertical and horizontal bars on a randomly colored background. (This is based on an exercise used in CS124 by Professor John Vaughn a few years ago.) Here is my solution to the exercise:
A new work of art is generated by this program every five seconds. The size of the canvas is 500-by-500 pixels.
The basic algorithm for drawing each artwork goes as follows: Fill the canvas with a randomly selected color. Choose a random number of lines to draw. For each line, decide randomly whether to draw a vertical bar or a horizontal bar. Choose a random color and a random thickness for the bar. Choose a random x-location (for a vertical bar) or y-location (for a horizontal bar). Draw the bar. A horizontal bar has width 500, and its height is given by the selected random thickness. For a vertical bar, the height is 500 and the width is given by the selected thickness.
To draw each bars, I use the subroutine g.fill3DRect, which draws a filled rectangle that is supposed to have a 3D appearance. In particular, g.fill3DRect(x,y,w,h,true) draws a filled rectangle with upper left corner at (x,y), with width equal to w, and with height equal to h. The fifth parameter is a boolean value, which can be either true or false. A true in the fifth position is supposed to make it look like the rectangle is raised a bit above the screen, while a false is supposed to look like the rectangle is pushed in a bit.
To program your version, you should edit the file Mondrian.java. This starter file contains the complete program, except for the actual drawing of the art. Do all your work in the paintComponent method; currently, this method simply fills the drawing area with a random color. You should implement the algorithm described in the preceding paragraph, with only minor variations. Be sure to use a for loop in your code!
Note that Mondrian.java defines two useful functions, randomHue(s,b) and randomColor(), which you can use in your code for creating random colors. (Remember that a function is a subroutine that returns a value. In Chapter 4, you will finally learn how to write your own functions, but you already know how to call functions that someone else has defined.) The randomColor() function takes no parameters and simply creates a color with random amounts of red, green, and blue. You would use it in a statement such as "g.setColor(randomColor());".
The randomHue() function uses a different way of specifying colors, in terms
of "hue," "saturation," and "brightness." It takes two parameters, representing
the saturation and brightness of the color, and it uses a random hue.
The saturation and the brightness must be real numbers
in the range 0 to 1. (You can probably best understand these terms by experimenting with
the function.) For example,
g.setColor(randomHue(0.2,1)) sets the drawing
color to be an unsaturated, bright color; this is how I set the background color in the starter
version of Mondrian. The less saturated a color is, the closer it is
to a shade of gray.
g.setColor(randomHue(1,1)) would give a bright, fully
g.setColor(randomHue(0,Math.random())) would give a random
shade of gray.