CPSC 170 Lab 0: Arrays

Getting Started

Open an xterminal. 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 up Netscape 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.

Open emacs.

Sales Reports

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:
  1. Compute and print the average sale. (You can compute this directly from the total; no loop is necessary.)
  2. Find and print the maximum sale. Print both the id of the salesperson with the max sale and the amount of the sale. Note that you don't need another loop for this; you can do it in the same loop where the values are read and the sum is computed.
  3. Do the same for the minimum sale.
  4. After the list, sum, average, max and min have been printed, ask the user to enter a value. Then print how many of the salespeople had higher sales than the number entered.
  5. There has been a 10% price increase! This is within the "inflationary increase" allow by sales contracts, so all sales are still valid. After everything above has been done, modify the sales array to reflect the 10% increase (make each sale 10% higher than before) and print the new values.
  6. 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 grading

Discouraged with the poor math skills you see in the general population, you have decided to construct a short quiz so people can test their own knowledge. You have distributed the quiz on paper, but people will enter their answers online, where they will immediately find out how well they did. File Quiz.java contains the skeleton for this program. Note that array key has been initialized with the answers to the quiz using an initializer list (see p. 277 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 to create and draw 10 circles from one of last year's labs. It uses class Circle.java and is called from DrawCircles.html. Save all of these files to your directory. Use the appletviewer and DrawCircles.html to run DrawCircles.java. Note that when the applet is redrawn (e.g., when the window is popped) a different set of circles is drawn. This is because the circles are created (randomly) in paint, 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.

Modify this applet so that it creates 10 circles and stores them in an array, then simply draws each circle in the array in paint. You will need to do the following:

  1. Declare and initialize an array to hold ten Circle objects. This should be an instance variable -- it should not be local to any method.
  2. 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.
  3. In init, write a loop that fills the array with Circle objects. Each time through the loop you will need to create a new circle and store it in the next slot in the array.
  4. Modify paint to run through the array and draw each circle. No new circles should be created in paint!!
When you run your program now, you should see the same set of circles when you redraw, but a new set if you choose Restart or Reload from the applet menu.

Print this program to turn in.

HAND IN: