CPSC 170 Lab 0: Arrays
Open an xterm.
In your home directory, create a subdirectory called cpsc170. Go (cd)
to cpsc170 and create another subdirectory called labs. Go (cd) to
labs and create another subdirectory called lab0. Do your work for this
lab in the lab0 directory.
Now open Mozilla and go to the
CPSC170 home page at http://cs.roanoke.edu/CPSC170A. Bookmark this page, then
follow the link for lab0 to open this document
in the browser.
File Sales.java contains a Java program that prompts
for and reads in the sales for each of 5 salespeople in a company. It then
prints out the id and amount of sales for each salesperson and the total sales.
Study the code, then compile and run the program to see how it works. Now
modify the program as follows:
- Compute and print the average sale. You can compute this directly from the
total; no loop is necessary.
- The salespeople are objecting to having an id of 0 -- no one wants
that designation. Modify your program so that the ids run
from 1-5 instead of 0-4. Do not modify the array -- just
make the information for salesperson 1 reside in array location 0, and so on.
- Find and print the amount of the maximum sale. Remember that
when finding a max value, you need to initialize the variable holding
the max to something, and choosing this value can be tricky. But when finding
the max of the values in an array, a nice solution is
to initialize the variable holding the max to the first element in the
array. Convince yourself that this is guaranteed to work.
Note that you don't need another loop to find the max;
you can do it in the same
loop where the values are printed. (You could do it in the same loop
where the values were read, but then it would be harder to use the
for initializing the max variable.) You will need a new variable to hold the
- In addition to the amount of the maximum sale, print the id of the
salesperson who has the maximum sale. You'll need another variable to
hold the id, of course... think how it should be initialized.
- Do the same for the minimum sale - find and print both the salesperson id
and the amount.
- After the list, sum, average, max and min have been printed, ask the
enter a value. Then print the id of each salesperson who exceeded that
amount, and the amount of their sales. Also print the total
number of salespeople whose sales exceeded the value entered.
- Instead of always reading in 5 sales amounts, at the beginning
ask the user for
the number of sales people and then create an array that is just the right
size. The program can then proceed as before.
Print this program to turn in.
Quiz.java contains the skeleton for a program to
grade a 5-question multiple-choice quiz. It assumes that the user already
has the questions (presumably the quiz itself has been distributed
separately); the user simply enters the letters of their answers
(a,b,c,d, or e) in response to the program's prompts, and the program
then tells the user's score.
Note that array key has been initialized with the answers to the quiz
using an initializer list (see p. 329-330 of the text).
These answers will not change -- you will just use them to check the answers
the user enters.
Fill in code as indicated by the comments to make the program behave as
described above. When it is complete, print the program to turn in.
Drawing More Circles
File DrawCircles.java contains an
applet that creates and draws 10 circles. It uses
class Circle.java and is called from
DrawCircles.html. It should look
familiar -- you wrote a similar program
in postlab 10 in CPSC 120. Save all of the
files above to your directory. Use the appletviewer and DrawCircles.html to
run DrawCircles.java. (Don't worry if you see more than 10 circles when the
applet starts up -- this will go away when the applet is redrawn.)
Note that when the applet is redrawn (e.g., if you pop the window or choose
restart or reload from the menu),
a different set of
circles is drawn. This is because the circles are created (randomly)
which is called every time the applet is drawn.
The applet can't draw the same circles as before because it doesn't store
them. Open DrawCircles.java, study it, and be sure you understand why this
Now you will modify DrawCircles.java so that it creates 10 circles and
stores them in an array before paint is called; paint then
simply draws each circle in the array.
(You do not need to modify Circle.java.)
Proceed as follows:
When you run your program now, you should see the same set of circles when
you redraw (e.g., pop the window), but a new set if you choose Restart or Reload from the
applet menu (this causes init to be executed again).
- Declare and initialize an array to hold ten Circle objects. This should
be an instance variable -- it should not be local to paint.
- Add an init method (public void init()).
Remember that init is called only once, when the applet is loaded,
which is what you want --
this way you only create one set of circles.
- In init, write a loop that fills the array with Circle objects.
(Since the array is an instance variable, it is visible in both init
Each time through the loop you will need to create a new circle
and store it in the next slot in the array.
- Modify paint to run through the array and draw each circle.
No new circles should be created in paint!!
Print this program to turn in.
- Printouts of Sales.java, Quiz.java, and DrawCircles.java.
- Tar the files in your lab0 directory and email the .tgz file
to me (firstname.lastname@example.org). Put cpsc170 lab0 in the Subject line.