Here are some links from the past if you want to look:

http://www.cs.indiana.edu/classes/c212/fall2011/labFour.html

http://silo.cs.indiana.edu:8346/c212/fall2012/0913.phps

Also please note: 

   Lab Five exercises:

   p. 301  R6.1
   p. 302  R6.2, R6.3, R6.4, R6.5, R6.6

   Homework Five programs:

   p. 307  P6.6, P6.7, P6.9, P6.10

Instructions have been posted on the main course website.

Here's the program we will develop in class today: 

import java.util.Scanner;

class Arrays {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    Integer[] numbers; // declaration
    numbers = new Integer[0];
    System.out.println( java.util.Arrays.toString( numbers ) );
    System.out.print("type: ");
    String line = in.nextLine();
    while (! line.equals("bye")) {
      Integer number = Integer.parseInt( line );
      Integer[] temporary = java.util.Arrays.copyOf(numbers, numbers.length + 1);
      temporary[temporary.length - 1] = number;
      numbers = temporary;
      System.out.println( java.util.Arrays.toString( numbers ) );
      System.out.print("type: ");
      line = in.nextLine();
    }
    java.util.Arrays.sort( numbers );
    System.out.println( java.util.Arrays.toString( numbers ) );
  }
}

Here's how it works:

-bash-4.1$ javac Arrays.java
-bash-4.1$ java Arrays
 []
type: 3
[3]
type: 2
[3, 2]
type: 6
[3, 2, 6]
type: 1
[3, 2, 6, 1]
type: -2
[3, 2, 6, 1, -2]
type: 10
[3, 2, 6, 1, -2, 10]
type: bye
[-2, 1, 2, 3, 6, 10]
-bash-4.1$ 

What we want is to write the program and then develop our own methods too. 

--


An array is essentially a block of locations all of the same type.

int[] a; // declaration 

a = new int[10]; // allocation

for (int i = 0; i < a.length ; i = i + 1) {
  a[i] = (int) (100 * Math.random() - 50); 
}

String[] b = { "this", "is", "an", "example" }; 

So we wrote the program:

import java.util.Scanner; 
import java.util.Arrays;

class Two {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);  
    // System.out.println( in ); 
    int[] numbers; // declaration 
    numbers = new int[0]; 
    while (true) {
      System.out.println( Arrays.toString( numbers )); 
      System.out.print("enter> "); 
      String line = in.nextLine(); 
      System.out.println("You have typed: " + line); 
      if (line.equals("bye")) break; 
      int number = Integer.parseInt( line ); 
      System.out.println( number ); 
      // http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOf%28int[],%20int%29
      numbers = Arrays.copyOf(numbers, numbers.length + 1); 
      numbers[numbers.length - 1] = number; 
    }
    Arrays.sort( numbers ); 
    System.out.println( Arrays.toString (numbers) );     
  }
}

It works like this: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Two
[]
enter>  [DrJava Input Box]
You have typed: 5
5
[5]
enter>  [DrJava Input Box]
You have typed: 2
2
[5, 2]
enter>  [DrJava Input Box]
You have typed: 3
3
[5, 2, 3]
enter>  [DrJava Input Box]
You have typed: 1
1
[5, 2, 3, 1]
enter>  [DrJava Input Box]
You have typed: 4
4
[5, 2, 3, 1, 4]
enter>  [DrJava Input Box]
You have typed: -3
-3
[5, 2, 3, 1, 4, -3]
enter>  [DrJava Input Box]
You have typed: 8
8
[5, 2, 3, 1, 4, -3, 8]
enter>  [DrJava Input Box]
You have typed: 5
5
[5, 2, 3, 1, 4, -3, 8, 5]
enter>  [DrJava Input Box]
You have typed: 0
0
[5, 2, 3, 1, 4, -3, 8, 5, 0]
enter>  [DrJava Input Box]
You have typed: bye
[-3, 0, 1, 2, 3, 4, 5, 5, 8]


Three questions for Monday: 


(a) can you write the methods toString, copyOf and sort? 

(b) what's the difference between arrays and ArrayLists? 

(c) what about two- and multi-dimensional arrays and ArrayLists? 


Notes from the second lecture today: 

--

Today is Wednesday

There is a lab on Wed. 

Most of the labs are on Thu and Fri

Class Notes says: Exam Lab

For this early evaluation exam that's not much

It will mean more for the midterm

--

For this exam lab you still have a Lab Assignment

You will find it on the page I gave you

The lab is due Monday in GitHub

There's also a new Homework assignment

It's due next Thu

It's the upper half of the other side

--

numbers, operators, expressions, types, variables
assignment statements, if statements, loops
methods


the reading assignment for the week is chapter six

there is also a chapter from Wayne and Sedgewick

at this stage their chapter one is the perfect review

--


Let's write a program that prompts the user
for numbers. The user is to enter integers, 
or "bye" to indicate end of input. Numbers are
to be entered one per line. At the end the program
prints the numbers collected sorted ascending order.

Can we do this with just the info in chapters 1-5?


An array is a sequence of locations of the same type.

We indicate arrays using brackets. 

int a;

a = 5;

int[] b; // declaration 

b = new int[10]; // allocation 

// b will have 10 zeros inside

for (int i = 0; i < b.length; i = i + 1) {
  b[i] = (int) (100 * Math.random() - 50);
} // initialization 

So we wrote this program:

import java.util.Arrays; 

class One {
  public static void main(String[] args) {
     int a; 
     a = 1;
     System.out.println( a ); 
     int[] n;
     n = new int[20]; 
     System.out.println( Arrays.toString( n )); 
  }
}


import java.util.Scanner;
import java.util.Arrays; 

class Two {
  public static void main(String[] args) {
    int[] numbers; // declaration
    numbers = new int[ 0 ];
    Scanner in = new Scanner(System.in);
    // System.out.println( in ); 
    while (true) {
      System.out.println( Arrays.toString( numbers ) ); 
      System.out.print("enter: "); 
      String line = in.nextLine(); 
      if (line.equals("bye")) 
        break;
      // System.out.println( line ); 
      int number = Integer.parseInt( line ); 
      // System.out.println( number + 0 ); 
      numbers = Arrays.copyOf( numbers, numbers.length + 1 );
      // http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#copyOf%28int[],%20int%29
      numbers[numbers.length - 1] = number;    
    }
    Arrays.sort( numbers ); 
    // http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort%28int[]%29
    System.out.println( Arrays.toString( numbers ) );
  }
}


Three questions for Monday:

  (a) can you write those methods copyOf, toString and sort

  (b) what about ArrayLists? 

  (c) what about additional dimensions (arrays and ArrayLists) 

--