CSIT 115 Class 2
Last time: Idea of API, example from book—now a class exercise (linked from class web page)
Next review example: Temperature2.java, pg. 436.
See how it runs… pg. 437
Now this program needs an array because it has to look at each number more than once.
The plan is:
Ask for the number of days using Scanner’s nextInt()
Set up an array with a spot for each day
For each day:
Read a number with Scanner’s nextInt()
Put it in the array, sum it up for the average
Compute the average temp.
Use a loop to count days over average
Report results
Star of this program: the array of ints for the daily high temperatures
With numDays = 9, we create the array like this:
Int[] temps = new int[numDays];
Note that we needed to know how many numbers ahead of time for an array, and there’s no way to resize an array, so this is a serious restriction. We can move all the numbers into a new bigger array, and this is a common trick.
The usual way to avoid the heartbreak of fixed array size is using ArrayList, which we will study after objects (in Chap 10).
After we get the daily temperatures into the array, it will look like this:
temps
[0] [1] [2] [3] [4] [5] [6] [7] [8]
75 |
78 |
85 |
71 |
69 |
82 |
74 |
80 |
87 |
temps[0] = 75, …temps[8] = 87
temps.length = 9
So could write firstTemp = temps[0]; lastTemp=temps[8], or more generally
lastTemp = temps[temps.length – 1]
Of course we can change any number in the array:
temps[1] = 90
[0] [1] [2] [3] [4] [5] [6] [7] [8]
75 |
90 |
85 |
71 |
69 |
82 |
74 |
80 |
87 |
How could we print this? Discussed on pg. 448—can use Arrays method, or more basically, just loop
for (int i=0;i<temps.length;i++) {
System.out.print(temps[i] + “ “);
}
75 78 85 …
Or more neatly, using the enhanced for loop:
for (int tem: temps) {
System.out.print(tem + “ “);
}
Back to the program
Crucial points:
Using a Scanner named console, get an int from the user for numDays
Int numDays = console.nextInt();
Given numDays (= 9, say), set up an array for numDays ints:
Int[] temps = new int[numDays];
Loop through numDays cases
For (int I =0; i<numDays; i++) {
Blah blah blah
}
Get one int from the user and put it in the array temps, at the ith spot:
temps[i] = consolt.nextInt();
Computing an average: need sum, number of cases
Sum: use a running sum, add each value on to it.
Start with sum = 0
In loop over i: sum += temps[i]; //Add one value to a running sum
After loop: average = sum/numDays, but watch out! Integer division will cause inaccuracy here
Example: sum = 14, numDays = 3, 14/3 = 4, but should be 4.6666 to higher accuracy.
Need to use double for average: double average = (double)sum/numDays;
OK, write out program…
Now, lLet’s try using methods here.
One job: summing up the array
We can write a method that accepts an int array, returns its sum
Header: public static int sumArray(int[] a)
Call to it: int sum = sumArray(temps); // use method to sum our temps array
Declaration: header + body:
public static int sumArray(int[] a)
{
int sum = 0;
for (int i=0; i<a.length;i++) {
sum += a[i];
}
return sum;
}
Or, using the “enhanced for loop”:
public static int sumArray(int[] a)
{
int sum = 0;
for (int entry: a) {
sum += entry;
}
return sum;
}
Call this method for temps: int sum = sumArray(temps);
We can even change an array from inside a method, unlike an int param. An array is an object, not a primitive structure, so its name is “reference”. More on this soon. For now, you can look at the example “incrementAll” on pg. 440.