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

1. Create a Java Project in Eclipse named Lab2.
2. Create a class in your project named Lab2. This should also create the file Lab2.java.
3. Print welcome message. Make sure to include the word welcome.
4. Ask the user to enter the size of array he/she wants. Make sure to include the word size.
5. Declare and create an array of integers of that size entered in the previous step. Name this array multiplesOfThree.
6. Use a loop to do the following:

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.

1. Once the user has filled up all the entries of the array with elements that are multiples of three, ask the user to what kind of operation he/she wants to perform on the array.
2. Using scanner to get the response from the user. There are four possibilities:

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.

• 1 point Your program contains an appropriate header
• 1 point Your program prints a welcome message
• 2 point Your program contains and uses correctly named variables
• 3 point Your program correctly uses loop to fill up the array
• 5 point Your program can correctly fill up the array with the numbers that are multiple of 3
• 2 point Your program notify a user if he/she entered a number that is not a multiple of 3
• 2 point Your program can correctly compute sum of all elements in the array
• 2 point Your program can correctly compute average of all elements in the array
• 1 point Your program can perform both computation for option 3
• 1 point Your program can handle unrecognized option

How to submit the lab

• A file named youronyen_lab2.zip, where youronyen is your onyen. The .zip file should include your whole project from Eclipse.  Your java file should have the appropriate header. Followthese instructions to create youronyen_lab2.zip
• Submit (Send File) youronyen_lab2.zip on Sakai by 23:55pm on the due date.
• If you do not follow these instructions you will not get credit for this assignment. Please let us know if you are having any problems with the assignment submission.