COMP 110
Lab 2
20 points
Assigned: Wednesday, Oct 14th
DUE: Wednesday, Oct 21st at 11:55
PM
Background
The purpose of this lab is to give you practice with arrays and
loops. We will learn how to create an array of variable length. In addition we
will also learn how to do computations involving an array.
Description
o
Ask a user to enter an integer that is a multiple of three.
o
Get the input from the user using a Scanner
o
Check if the number is divisible by three.
o
If this is the case, save the number into the array.
o
If not, do not save the number and do
notify user that the number is not multiple of three.
o
If the user entered 1, then compute the sum of all entries in the
array. Create an integer variable named sum and then save the result into
the variable. Print the result. Make sure to include the word sum in
your message.
o
If the user entered 2, then compute the average of all entries in
the array. Create a double variable named average and then save the result
into the variable. Print the result. Make sure to include the word average
in your message.
o
If the user entered 3, then compute both the sum and the average
of all elements in the array. Create two aforementioned variables. Print both
results. Make sure to include the words sum and average in
your message.
o
If the user entered anything else, print the message Unrecognized
Option.
Sample Output
Welcome!
Please enter the size of array you
want to create.
4
Please enter an integer that is
multiple of 3.
9
Please enter an integer that is
multiple of 3.
6
Please enter an integer that is
multiple of 3.
21
Please enter an integer that is
multiple of 3.
99
What kind of operation would you like
to perform on the array?
1: Compute the sum only
2: Compute the average only
3: Compute both sum and average
3
The sum is 135
The average is 33.75
Note:
o
You are not allowed to any method that makes the assignment
trivial. For instance, you should not use any method that computes the average
for you.
o
You do not need to worry about forcing the output average to be
always 2 decimal points.
Grading
How to submit the lab