Howdy. Welcome to the month of July 2014!

We start by defining the lab assignment for today. 

Compile and run this class:

import java.util.Scanner;

class Arrays {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int[] numbers; // declaration
    numbers = new int[0]; // allocation 
    System.out.println( java.util.Arrays.toString( numbers ) );
    System.out.print("type: ");
    String line = in.nextLine();
    while (! line.equals("bye")) {
      int number = Integer.parseInt( line );
      int[] 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 for me: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Arrays
[]
type:  3
[3]
type:  2
[3, 2]
type:  5
[3, 2, 5]
type:  1
[3, 2, 5, 1]
type:  -4
[3, 2, 5, 1, -4]
type:  bye 
[-4, 1, 2, 3, 5]
>

This is just like your Homework One, only more is done here. 

All numbers are collected and reported. 

Can you understand this program? 

If you try you'll find out you need to understand: 

  java.util.Arrays.toString(...)
  java.util.Arrays.copyOf(...)
  java.util.Arrays.sort(...)

These are the methods you have to define in lab today. 

For that we warm up by defining a method: 

class Tuesday {
  public static void main(String[] args) {
    assert( isPrime(3) ); 
    assert( ! isPrime(4) ); // == false ); 
    assert( isPrime(5) ); 
    assert( isPrime(41) ); 
    assert( isPrime(2) ); 
  }
  public static boolean isPrime(int number) {
    for (int count = 2; count < number; count = count + 1) {
      if (number % count == 0) {
        return false; 
      }
    } 
    return true;
  }
}

Notice I include evidence that the program is working well. 

Next I need to start exploring arrays. 

So I do the following in Dr Java:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int[] book = { 4, 2, 3, -1, 7 };
> book
{ 4, 2, 3, -1, 7 }
> book[3]
-1
> book[0]
4
> book.length
5


I get encouraged and I write this code:

import java.util.Arrays;

class Tuesday {
  public static void main(String[] args) {

    int[] a = {6, 3, -2, 8}; 
    System.out.println( a[2] ); // -2 
    System.out.println( a[ a.length-1 ] ); // 8
    System.out.println( a ); // {6, 3, -2, 8}

    double squareRootOfTwo = Math.sqrt( 2 ); 
    System.out.println( squareRootOfTwo ); 
    
    Arrays.sort( a ); 
    
    String myArray = Arrays.toString( a ); 
    System.out.println( myArray ); 
    
  }
}

This includes having to print the array. 

The third method you need to define is copyOf and it works like this: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int a = 4
> a
4
> a = 5
5
> a
5
> int[] b = { 5, 2, 3, 1 -6 }
> b
{ 5, 2, 3, -5 }
> b[1]
2
> b[1] + 5
7
> b
{ 5, 2, 3, -5 }
> b[1] = 9
9
> b
{ 5, 9, 3, -5 }
> b
{ 5, 9, 3, -5 }
> Arrays.toString( b )
Static Error: No method in static Arrays has name 'toString'

The Arrays class mentioned here is the one I started with. 

So I call the Arrays class in java.util by the package name: 

> java.util.Arrays.toString( b )
"[5, 9, 3, -5]"
> java.util.Arrays.copyOf( b , 6 )
{ 5, 9, 3, -5, 0, 0 }
> b = java.util.Arrays.copyOf(b, b.length + 1)
{ 5, 9, 3, -5, 0 }
> b
{ 5, 9, 3, -5, 0 }
> b[4] = 19
19
> b
{ 5, 9, 3, -5, 19 }


So your goal in lab is to define these three methods. 

See you then. 

--