Espresso: A Concentrated Introduction to Java

# Laboratory: Arrays

Summary: In this laboratory, you will begin working with arrays in Java. The particular focus of this lab is working with arrays of numbers todescribe grades.

Contents

## Exercises

### Exercise 0: Preparation

Create a new package for this laboratory named `username.grading` in project Code.

a. Create a utility class, `Grader`, with one static method, `printGrades(PrintWriter pen, String[] names, int[] grades)`. This method should print the following text for each name/grade pair.

```Name's grade is number.
```

b. Create a main class, `Sam`, which does the following:

• Creates appropriate `PrintWriter`s and `InputReader`s.
• Creates an array of ten strings with names of people in this class.
• Creates an array of ten random grades (all integers).
• Uses a `Grader` to print those using `printGrades`.

### Exercise 2: Atypical Input

Determine experimentally waht happens in each of the following situations:

a. You try to create an array of size 0.

b. You try to create an array of negative size.

c. You try to use an invalid index (one less than 0 or greater than or equal to the length of the array).

### Exercise 3: Default Elements

Determine experimentally what the default values are in each of the following kinds of arrays.

a. An array of integers that is constructed but not initialized.

b. An array of strings that is constructed but not initialized.

c. An array of `BigIntegers` that is constructed but not initialized.

### Exercise 4: Finding the Highest Grade

Add a static method, `int highestGrade(int[] grades)` `Grader`. This method, as you might guess, should determine the highest grade in the array.

The strategy for determining the highest grade is fairly straightforward:

• Assume the grade in index 0 is the highest.
• Look at the remaining indices, updating your assumption as you see a higher grade.

Test your method when you are finished writing it.

Add a static method, ```void scale(int[] grades, double scalefactor)```, to `Grader`. This method should scale each grade in the array by the scale factor.

Note that you will need to do some type coercion in scaling grades.

When you have completed writing the method, test it.

### Exercise 6: Recomputing Grades, Revisited

Some faculty (but not me), like to recompute grades using the following algorithm:

• Determine the the highest grade in the class.

a. Add a static method, `void recompute(int[] grades)` to `Grader`.

b. Test that method.

c. What flaws do you observe in this strategy for recomputing grades?

## For Those With Extra Time

Build a new version of `HorizontallyComposed` and `VerticallyComposed` that accept arrays of `TextBlocks` rather than pairs of `TextBlock`s

## History

Sunday, 6 March 2005 [Samuel A. Rebelsky]

• Created

Tuesday, 7 March 2006 [Samuel A. Rebelsky]

• Minor edits.

This page was generated by Siteweaver on Thu Mar 30 15:24:34 2006.
The source to the page was last modified on Tue Mar 7 08:51:17 2006.
This page may be found at `http://www.cs.grinnell.edu/~rebelsky/Espresso/Labs/arrays.html`.

You may wish to validate this page's HTML ; ; Check with Bobby

Samuel A. Rebelsky
rebelsky@grinnell.edu