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.

Grading

How to submit the lab