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
-
You can — and should! — search for related questions before you post a new question.
-
You can post anonymously, so that other students won’t see your name.
-
If you need to show us part of your work in order to ask your question, you can make a private post that only the course staff can see.
However, if your question that doesn’t reveal your work, please don’t post it privately, because then other students can’t benefit from it. Non-private questions are also more likely to get a quick reply, because more people can see and answer them.
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.
-
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};
-
Given the two snippets above, what is the value of the following expression?
a1 == a2
-
Given the two snippets above, what is the value of the following expression?
b1 == b2
-
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 themain
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()
andmystery()
:+--------------+ | mystery | | ------- | | +----+ | | vals2 | | | | +----+ | | +----+ | | vals | | | | +----+ | +--------------+ | main | | ---- | | +----+ | | a1 | | | | +----+ | +--------------+
Task 2: Array methods
Open Eclipse, and implement the following array-processing methods in
a class named ArrayPractice
.
-
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?
-
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 }
-
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.
-
Go to Practice-It.
-
Create an account if needed.
-
Select the group of problems entitled Building Java Programs, 3rd edition.
-
Try problems from:
- BJP3 Chapter 7: Arrays
The system will test your solution and tell you whether it is correct.
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.