handout #9
CSE142--Computer Programming I
Programming Assignment #3
due: Tuesday, 1/24/06, 2 pm
This assignment will give you practice with
value parameters, Java objects and graphics. You will be using a special class called DrawingPanel
written by Stuart and his coauthor Marty Stepp and a class called Graphics that
is part of the Java class libraries.
You will also refer to various color constants from a class called Color
that is also part of the standard Java class libraries.
The assignment is divided into two parts. The first is meant to be a warm-up to
get you used to basic drawing operations.
The second part will allow you to practice drawing a complex figure with
a parameterized method. You will
want to look at handout #7 for an example of how to use these various classes
to draw.
For this assignment you are limited to the
language features in chapters 1 through 3. Remember that you are not to use more advanced features to
solve the problem. You can't, for
example, make use of if/else statements to solve this task. You will, however, use the DrawingPanel
and Graphics classes described at the end of chapter 3 as well as the color
constants from the Color class, as described in chapter 3.
Part 1:
Drawing1.java (4 points)
This
part of the assignment is meant to give you some practice with the DrawingPanel
and Graphics classes. You can
either produce a specific output or you can design your own output. If you want to design your own, it must
meet the following criteria:
This flexibility will allow you to be creative
and to draw any figure you like.
After the assignment is turned in, we may anonymously show off some of
the figures drawn by students for everyone to see.
If you don't want to design your own output,
then you should meet the following criteria. You are to construct a DrawingPanel with a width of 400 and
a height of 200. It should have a
green background and should have two solid black lines (one horizontal and one
vertical) that divide it into 4 equally sized quadrants.
In the upper-left and lower-right quadrants you
are to draw a red oval that has horizontal and vertical lines that divide it
into four equally sized parts. You
should draw the largest oval that doesn't go outside the quadrant. The inside of the oval should be red
throughout and the lines should be black.
You do not have to draw an outline around the oval.
Below is a picture of
what your drawing panel should look like.
You are to to produce either the figure above or
one of your own design. Your class
should be called Drawing1.java.
You can break this program up into methods if you want to, but it's okay
in this case to simply have all of the drawing commands appear in the main
method.
Part 2: Drawing2.java (16 points)
This
second part of the assignment will involve more complex computations with a figure
that has several levels of structure.
You are to produce the following image.
This image has several levels of structure. You will find that there is a basic
subfigure that occurs throughout, which is a square with concentric circles in
it. The upper-left corner has
exactly one such subfigure that is 100 pixels wide and has 10 concentric
circles. The concentric circles
are enclosed in a square and drawn in black against a yellow background. The panel also has three square grids
that are composed of multiple copies of this basic subfigure. The grid in the lower-left part of the
panel has upper-left coordinates (18, 175) and is a 6-by-6 square of subfigures
each 24 pixels wide with 4 concentric circles. The grid in the upper-right part of the panel has upper-left
coordinates (180, 25) and is a 5-by-5 square of subfigures each 40 pixels wide
with 5 concentric circles. The grid
in the lower-right part of the panel has upper-left coordinates (180, 250) and
is a 4-by-4 square of subfigures each 36 pixels wide with 6 concentric circles.
The overall panel has a green background and is
400 pixels wide and 425 pixels high.
Each of the subfigures is yellow with black circles and squares. You are to write a program that exactly
reproduces this image. You are
required to have two particular static methods that are described below. You will not be using class constants
for this assignment. Instead you
will be exploring the use of value parameters.
This program does not require a lot of lines of
code, but the computations are not simple. You might very well find yourself overwhelmed with the
amount of detail you have to handle all at once. A computer scientist named Brian Kernighan has said that, “Controlling
complexity is the essence of computer programming,” which is why this makes a
good exercise for a young computer scientist. You will find that the techniques of decomposition and
iterative enhancement described in chapter 1 will help you in this situation.
Start with one piece of this problem. In particular, you should write a
static method that draws one square with concentric circles inside of it. That subfigure is repeated throughout
this image, so it makes sense to have a separate method for producing it. Start with the subfigure in the upper-left
part of the screen. It's not in a
grid pattern, it's just the subfigure itself (one set of concentric circles in
a square).
Your first version of this method could draw the
specific subfigure in the upper-left corner (always drawing it in that position,
always making it 100 pixels wide, always having 10 concentric circles), but
you'll want to generalize this with the use of value parameters. You should be able to call it with
different sizes, different locations and different numbers of concentric circles.
The drawing commands we are using are defined in
terms of integers, which leaves open the possibility that things won't divide
up evenly. You don't have to worry
about this possibility. In
particular, you may assume that the subfigure height will always be a multiple
of the number of concentric circles you are supposed to draw (e.g., 10 circles
in a figure 100 wide, 6 circles in a figure 36 wide, 5 circles in a figure 40
wide, 4 circles in a figure 24 wide).
Once you have completed the static method that
produces one of these subfigures, write another static method that produces a
square grid of these subfigures.
You will call this method three different times to produce the three
grids of the overall image.
Obviously it will have a lot of parameters to be flexible enough to draw
each of these three grids. The key
point is that a single method can be used that produces all three grids. For an example of this, study the same
program in handout #8. You will
see that the second version (Draw2.java) has .three different draw
methods. This is highly
redundant. Look at how the third
version manages to use a single method with parameters to replace these three methods. You are required to have a similar
method for drawing grids that is called three different times rather than
having three different grid-drawing methods.
In grading, we will require these two static
methods: one for a subfigure with just one square and its concentric circles
and one for producing a grid of such figures that is called three different
times to produce the three grids.
We will expect you to use good style (indentation, commenting, good
variable names, etc.). Be sure to
include a brief comment with each method (e.g., explaining what the parameters
represent). Your program should be
stored in a file called Drawing2.java
Remember that to run your files Drawing1.java
and Drawing2.java, you will have to download the file DrawingPanel.java from
the class web page (the class that Stuart wrote) and store it in the same
folder where you will be storing your classes. The DrawingPanel file will be available under the
“assignments” link from the class web page. The standard Java classes (Graphics and Color) are part of
the java.awt package, so you need to include the following line of code at the
beginning of your class file to access them:
import java.awt.*;
Turn in your two program files using electronic
turnin. You do not have to turn in
DrawingPanel.java.