Lab: Arrays and References
Objective
In this lab you will test your understanding of references and
arrays by implementing some static methods involving arrays of NaturalNumbers.
Setup
Follow these steps to set up a project for this lab.
- Create a new Eclipse project by copying ProjectTemplate.
Name the new project ArraysAndReferences.
- Open the src folder of this project and then open
(default package). As a starting point you can use any of
the Java files. Rename it ArraysAndReferences and delete
the other files from the project.
- Follow the link to ArraysAndReferences.java,
select all the code on that page and copy it to the clipboard;
then open the ArraysAndReferences.java file and paste the
code to replace the file contents. Save your file.
Method
- Complete the body of the productOfArrayElements
static method that computes and returns the product of the
elements of the given array. The contract is provided with the
method header.
- Run the program and test your implementation of productOfArrayElements.
- What should be the correct output?
- How do you know what the correct output should be?
One of the problems with the current test code is that it uses a
big input (an array of 42 elements) for which it is difficult to
even know what the correct output should be.
- Modify the statement that creates the array so that the
array has 1 element (instead of 42). What is the expected
output? Run the program again. Does it produce the correct
output?
- Now try with an array of size 2. What is the expected
output? Run the program again. Does it produce the correct
output?
- Carefully trace over the following initialization code
(where we replaced 42 with a more manageable 5). Draw a picture
(of the kind used in Arrays
and References, slide #5, noting that the elements of the array
here are of the reference type NaturalNumber, not the
primitive type int) of the value of
the array after the initialization code completes.
/*
* Initialize an array of NaturalNumbers with values 1 through 5.
*/
NaturalNumber[] array = new NaturalNumber[5];
NaturalNumber count = new NaturalNumber2(1);
for (int i = 0; i < array.length; i++) {
array[i] = count;
count.increment();
}
- Fix the initialization code so that it has the correct
behavior, i.e., it actually does what is described in the comment,
and draw another picture showing the new value of the array.
- Run the modified program and test whether your
implementation of productOfArrayElements is correct. If
you still observe some problem with the output produced, you need
to figure out whether the problem is in the new initialization
code or in the implementation of productOfArrayElements.
Debug your code until you are satisfied that they both behave as
expected.
- Complete the body of the computePartialProducts
static method that is given an array of NaturalNumbers
and replaces each element of the array with the partial product of
all the elements in the given array up to and including the
current one (so the first element is the "product" of the first
element of the incoming array, i.e., it does not change; the
second element is the product of the first two elements of the
incoming array; the third element is the product of the first
three elements of the incoming array; etc.). The contract is
provided with the method header.
- Modify the main program and run it to test your
implementation of computePartialProducts. To output an
array you can write your own loop or use the Arrays.toString()
method from the java.util package.
Additional Activities
- Complete the body of the partialProducts static
method that is given an array of NaturalNumbers and
creates and returns a new array of NaturalNumbers, of the
same size of the given array, where each element is the partial
product of all the elements in the given array up to and including
the current one. In other words, this method does essentially the
same thing as computePartialProducts except that it does
not change the incoming array and instead it returns the partial
products in a new array. The contract is provided with the method
header.
- Modify the main program and run it to test your
implementation of partialProducts.