CMPU 101 - Lab 6

$Revision: 1.2 $

In this lab you will work with arrays and loops.  The lab is structured as a series of methods you will implement and then invoke from a main method.

Getting Started

Start DrJava.  In a new source file, create a class called Temps.  Add a main method that creates a new Scanner object reading from System.in.  Your main method should look like this:

public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
}

Don't forget to import java.util.Scanner at the top of the file.

Running the Program

It will be more convenient to run the program from a terminal window (rather than the DrJava interactions window).  Open a terminal window using the button on the toolbar that looks like this:

To run the program, type the command

java Temps

in the terminal window and press return.  Make sure you compile your program using the "Compile All" button before running it in the terminal window.

Step 1: Read an Array of Temperatures

Write a static method called readTemps that

  1. takes a reference to a Scanner object as a parameter and
  2. returns an array of double values.

The method should prompt the user for a number of days, create an aray of double whose length is the number of days, and then use the Scanner to read one double value representing a temperature into each element of the array.  Here is what calling the method should look like in the terminal window (user input shown in bold):

Number of days: 7
Temp on day 0: 30.0
Temp on day 1: 31.4
Temp on day 2: 38.2
Temp on day 3: 55.3
Temp on day 4: 23.0
Temp on day 5: 16.3
Temp on day 6: 26.7

In this example, the array returned by the method should return an array containing the values

30.0, 31.4, 38.2, 55.3, 23.0, 16.3, 26.7

Modify the main method so that it calls this method (passing scanner as the argument) and saves the returned array in a local variable.

Step 2: Print the Contents of the Array

Write a static method called printTemps that

  1. takes an array of double as a parameter and
  2. returns void (does not return a value)

The method should use System.out.print to print out each value in the array passed as the parameter, separated by ", ".  After printing out each value in the array, the method should call System.out.println with no arguments in order to terminate the line.

Add statements to the main method to print "Daily temperatures:" and then print the values of each element of the array.

Step 3: Compute the Average Temperature

Write a static method called computeAverageTemp that

  1. takes an array of double as a parameter and
  2. returns a double value which is the average of all of the elements in the parameter array

Add code to your main method so that it prints the string "Average daily temperature: " and then prints the average of the temperatures stored in the array.

Step 4: Find the Greatest Increase in Temperature

Write a static method called findGreatestIncrease that

  1. takes an array of double as a parameter and
  2. returns an int value indicating the array index of the first of two consecutive days where the greatest increase in temperature occurred

For example, if the array filled in by the user contains the values

30.0, 31.4, 38.2, 55.3, 23.0, 16.3, 26.7

then the method should return 2, because the greatest increase in temperature was between the element at index 2 (38.2) and the element at index 3 (55.3).

Add code to your main method to use findGreatestIncrease to find the greatest increase in temperature and print it in the form

Greatest increase: from x to y and day z

where x and y are two consecutive daily temperatures and z is the array index of the element whose value is x (the first of the two daily temperatures).

Complete Example Transcript

Here is a complete example transcript showing the output of the Temps program based on user input (which is shown in bold):

Number of days: 7
Temp on day 0: 30.0
Temp on day 1: 31.4
Temp on day 2: 38.2
Temp on day 3: 55.3
Temp on day 4: 23.0
Temp on day 5: 16.3
Temp on day 6: 26.7
Daily temperatures: 30.0, 31.4, 38.2, 55.3, 23.0, 16.3, 26.7
Average daily temperature: 31.557142857142853
Greatest increase: from 38.2 to 55.3 on day 2

When You Are Done

Submit Temps.java to the CS 101 Submission Website.

Make sure you submit the Java source file: the filename should end in ".java", not ".java~" or ".class".