|
|
Laboratory 10
Arrays
Week of Wednesday, 9 November, 2005
|
Objective
A
programmer often needs to represent a group of values or objects
together in a list. The basic Java list mechanism is the array. When
you use a list, you often need to be able to find values in the list.
Therefore, we review several search algorithms.
Key
Concepts
- One-dimensional arrays
- Subscripting
Getting Started
Using the procedures in the previous laboratories, copy the
files Sum.java, ArrayFun.java,
Descriptions.java, and
DescriptionGenerator.java for your manipulation
(right-click and select "save as...").
Basics
An
array in Java is a list of elements. The list is an object. The
individual elements can be accessed through subscripting using the
bracket operator [].
If a list is composed of n values, then the first element has
a subscript value of 0, the second element has a subscript value of
1, and so on with the last element having a subscript value of n-1.
To
assist programmers, an array has a public data field named length
that specifies the number of elements in the list.
Another
form of array assistance is the automatic detection by Java of
illegal subscripting – subscripts that are too big or too
little. If a subscript is determined to be invalid, an exception of
type IndexOutOfBoundsException is generated. As it is with other
unhandled exceptions, if IndexOutOfBoundsException occurs and is not
handled explicitly by the program, then the program terminates.
The
IndexOutOfBoundsException prevents your program from inappropriately
accessing memory outside its allocated space. (Attempting illegal
memory accesses and assignments is a common exploitation effort of
virus writers.)
- Open the program file Sum.java. This program initializes two
arrays a and b and produces a third array
c whose values are the sums
of the values of a and b.
- Compile and run the program to see whether its behavior
matches your expectations.
- Correct the errors in the program.
- Recompile and re-execute the program. Check the sums to ensure
that the program ran correctly.
-
Submit
the completed program Sum.java.
When
writing programs in the future pay attention to the fact that when
using a for loop in Java to iterate through an array, the body of
loop should typically only iterate when loop indices are in the range
0 to n-1, where n is the size of the array. This design
accommodates the fact that Java numbers array indices from 0 to n-1.
Hence, the typical for loop initializes its loop control variable to
0 and repeats the loop as long as the index is less than the size of
the list.
Array manipulations
Open program file ArrayFun.java. This program first determines
from its user the size n of a list that is to be extracted.
The program then extracts n values from the standard input
stream and uses them to set the values of the elements of array
number[]. The program then determines and displays the average
of the values in the array.
- Modify the program in the following manner
- Add a new loop that determines the number of values in the
list that are equal to the average.
- Add a System.out.println()
statement that displays the number of values in the list that are
equal to the average.
- As a simple test case, run the program on a list of size four
whose values are 2, 1, 2, and 3. Also run the program on a list of
size three whose values are 4, 0, and 2.
-
Submit
the completed program ArrayFun.java.
- Modify the program to determine and display the minimum in the
list. Assign this value to an int
variable named min.
The standard approach to such a task is to initialize min
with the value of the first element in the list (i.e., number[0]).
A loop then considers the other elements in the list. Each of these
elements is tested in turn to determine whether it is less than min.
If an element is found to be less than min, the value of min is
updated. After the loop completes, the minimum value is definitely
stored in min.
-
Submit
the completed program ArrayFun.java. Do not worry about overwriting the previous
submission.
- Modify the program to display each element in the list. If the element
is equal to the average then, the text "average" should be
displayed after the value. If the element is equal to the minimum
then, the text "minimum" should be displayed after the value. Although
it it is pathological, it is possible for a value to be both the
minimum and average.
-
Submit
the completed program ArrayFun.java. Do not worry about overwriting the previous
submission.
Descriptions
For the last part of this lab, we will finish a Descriptions class, which
will be used in our final game. The class will allow us to generate a
random adjective to describe a room ("dark", "haunted", etc.) and a random room
type ("living room", "parlor", etc.), as well as random weapon names ("katana",
"sai", etc.) and monster names ("goblin", "troll", etc.).
- Examine the code in Descriptions.java.
There are two arrays: the first is the list of adjectives, and the second is
the list of room types. The third instance variable is a
Random object, used to create random numbers. The default
constructor is there, but does not need to do anything. The other two
methods (getNextAdjective() and getNextRoomType())
require completion for this part of the lab.
- Examine the DescriptionGenerator.java file. This file contains just a
main() method, which uses the DescriptionGenerator class. Do you
understand how this works? If not, ask a TA.
- Add a few values to each of the arrays in Descriptions.java (minimum
of 5 each).
- Next, complete the getNextRoomAdjective() method. It
currently returns null, but should return one of the
adjectives in the roomAdjectives array, chosen at random. Thus, you
need to generate an appropriate random number (via the
nextInt() method in the
Random class), then return that element in the roomAdjectives array.
- Similarly, the getNextRoomType() method should do the
same for the roomTypes array. Note that the two arrays may be of
different sizes, so be sure not to mix them up in your code.
- Similarly for the getNextMonsterName() and
getNextWeaponName() methods.
- The Descriptions class itself can't be run, but the DescriptionGenerator
class can be (as only the latter has a main() method).
Every time you run the DescriptionGenerator, it will print out twenty random rooms,
each with a random description, a random monster, and a random weapon.
-
Submit the Descriptions.java file.
Finishing Up
-
Submit Sum.java, ArrayFun.java, and Descriptions.java. As
DescriptionsGenerator.java was not modified, it does not need to be submitted.
- Copy any files you wish to keep to your home directory.
- Delete all of the files you copied or created on the
laboratory machine.
Homework J7
Use any remaining time in the lab to work on HW J7.