Spring 2016, The College of Saint Rose

Lecture 22: Arrays
Date: Thursday, April 14, 2016

Agenda

Due at the start of class, Thursday, April 21.

Please submit answers to these questions either as a hard copy (typeset or handwritten are OK) or by email to terescoj AT strose.edu by the start of class. Please use a clear subject line when submitting by email (e.g., CSC 252 Lecture 22 Assignment, Mary Smith). We will discuss these questions at the start of class, so no late submissions are accepted.

1. Convert your program with the stains on t-shirts from a recent in-class exercise to use an array instead of an ArrayList to hold the FilledOvals that represent the stains. This involves changing your instance variable declaration, replacing the construction of the ArrayList with a construction of an array (which much be done after you choose the random number of stains to place on the TShirt), and changing all places that the FilledOvals are added to or accessed within the ArrayList to use the array instead. (10 points)
2. Write a method that takes an array of double values as its parameter and returns the sum of all elements in the array. (4 points)
3. What is printed by this program? (8 points)
private void mystery() {
int [] report = { 5, 4, 10, 4, 6, 3, 4 };
int turn = 0;
int shot = 0;
int lastTotal = 0;

while (shot < report.length && turn < 10) {
int increment = report[shot] + report[shot+1];
if (increment >= 10) {
increment = increment + report[shot+2];
}
if (report[ shot] < 10) {
shot++;
}
lastTotal = lastTotal + increment;
shot++;
turn++;
System. out. println(shot + " : " + lastTotal);
}
}

Examples