Arrays


Goals


Setting up and accessing a fixed-length array (File: Five.java)

Download the file Five.java which contains "skeleton code." This is a valid Java program, but it doesn't yet do what we want it to.

Complete Five.java by doing the following:

  1. Make the existing constructor work
  2. Add a new constructor that accepts five integer arguments and places those arguments in the data array
  3. Make the fillArray, sumArray and report methods work as described in the comments above them.
  4. Add a getData method that takes no arguments and returns the instance variable that is an array of five integers. Why might it be a bad idea to have such a method?
Sample Interactions five.hist
> Five test = new Five();  
> test.sumArray()  0  
> test.report() 
 0 
 0  
 0  
 0  
 0  
 sum: 0  
> test.fillArray(0);  
> test.sumArray()  
100  
> test.report() 
 0  
10 
20  
30  
40  
sum: 100