Lesson 17 – Arrays

Big Java 6

When you come in

  1. Connect to the Internet
  2. Log in to Piazza
  3. Start Bluej
  4. Navigate to laughton.com/obrien/sjsu/cs46a/CRT_lessons/ and open this lesson

Questions

Concept - Declaring an array

 

Concept - Accessing array elements

Activity - Arrays

Record your participation in Piazza clicker question Lesson17 Q1

Assume the array primes has been initialized like this
int[] primes = { 2, 3, 5, 7, 11 };
What does it contain after executing the following loop?
for (int i = 0; i < primes.length; i++)
{
   primes[i]++;
}

Note: primes[i] accesess the ith element of the primes array and primes[i]++  increments the ith element of the array

  1. 2, 3, 5, 7, 11
  2. 3, 4, 6, 8, 12
  3. 7, 8, 10, 12, 16
  4. Nothing. There is a syntax error

Concept: Enhanced for loop

Activity - Enhanced for loop

Record your participation in Piazza clicker question Lesson17 Q2

Assume the array primes has been initialized like this
int[] primes = { 2, 3, 5, 7, 11 };
What does it contain after executing the following loop?
for (int p: primes)
{
     p++;
}
  1. 2, 3, 5, 7, 11
  2. 3, 4, 6, 8, 12
  3. 7, 8, 10, 12, 16
  4. Nothing. There is a syntax error

Activity - Creating and filling an array

Record your participation in Piazza clicker question Lesson17 Q3

Complete the method body for the getCombination method in the class Lottery. It returns an array of n random numbers less than 100 that might then be used in buying a lottery ticket. Create a Bluej project and copy in this code. You will need to instantiate a Random object. Test the method in the workbench
import java.util.Random;
public class Lottery
{
   public int[] getCombination(int n) 
   { 
       //create random object
       //create an array that can hold n ints
       //for every cell in the array, fill it with a random number
   }
   //possibly other methods
}
  1. Got it
  2. Got it, but it was hard
  3. Tried but did not get it in the time available
  4. Did not know where to start

Activity - Array Algorithms: finding the max

Record your participation in Piazza clicker question Lesson17 Q4

Here is some starter code (arrays.zip). It contains a class IntArrayProcessor and an IntArrayProcessorTester. You can download it, unzip it, and open the Bluej project. We are going to practice working with array algorithms

Activity - Array Algorithms: counting matches

Record your participation in Piazza clicker question Lesson17 Q5

Activity - Array Algorithms: Separating elements

Record your participation in Piazza clicker question Lesson17 Q6

Activity - Swapping Elements

Record your participation in Piazza clicker question Lesson17 Q7

Activity - Partially filled Arrays

Record your participation in Piazza clicker question Lesson17 Q8

Here is a starter project for working with partially filled arrays