Inf1 OP : Lab Sheet Week 3 Q5 - Median Temperature
Overview
Warning
Pair Programming:
This exercise is reserved for pair programming in the live lab sessions.
Please skip it when doing the exercises individually.

Since this exercise is more complex than the previous one, it has been broken down into three sub-tasks:

  1. Converting a sequence of command-line arguments into an array of temperature values.
  2. Sorting this array.
  3. Calculating the median of the array.

Our data comes from a super high-tech thermometer which records the temperature over time, but outputs its readings in a slightly strange format. The format consists of series of symbols, of which the first is a number that represents the starting temperature, while the succeeding symbols show the change in temperature since the previous one. These symbols are decoded as follows:

  • '.' means no change
  • '+' means an increase of 1 degree from the previous temperature
  • '-' means a decrease of 1 degree from the previous temperature

All the calculated values are to be interpreted as numbers of type int. For example, the sequence of symbols

34 . . + + . - .

would encode the following sequence of temperatures:

34 34 34 35 36 36 35 35

Our task is the calculate the median of the temperature data. We first need to sort our array of numbers. Then,

  • if the array contains an odd number \( n \) of elements, the median is the middle element; i.e., the \( {(n+1)/2}^{th} \) element.
  • if the array contains an even number \( n \) of elements, the median is the mean of the two middle elements: i.e., the mean of \( {n/2}^{th} \) and the \( {(n/2)+1}^{th} \) element.

Warning

Note that we have expressed the indices of elements just now using 1 as the start index. Remember that Java uses zero-based indexing!

For example, suppose we have an array containing the following elements:

3 6 7 8 9 3 1 2 7

First we sort the array:

1 2 3 3 6 7 7 8 9

Now, since we have nine elements in the array (i.e. an odd number), the median value is simply the middle (i.e. \( 5^{th} \)) element in the sorted array, i.e 6.

For contrast, suppose we have an array with these values:

3 6 7 8 5 5

Then we would again sort it:

3 5 5 6 7 8

This time, there are six elements. Since there is no middle element, to calculate the median we take the mean of the two central elements, i.e the \( 3^{rd} \) and \( 4^{th} \) elements and take their mean:

\[ (5 + 6)/2.0 = 11/2.0 = 5.5 \]

Note

Although the temperature outputs values as integers, the example we have just seen shows that your median value may turn out to be a floating-point number. Make sure that your program can handle this properly.

Calculate Temperature Values

In the first part of this exercise, write a program TempMedian that will read the input from the command-line, and create an array of temperature values of type int. Then print out the values:

: java TempMedian 34 . . . + . + . +
34 34 34 34 35 35 36 36 37

: java TempMedian 7 - - - - . .
7 6 5 4 3 3 3

Warning

Because of the way object comparison works in Java, you cannot use a boolean expression of the form s1 == s2 to test whether two items s1 and s2 of type String are the same. You must use the equals() method of String objects. I.e., you should write if (s1.equals(s2)) (or equivalently if (s2.equals(s1))).

Sort the values

Now that you have an array of int values representing temperatures, you should extend TempMedian so that it now also prints out the sorted array of values on a new line (i.e., the second line).

To sort an array of numbers, use the Java static method Arrays.sort(). Note that this sorts the list ‘in place’; you do not have to assign the result of sorting to a new variable. For example, the following code will sort the array myValues[]:

import java.util.Arrays;

public class SortingExample {

    public static void main(String[] args) {
        int[] myValues = new int[4];
        myValues[0] = 3;
        myValues[1] = 2;
        myValues[2] = 5;
        myValues[3] = -1;

        Arrays.sort(myValues);

        // print out elements of myValues[]
    }
}

Your program should output the sorted array on the second line. For example:

: java TempMedian 34 . . . + . - - - . +
34 34 34 34 35 35 34 33 32 32 33
32 32 33 33 34 34 34 34 34 35 35

: java TempMedian 7 - - - . .
7 6 5 4 4 4
4 4 4 5 6 7

: java TempMedian 10 - + . - . - . +
10 9 10 10 9 9 8 8 9
8 8 9 9 9 9 10 10 10
Calculate the Median

Finally, your program TempMedian should output the median value of the data set. Take care to ensure that your program outputs the median as a floating-point number.

For example:

: java TempMedian 34 . . . + . - - - . +
34 34 34 34 35 35 34 33 32 32 33
32 32 33 33 34 34 34 34 34 35 35
34.0


: java TempMedian 7 - - - . .
7 6 5 4 4 4
4 4 4 5 6 7
4.5

: java TempMedian 10 - + . - . - . +
10 9 10 10 9 9 8 8 9
8 8 9 9 9 9 10 10 10
9.0

: java TempMedian 3 - - - -
3 2 1 0 -1
-1 0 1 2 3
1.0

An automated test has been created for this exercise: TempMedianTest.java.