CSci 1211 Problem set 1. Due Friday, Jan. 23rd at 5pm
Resources: Java
drawRect
and fillRect
methods, Java
Color
class, and Java Random
class.
This is the first problem set. We are still taking "baby
steps" in Java, so our first programs are "baby
programs".
Problem 1 (20 points): draw a house in a Java applet
In this program you will draw a simple picture of a house in a Java
applet. Download the following House.html
file and the House.class file to see how the
program should work.
To run the demo, save these two files in the same
folder, start the command prompt, type
cd C:\myfolder
(replace myfolder
by your folder name), and then type
appletviewer House.html
House.class contains the
bytecode, so you don't need to compile anything.
Note: If your browser has the right plug-in, then the applet
will start automatically when you click on House.html, you don't need
to save the demo.
What you need to do:
- Create a new folder for your problem set (say,
pset1
).
- Copy/paste the code from this file
into a new file
House.java
(start jEdit, open a new file,
copy/paste the code, and save the file into the folder
pset1
). Also, save the file House.html
into the same folder.
- Add the code to draw the house (using
Color.lightGray
, the windows (using
Color.yellow
), and the door (using
Color.green
).
- The size of the building is 200 by 250
pixels, the left upper corner is at (50, 100).
- The windows are 50 by
60 pixels, they are 25 pixels away from the sides.
- The door is 100 pixels tall.
You can figure out the rest of the parameters based on
these numbers. The picture must look exactly like the one in the demo.
Problem 2 (20 points): adding random numbers
This program is a Java application. You should start by copy/pasting
code from the file Add.java.
This program generates and solves a very simple arithmetic problem: if
Jack picked n apples and Jill picked m, how many apples did they pick
total? Each of them could pick any number of apples less than 10.
The output of the program should look something like this:
Jack picked 9 apples
Jill picked 2 apples
Together they picked 11 apples
The program should give you different results every time you run
it. The program must have three different integer variables:
- The variable to store the number of apples picked by Jack (already
declared in the given part of the program, called
jack
),
- The variable to store the number of apples picked by Jill,
- The variable to store the total number of apples they picked.
Problem 3 (extra credit, 10 points): random rectangles.
Use file RandomRectangles.java and
the source
of RandomRectangles.html to create
a picture that displays three random rectangles of different
colors. Clicking on RandomRectangles.html shows you the
demo if your browser is set up right (refreshing the page generates
a new picture). Otherwise download RandomRectangles.class.
This page is a part of the course CSci
1211 at UMM.