Learning objectives:
• Develop a simple Java program that asks for input, does arithmetic, and provides output.
• Practice definite loop
• Apply the Software Development Process.
Opening the starter code:
Download the Temps.java file which you will find labeled in the HW folder for this assignment
on OAKS. To open a java file without a BlueJ package, open BlueJ. Select the Project tab, then
click “Open Non BlueJ…” Find the folder that contains the file you want to open (in this case
Temps.java). Click the “Select Folder”.
What’s in the starter code?
The Temps class has two instance variables: an array of int values (temps) that represent
temperatures for some number of days and an int (numberOfDays) representing the number of
values that have been stored in the array.
A constructor has been written which accepts as a parameter an int representing the number of
days to be stored. The constructor sets the instance variable numberOfDays to this parameter
and fills the temps array with random temperatures between 76 and 95
A test driver is provided in the same class (it’s the main method).
Programming problem:
Write code for the following methods
- printList() → void: This void method prints the temperatures stored. It should print the
numeric day value followed by the stored temperature. The output could look
something like:
[0]: 79
[1]: 92
[2]: 84 - getTemp (int) → int: This method accepts as a parameter an int representing the day of
the month. The function returns the temperature on record for that day. The function
should return -1 if the day requested is not a valid day. For example, getTemp(2) should
return 84 given the data as pictured above. - min() → int: This method returns the minimum temperature found in the array.
- avg() → double: This method returns the average of the temperatures.
- copy() → int []: This method returns a copy (not a clone) of the array of values. This
method returns a copy of the list of temps. The driver compares the first elements in the
temps and the copy, before and after the copy is changed. This does not really confirm that the copy method worked properly. Describe in the comments of your .java how to
accurately verify that the returned list is a copy.