CS 139 Lab:Array_Play
Objectives:
|
- Create an array
- Initialize the array
elements to an appropriate value
- Use an array to track
a sequence of values
- Display the contents
of the array
|
Background:
|
Arrays are a collection
of values (or objects) that are referenced by the collection name and a
subscript. Arrays provide flexibility in processing groups of like
elements that are supposed to receive equivalent treatment. |
New Terms:
|
- array
- a homogeneous
collection of values
- array.length
- an attribute of the array that is the number of elements in the array
- subscript
- used to specify the
particular element of an array
- initializer list
- a list of values used to instantiate and initialize an array to the values of the items in the list
- ArrayIndexOutOfBounds
Exception
- an exception that is
raised when a program tries to access an element that is either
negative or larger than the number of elements in the array.
|
Materials:
|
You will write this
program from scratch. You may download Die.class
to use in part 2. Documentation for this class is found here. |
Acknowledgment |
Lab adapted from a lab
by Nancy Harris |
1 General Instructions:
Getting Started
Create a working directory for this lab on your n-drive. Download a
copy
of Die.class
to that directory.
Create and edit a new java
file, ArrayPlay.java. (If you have a template feel free to use it.)
ArrayPlay
will contain a
single main method. For each part, you will add
code to the
program, not replace prior code. When you are done, this
program will provide you with a reference for working with arrays
including initializing, printing, and manipulating.
2 Part 1 - Sequentially
accessing an array
NOTE:
Wherever you are asked to do something to your array and
then print it, you should finish the update step and have a separate
loop to do the printing.
- In the main method, create a
single array of integers that is 6 elements long. (Do you want to use a
constant, literal, or variable for the length? Hint, look at later steps.)
- Initialize each of the array
elements to the value, -1. (use an appropriate loop to do so).
- In another loop, print each
element of the array on a separate line with an appropriate heading
before the output.
- TEST
IT!
- Add in code to change the
value of each element of the array to its subscript. (Use a loop). For
example, myArray[3] should hold the value 3.
- In another loop, print each
element of the array in step 5 on a separate line (with an appropriate heading
before the group.)
- If you have not seen an
ArrayIndexOutOfBoundsException, make one. Change your loop so that it
reads past the end of the array. Run it. What happens?
- Add code to reinitialize
your array to all zeros.
3 Part 2 - Randomly accessing
an array
- Using the downloaded Die.class,
create a new Die
object. (This is an instantiation. It is like instantiating a Toolkit object in your PAs.)
- Create a loop that will
iterate at least 100 times. In the loop body:
- Roll the die.
- Take the result of the
roll and map it to the location in the array. (The die values will go 1
- 6; your array subscripts are 0 - 5. Think about how you will map the die value to the array).
- The value of the array
element is a count of the number of times that roll is made. So if 1
comes up three times, array[0] should have the value 3.
- After the loop finishes,
print the results of the rolls as they correspond to die faces. In
other words, how many times was a 1 rolled?, 2? etc.
For example, if the
array holds 20, 17, 19, 15, 12, and 17 in positions 0 - 5, you would
output
1
was rolled 20 times.
2 was rolled 17
times.
... etc.
4 Part 3 - Working with two
arrays
- You will need two double
array objects. Declare these and then....
- Use an intializer list
to intialize one of the arrays to 10 double
values (of your choice). See the book regarding initializer
lists.
- Instantiate the other
array to 10
elements, but do not initialize the contents.
- Print the contents of
both arrays. Print them side by side on your screen. Label this output,
Before.
- Copy the contents of the
first array to the second array and again print their contents side by
side. Label this output After.
- Finally, change the
contents of one element of the first array and a different element of
the second array. (assign a different number than before).
- Again, print the
contents of the two arrays side by side. Label this output After
Change.
- Check that the contents
of the two arrays are indeed different. If not, try to figure out why
they are not and make the appropriate corrections.
5
Turning in your work
Either show your work to the lab instructor or upload the completed
code to Blackboard.
6 Thought Questions - These
do
not need to be turned in.
- Is it always necessary to
initialize an array? Why or why not?
- An array is a reference
type. What will it mean when you try to pass an array to a method or a
function? What if you built an initialization function? What would you
pass and what would you return? Is it even necessary to return a value?
- Is it possible to process
an
array from back to front instead of from front to back (length - 1 to 0
rather than the usual 0 to length - 1)? Try it.
7 OPTIONAL - An array of objects
- Create a String array of length 6. Use an initializer list to initialize the list to 6 random String values.
- Use a loop to display the length of each of the items in the array and the String value itself one line per array element. (If you have trouble, think about how you would print the data if there were 6 different variables. A subscripted variable can be used just like a regular variable).
- Use the "enhanced for loop" described on page 464 in Gaddis, to do step 16 (use a separate loop so you have both variants.)
- Finally write a small method that takes in the array that you have built in step 15 and returns the longest String in the array. (Think about how you will keep track of that and how you will return it). In the main method, write a call to that method and display the result with an appropriate label.