Java

TOPIC 44 – ARRAYS 1

 

LESSON WORK

 

 

GROUP WORK

 

QUESTION 1

a)    Write the statement needed to create an integer array called arr1 that contains 6 elements.  Using 6 different assignment statements, set the value all elements to 1.

b)    Write the statement needed to create an integer array called arr2 that contains 6 elements.  Using a for loop, initialize each element to 1.

c)     Using a single-step statement, create an integer array called arr3 that contains 6 elements.  All 6 elements should be initialized to 1.

QUESTION 2

a)    Using 6 different statements, output the content of arr1 to screen.

b)    Using a for loop, output the content of arr2 to screen.

c)     Explain why the for loop would be an even better approach if the array was larger.

 

QUESTION 3


The following steps should all be placed inside a single program:

a)    Create an integer array of size 20.

b)    Using a loop, give each element a random value between 1 and 100.

c)     Using another loop, calculate the sum of all elements in the array.

 

 

INDIVIDUAL WORK

 

QUESTION #1 (SELF-CHECK)

 

Fill in the blanks.

 

a)    The first element in an array has index number __________.

b)    The last element in an array containing 35 elements has index number __________.

c)     An array of length 12 will have elements with indexes ranging from _________ to _________.

d)    True or false?  If attempting to access the element with index 20 in an array of length 20, you will get an out of bounds error.  __________

e)    The __________ of an array or the ____________ of an array refers to the number of elements in the array.

f)      True or false?  An array in Java can be resized.  __________

g)    True or false?  An array in Java can be given a size at run-time.  __________

h)    For an array called data, we can check the number of elements in it by using __________.

i)       The line int x; declares an integer variable named x.  What line of the code declares an integer array name numbers?  _______________

j)       If grades is a double array, then we can get a String representation for that array by using ______________.

SOLUTION

 

Check your answers here.

 

 

QUESTION #2 (SELF-CHECK)

Try doing as many of these quick programs as you can.

 

A)    Use a one-line statement to create an array named primes that gets all prime numbers under 20.  Then, again using a single line, output the array to screen.

 

B)    Create an integer array of length 5.  Name your array faves. Use five assignment statements to store your five favourite numbers into the array.  Finally, output the content of your array to screen.

 

C)   Create a boolean array named flags of size 40. Use a for loop to set all even elements to true and all odd elements to false. Then output the contents of the array to screen.

 

D)   Create an int[] of size 20. Use a for loop to initialize each element j to the value j * 2. Output the array to screen.

E)    Create an integer array of size 10.  Elements 0 and 1 both get a value of 1.  All other elements get the sum of the two previous elements in the array. 

For example, element 2 will get 2 because 1 + 1 is 2.

 

Your code should make use of a for loop.  You should output your array to screen.

F)    Consider the following statement that creates an array:

double[] results = {9.6, 9.5, 9.9, 10.0, 9.4};

Write the for loop required to output the value of each element one at a time on the screen.

 


SOLUTION

 

Check your answers here.


QUESTION #3

In this program, you must ask the user for the amount of data that will be stored in an array.  So, the size of the array is determined at run-time.

 

Create a program that asks the user how many vegetables they like.  Then, create a String array named veggies that will store that number of vegetables.  Then, use a for loop to ask the user to enter the name of each vegetable they like and store it in the array.  Output the content of the array to screen.

 

PSEUDOCODE

Ask the user for the number of veggies they like.

Store answer in variable named vegCount.

 

Create a String array of size vegCount.

 

Loop over each element in the array.

   Ask user for a veggie they like.
   Store result in array

 

Output array to screen

 

SAMPLE OUTPUT

How many veggies do you like?

3

Enter a veggie that you like.

Carrot

Enter a veggie that you like.

Celery

Enter a veggie that you like.

Cucumber

 

[Carrot, Celery, Cucumber]



QUESTION #4

Consider the five following arrays:

 

String[] names = {"Rachel", "Joe", "Moni", "Ross", "Chandler", "Phoebe"};

int[] c1 = {78, 62, 85, 94, 82, 90};

int[] c2 = {82, 67, 84, 90, 74, 52};

int[] c3 = {79, 64, 86, 98, 83, 94};

int[] c4 = {92, 82, 85, 74, 80, 62}; 

 

The first array contains student names.  The array c1 contains student marks in course #1.  The array c2 contains student marks in course #2.  And so on…  The marks have been ordered in the same way as the student names.  So, Rachel’s marks are 78, 82, 79 and 92 and Chandler’s marks are 82, 74, 83 and 80.

 

Write the code required to output the name of each student along with their average.  Your code should also meet the following criteria:

 

  • It should work for any number of students.  So if we add another student or we drop a student, your code should still work.  This requires you to use a for loop.  You can assume that all five arrays will always be the same length.

 

SAMPLE OUTPUT

 

Rachel (82.75)

Joe (68.75)

Moni (85.0)

Ross (89.0)

Chandler (79.75)

Phoebe (74.5)

 

POSSIBLE ADDITION

 

If you have time, can you alter the code to allow for students that have a spare period.  In such a case, the mark entered in the array would be -1.  Of course, the average has to only consider the other three classes.