Lab 7: Arrays
Download and unzip lab7.zip, which contains all
the files you will need for this assignment. Open the
file Lab7.java in DrJava. All the code you write for this
assignment should be added to this file.
Be sure to test your code after completing each exercise.
countEven
Complete the countEven method that asks the user for 10 integers
that are stored in an array
and then
displays the number of even integers that were input. (Do you
really need an array here?)
countAboveAverage
Complete the countAboveAverage
method that asks the user for 10 integers
that are stored in an array
and then
displays the
number of integers that were above their average.
(Do you need an array here?)
NOTE: You might want to print out the average also to check your work.
plotFunction
Complete the plotFunction method so that it opens
a window of size 500 X 500 and plots the function
f(x) = x2/200 - x + 200. To draw the function, create an array
of int of
size 500. For each index x of the array, compute f(x) and store this in
the array at index x.
Then draw a line for each pair of values in the array.
You should get a picture that looks something like this:
BE CAREFUL: The y-axis is flipped in the window. We want the origin to
be in the lower left but Java puts it at the upper left.
Challenge: colorgram
Implement the method colorgram, which reads in 8 positive integers
from a data file and computes the total of the data. Then create a window
with a width equal to that total and a height of 200.
Create a "colorgram" where
each bar's width is given by each of the 8 integers.
Draw each bar using a different color. (Can you do this using a loop
rather than using 8 separate statements?)
For example, if the array has the following integers:
5, 10, 20, 40, 30, 50, 5, 40
then we might see something like this:
NOTE: You will need to
create a data file with 8 positive integers, one per line, and
store this file in the Lab7 folder.
COLOR HINT: Each color above (including the white bar) consist
of red, green and blue components that are either 0 or 255. For example,
the image above was drawn with the following colors in the order given:
Black 0 0 0
Blue 0 0 255
Green 0 255 0
Cyan 0 255 255
Red 255 0 0
Magenta 255 0 255
Yellow 255 255 0
White 255 255 255