Date: Mon, 14 Jul 2014 16:02:37 -0400
From: Daniel Brady (dabrady@umail.iu.edu)
To: Adrian German (dgerman@soic.indiana.edu)
Subject: Today's lab

I've attached the file we worked on in lab today. We only did one problem: the 
Interactive Lists problem. I expected to do more, but it turned out to be a problem 
that covered a lot of ground; there were lots of questions regarding:

 *  ArrayList syntax
 *  the for-each loop
 *  scanners
 *  when to use primitives vs. their wrapper classes (i.e. double vs. Double)
 *  parsing strings
 *  how to stop parsing user input based on some exit command
 *  when to initialize variables and what to initialize them with

We finished it around 3:15pm, and then there were a number of questions regarding GUIs 
assignments, so I turned it into a general help session for the last 15 minutes.

// Review.java 

import java.util.*;

public class Review {

  public static void main(String[] args) {
    interactiveLists();
  }

  //---- Review problems ----//
  public static void interactiveLists() {
    // Create a scanner.
    Scanner scanner = new Scanner(System.in);
    String prompt = "Please enter some values:";
    String exitCommand = "";
    String value = "hukarz";
    ArrayList<Double> values = new ArrayList<Double>();

    // Get user input.
    System.out.println(prompt);
    while ( true ) {
      value = scanner.nextLine();
      if ( !value.equals(exitCommand) ) {
        values.add( Double.parseDouble(value) );
      } else {
        break;
      }
    }

    System.out.println( values );

    // Compute things.
    double sum = 0;
    double max = 0; // immediately overwritten, but needed
    double min = 0; // for compiler happiness
    // Initialize max and min.
    if ( !values.isEmpty() ) {  // if ( values.size() > 0 )
      max = min = values.get(0);
      //min = values.get(0);
    }

    for ( Double val : values ) {
      // Add to the sum.
      sum += val;
      // Update max and min.
      if ( val > max ) {
        max = val;
      }
      if ( val < min ) {
        min = val;
      }
    }

    double avg = sum / values.size();
    double range = max - min;

    System.out.println("The average is: " + avg);
    System.out.println("The min is: " + min);
    System.out.println("The max is: " + max);
    System.out.println("The range is: " + range);
  }
}

/* The file above can be compiled and run as follows: 
 
-bash-4.1$ javac Review.java
-bash-4.1$ java Review
Please enter some values:
3
2
-1
0
9
-23
34.12 

[3.0, 2.0, -1.0, 0.0, 9.0, -23.0, 34.12]
The average is: 3.4457142857142853
The min is: -23.0
The max is: 34.12
The range is: 57.12
-bash-4.1$  

*******************************************************/