Howdy.

Exam on Wed. 

public class Monday {
  public static void main(String[] args) {
     double num = Double.parseDouble( args[0] ); 
     double low = 0, high = num; 
     double guess = (low + high) / 2; 
     while ( high - low > 0.0000001 ) {
       if (guess * guess == num) {
         break;
       } else if (guess * guess > num) {
         high = guess; 
         guess = (low + high) / 2; 
       } else { // guess * guess < num 
         low = guess;          
         guess = (low + high) / 2; 
       }
       System.out.println( guess ); 
     }
     System.out.println( guess + " " + Math.sqrt(num) );
  }
}

Arrays: 

  (a) declaration 

  (b) allocate

  (c) initialize (modify) 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int a
> a = 5
5
> int[] b
> b = new int[10]
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
> b
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
> int c[]
> c
null
> c = new int[6]
{ 0, 0, 0, 0, 0, 0 }
> b[4]
0
> c[2]
0
> c[2] = 9
9
> c
{ 0, 0, 9, 0, 0, 0 }
> b[4] = -2
-2
> b
{ 0, 0, 0, 0, -2, 0, 0, 0, 0, 0 }
> b.length
10
> b[b.length - 1] = 8
8
> b
{ 0, 0, 0, 0, -2, 0, 0, 0, 0, 8 }
> c
{ 0, 0, 9, 0, 0, 0 }
> c[c.length - 1] = 12
12
> c
{ 0, 0, 9, 0, 0, 12 }


public class Monday {
  public static void main(String[] args) {
    
    int[] b = { 4, 3, 6, 5, 1, 2 }; 
    
    int c[]; // declaration
    
    c = new int[6]; // allocation
    
    c[2] = -1; 
    c[c.length - 1] = 14; 
    
    System.out.println( c ); 
    
  }
}

This compiles and runs and prints: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Monday
[I@426b33b8


So I modify it: 

import java.util.Arrays; 

public class Monday {
  public static void main(String[] args) {
    
    int[] b = { 4, 3, 6, 5, 1, 2 }; 
    
    int c[]; // declaration
    
    // System.out.println( c ); 
    
    c = new int[6]; // allocation
    
    System.out.println( Arrays.toString( c ) ); 
    System.out.println( c.length ); 
    
    c[2] = -1; 
    c[c.length - 1] = 14; 
    
    System.out.println( Arrays.toString( c ) ); 
    
  }
}

Here's how it runs: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Monday
[0, 0, 0, 0, 0, 0]
6
[0, 0, -1, 0, 0, 14]


Now I introduce another useful function: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> import java.util.Arrays
> int[] a = {1, 2, 3};
> a
{ 1, 2, 3 }
> Arrays.copyOf( a, 4 )
{ 1, 2, 3, 0 }
> Arrays.copyOf( a, 4 )
{ 1, 2, 3, 0 }
> int[] b = Arrays.copyOf( a, 4 )
> a = Arrays.copyOf( a, 4 )
{ 1, 2, 3, 0 }
> a
{ 1, 2, 3, 0 }
>

Do you think I can have arrays of size 0 (zero)? 

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

public class Monday {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in); 
    System.out.print("Type: ");
    String line = in.nextLine(); 
    int[] nums = new int[0]; 
    System.out.println( Arrays.toString( nums ) ); 
    while (! line.equals("bye") ) {
      int num = Integer.parseInt( line ); 
      nums = Arrays.copyOf( nums, nums.length + 1 ); 
      nums[nums.length - 1] = num;
      System.out.println( Arrays.toString( nums ) ); 
      System.out.print("Type: ");
      line = in.nextLine();   
    }
    
  }
}

Here's how it works:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Monday
Type:  [DrJava Input Box]
[]
[5]
Type:  [DrJava Input Box]
[5, 2]
Type:  [DrJava Input Box]
[5, 2, 6]
Type:  [DrJava Input Box]
[5, 2, 6, 3]
Type:  [DrJava Input Box]
[5, 2, 6, 3, 1]
Type:  [DrJava Input Box]
[5, 2, 6, 3, 1, 7]
Type:  [DrJava Input Box]


On the paper write your name for attendance

Write the names of your lab instructors

For each

  (a) a good thing (if any)

  (b) a bad thing (if any) 

  (c) which one of (a) or (b) is more important to you (for now)

--