CS 112
Spring 2019

Old version

This is the CS 112 site as it appeared on May 8, 2019.

Lab 2: Arrays, primitives and references

Using folders

We strongly encourage you to create a separate folder for each lab and each problem set, so that you can more easily keep track of your work. For example, you could create a folder called lab2 for your work on this lab, and put all of the files for this lab in that folder. If you are working on a lab machine, you will need to create the folder on the Z: drive, so that it won’t be lost when you log out.

Piazza Pointers

Task 0: Review Strings and Static Methods from Lab 1, (optional)

Task 1: Arrays and references

Your work for this task should go on the piece of paper that we give you. Please show your paper to a staff member before you leave the lab.

  1. Let’s draw what things look like in memory for the each of the following code snippets:

    // snippet A
    int[] a1 = {1, 2, 3, 4};
    int[] a2 = a1;
    
    // snippet B
    int[] b1 = {1, 2, 3, 4};
    int[] b2 = {1, 2, 3, 4};
    
  2. Given the two snippets above, what is the value of the following expression?

    a1 == a2
    
  3. Given the two snippets above, what is the value of the following expression?

    b1 == b2
    
  4. Consider the following static method:

    public static void mystery(int[] vals) {
        for (int i = 0; i < vals.length; i++) {
            vals[i] *= -1;
        }
    
        int[] vals2 = new int[vals.length];
        for (int i = 0; i < vals.length; i++) {
            vals2[i] = vals[i];
        }
        vals = vals2;
    
        for (int i = 0; i < vals.length; i++) {
            vals[i] += 2;
        }
    }
    

    What does the array a1 look like after the execution of the following code snippet, which you many assume is part of the main method of a valid Java class?

    int[] a1 = {1, 2, 3, 4};
    mystery(a1);
    

    Draw pictures to show what things look like in memory. You may find it helpful to use the following template, which shows separate method frames for main() and mystery():

    +--------------+
    | mystery      |
    | -------      |
    |       +----+ |
    | vals2 |    | |
    |       +----+ |
    |       +----+ |
    |  vals |    | |
    |       +----+ |
    +--------------+
    | main         |
    | ----         |
    |       +----+ |
    |    a1 |    | |
    |       +----+ |
    +--------------+
    

Task 2: Array methods

Open Eclipse, and implement the following array-processing methods in a class named ArrayPractice.

  1. Write a main method that you will use to test all your methods. But first, try this:

    Example:

    > int[] a = {2, 4, 6, 8};
    > System.out.println( a );
    
    // What do you expect to see?
    
    > System.out.println( Arrays.toString(a) );
    
    // Now, what do you expect to see?
    
  2. Write a static method named square that takes an array of integers and squares each element of the array.

    This method does not need to return anything. (If you’re not sure why, please ask!)

    Example:

    > int[] a1 = {1, 2, 3, 4};
    > Lab2Task2.square(a1)
    > System.out.println( Arrays.toString(a1) );
    { 1, 4, 9, 16 }
    
  3. Write a static method named shiftLeft that takes an array of integers and shifts each element one position to the left. The last element of the array should end up being 0, and the original first element should be returned by the method.

    For example:

    > int[] a2 = {1, 2, 3, 4, 5, 6};
    > Lab2Task2.shiftLeft(a2)
    > System.out.println( Arrays.toString(a2) );
    { 2, 3, 4, 5, 6, 0 }
    

    Hint: To determine the necessary logic, you may find it helpful to begin by writing down examples of specific assignments statements that the method will need to perform as part of the shifting. Then look for the general pattern in these assignments, and use it to write the necessary loop.

    Other hints:

    • You will need to store the original first element in a variable before you do the shifting, so that you can return the first element at the end of the method.

    • Don’t forget to put a 0 in the last position of the array.

Extra Practice!

If you get through the exercises above, congratulations!

For extra practice, you can try some exercises from an excellent site called Practice-It.

Task 4: Submit your work

You should show the paper with your work on Task 1 to the teaching assistant.

Don’t worry if you didn’t finish all of the tasks. You should just submit whatever work you were able to complete during lab.