CS 112 Lab 2

Agenda

We will review the following topics in today's lab,

  1. Using the Eclipse IDE

  2. Java API documentation (google Java 6 API)
    http://java.sun.com/javase/6/docs/api/

  3. Measuring execution time

  4. Pseudorandom numbers

  5. Generic types in Java

    This webpage gives a good overview of generics in Java
    http://www.onjava.com/pub/a/onjava/excerpt/javaian5_chap04/index.html

  6. List interface and the LinkedList class (compare with the Array class)

Today's example

package lab02;
import java.util.*;
 
public class LabExample {

    public static void main(String[] args) {
        
        // Measuring execution times.
        long start = System.nanoTime();
        long stop = System.nanoTime();
        System.out.printf("Execution time: %d \n\n", stop - start);
        
        // Pseudo-random number generation
        // Note: seed determines the random number sequence.
        long seed = 232345;  // using fixed seed.
        Random rand = new Random(seed);
        // Now generate a few random numbers
        int randomInt = rand.nextInt(10);
        double randomDouble = rand.nextDouble();
        
        // To set a seed that changes each time you run
        seed = Calendar.getInstance().getTimeInMillis();
        rand = new Random(seed);
        int randomInt2 = rand.nextInt(10);
        double randomDouble2 = rand.nextDouble();
        System.out.printf("%d %f | %d %f\n", 
                randomInt, randomDouble, randomInt2, randomDouble2);
        
        // GENERIC TYPES MOTIVATION
        // Untyped List: is a collection of ANY instances of Object class
        List list = new LinkedList();
        list.add(new Integer(10));
        list.add(new Double(10.5));
        list.add(new String("asdas"));
        // There is no consistency in the item type.
        // We run into trouble when we run sumList below.
        double listSum = sumListUntyped(list); 
        
        // TYPED list: the list can ONLY contain instances of a "specific" class
        List<Integer> intList = new LinkedList<Integer>();
        intList.add(new Integer(10));  // okay
        intList.add(new Double(10.5)); // not allowed, compile error
        intList.add(new Float(20.7));  // not allowed, compile error
        double intListSum = sumListTyped(intList);
        
        List<Double> dblList = new LinkedList<Double>();
        dblList.add(new Integer(10)); // not allowed, compile error  
        dblList.add(new Double(10.5)); // okay
        dblList.add(new Double(20.7));  // okay
        double dblListSum = sumListTyped(dblList);
        
        Integer maxIntList = maxListTyped(intList);
        Double dblIntList = maxListTyped(dblList);
    }
    
    // Untyped list sum
    static double sumListUntyped(List list){
        Iterator listIterator = list.iterator();
        
        double sum = 0;
        while( listIterator.hasNext() ){
            sum += (Double)listIterator.next();
        }
        return sum;
    }
    
    // Typed list sum method, note the method specification
    static double sumListTyped(List<? extends Number> list){
        Iterator<? extends Number> listIterator = list.iterator();
        
        double sum = 0;
        while( listIterator.hasNext() ){
            sum += listIterator.next().doubleValue();
        }
        return sum;
    }
    
    // Typed list max method, note method specification
    static <N extends Number> N maxListTyped(List<N> list){
        Iterator<N> listIterator = list.iterator();
        
        N maxValue = listIterator.next();
        while( listIterator.hasNext() ){
            N curValue = listIterator.next();
            if( curValue.doubleValue() > maxValue.doubleValue() )
                maxValue = curValue; 
        }
        return maxValue;
    }
}

URL

http://cs-people.bu.edu/tvashwin/cs112_spring09/lab02.html