Howdy. 

  Ankney, Garrett
  Berry-Simms, Khalea
  Clark, Daniel
  Cooper, Keiland
  Cotter, Ryan
  Gan, Kang Jie
  Hay, Stuart
  He, Kefei
  Hiraga, Misato
  Hu, Tao
  Jing, Elise
  Kaefer, John
  Kim, Jinsu
  Kowala, Katherine
  Liu, Li
  Matlock, Noah
  Navarro, Nicholas
  Nieves, Rafael
  O'dell, Taylor
  Parker, Patrick
  Pena, Jillian
  Qian, Jiang
? Reba, Christopher
  Ruiz, Daniel
  Salazar, Luis
  Shen, Bihan
  Stamets, Justin
  Wang, Iris
  Wang, Yibo

Java programs are made out of classes. 

Classes are containers. What do they contain? 

They contain members: variables and procedures (methods). 

The members can be: static or non-static. The main method is static. 

In Java there are four kinds of variables: 

  (a) local variables (inside methods)

  (b) parameters 

The names of parameters and local variables: whatever name(s) you gave them. 

  (c) static variables (defined in class outside methods, belong to the class) 

  (d) instance variables (like static but don't have the keyword static)

Static variables need to be accessed through the class they belong. 

Instance variables need to be accessed through the instance they belong to. 

Classes also contain a blueprint (all the instance members plus constructors). 

public class Horse {
   
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a
> a 
null
> a = new Horse()
Horse@2437696c
> a
Horse@2437696c


When you define a class it comes with a default constructor. 

The default constructor is the no-arg constructor. 

So above when I have nothing I actually have this:

public class Horse {
  public Horse() {
    
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Horse()
> a
Horse@5ac9c9d9
> Horse b = new Horse()
> b
Horse@7b7019b7


Now you can customize your modeling and write something like this:

public class Horse {
  String name;
  public Horse(String givenName) {
    name = givenName; 
  }
}

Default means in absence of and you have just designed a constructor. 

So you are not in the default case so you get the constructors you specify.

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Horse("Seabiscuit");
> Horse b = new Horse("Secretariat");
> a
Horse@413e42b7
> b
Horse@6cc264bd


I'd like these guys to talk. 

> a.name
"Seabiscuit"
> b.name
"Secretariat"


Now let's make these guys talk. 

public class Horse {
  String name;
  public Horse(String givenName) {
    name = givenName; 
  }
  public void talk() {
    System.out.println("Howdy."); 
  }
}

Now we play:

public class Horse {
  String name;
  public Horse(String givenName) {
    name = givenName; 
  }
  public void talk() {
    System.out.println( name ); 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Horse("Seabiscuit");
> a.talk()
Seabiscuit
> a
Horse@5cd167b


Is it possible for me to get that ID printed from inside? 

Does a Horse object know about its unique ID or not? 

public class Horse {
  String name;
  public Horse(String givenName) {
    name = givenName; 
  }
  public void talk() {
    System.out.println( this ); 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Horse("Seabiscuit");
> a
Horse@3e929040
> a.name
"Seabiscuit"
> a.talk()
Horse@3e929040
> Horse b = new Horse("Secretariat");
> b.talk()
Horse@34fe4f8c


What does this mean? It means that every object can refer to itself that way. 

You can't use this inside a static method because static methods belong to class.

Instance methods belong to instances which are not created until you invoke new. 

Instance methods in fact have the ability to refer to the object that holds them. 

That's what I meant that we should always access instance members through the object/instance that holds them. 

public class Horse {
  String name;
  public Horse(String name) {
    this.name = name; 
  }
  public void talk() {
    System.out.println( "Howdy, I am " + this.name ); 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse b = new Horse("Secretariat");
> Horse a = new Horse("Seabiscuit");
> a.talk()
Howdy, I am Seabiscuit
> b.talk()
Howdy, I am Secretariat
> a
Horse@75c2e76d


Let's discuss arrays. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int[] n // declaration 
> n
null
> n = new int[6] // alocation 
{ 0, 0, 0, 0, 0, 0 }
> n
{ 0, 0, 0, 0, 0, 0 }
> n[1] = 3 // initialization 
3
> n
{ 0, 3, 0, 0, 0, 0 }
> n.length
6
> n[1] += 2 // modification or update
5
> n
{ 0, 5, 0, 0, 0, 0 }


public class One {
  public static void main(String[] args) {
    int[] n = new int[10]; 
    System.out.println( n ); 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
[I@29324670


How do I see what's inside? 

https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html

https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html

import java.util.Arrays; 

public class One {
  public static void main(String[] args) {
    int[] n = new int[10]; 
    System.out.println( Arrays.toString( n )); 
    for (int i = 0; i < n.length; i++) {
      n[i] = n.length - i; 
    }
    System.out.println( Arrays.toString( n )); 
  }


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]


https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#toString-int:A-

https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#copyOf-int:A-int-

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int[] n = new int[3]
> n
{ 0, 0, 0 }
> n[0] = 3
3
> n[1] = 7
7
> n[2] = 5
5
> n
{ 3, 7, 5 }
> int[] m = Arrays.copyOf(n, n.length + 1)
Static Error: Undefined name 'Arrays'
> int[] m = java.util.Arrays.copyOf(n, n.length + 1)
> m
{ 3, 7, 5, 0 }
> m[3] = -2
-2
> n
{ 3, 7, 5 }
> m
{ 3, 7, 5, -2 }
> n = m
{ 3, 7, 5, -2 }
> n
{ 3, 7, 5, -2 }
> m
{ 3, 7, 5, -2 }


Let's go back to the problem we discussed whereby the user is entering
numbers one by one and when the user enters "Bye" we print stats: count, 
sum, average, max, min. This time I want you to show me everything you
have seen up to that point, all the numbers, sorted in ascending order. 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int[] n = new int[0]
> n
{  }
> n.length
0


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

public class One {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] n = new int[0]; 
    while (true) {
      System.out.print("Type: "); 
      String line = input.nextLine(); 
      if (line.equalsIgnoreCase("bye")) { 
        System.out.println("Bye bye now...");
        break;  
      }
      int number = Integer.parseInt(line); 
      n = Arrays.copyOf(n, n.length + 1); 
      n[n.length-1] = number; 
      System.out.println( Arrays.toString( n ) ); 
    }
  }
}

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

public class One {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] n = new int[0]; 
    while (true) {
      System.out.print("Type: "); 
      String line = input.nextLine(); 
      if (line.equalsIgnoreCase("bye")) { 
        System.out.println("Bye bye now...");
        break;  
      }
      int number = Integer.parseInt(line); 
      n = Arrays.copyOf(n, n.length + 1); 
      n[n.length-1] = number; 
      System.out.println( Arrays.toString( n ) ); 
    }
    // after the loop 
    Arrays.sort( n ); 
    System.out.println( Arrays.toString( n ) );     
  }


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
Type:  [DrJava Input Box]
[4]
Type:  [DrJava Input Box]
[4, 2]
Type:  [DrJava Input Box]
[4, 2, 3]
Type:  [DrJava Input Box]
[4, 2, 3, 5]
Type:  [DrJava Input Box]
[4, 2, 3, 5, 1]
Type:  [DrJava Input Box]
[4, 2, 3, 5, 1, 6]
Type:  [DrJava Input Box]
[4, 2, 3, 5, 1, 6, 3]
Type:  [DrJava Input Box]
[4, 2, 3, 5, 1, 6, 3, 2]
Type:  [DrJava Input Box]
[4, 2, 3, 5, 1, 6, 3, 2, 4]
Type:  [DrJava Input Box]
Bye bye now...
[1, 2, 2, 3, 3, 4, 4, 5, 6]


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

public class One {
  public static String toString(int[] whatever) {
    String answer = ""; 
    for (int index = 0; index < whatever.length; index = index + 1) {
      answer = answer + " " + whatever[index];  
    }
    return "[ " + answer + " ]"; 
  }
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] n = new int[0]; 
    while (true) {
      System.out.print("Type: "); 
      String line = input.nextLine(); 
      if (line.equalsIgnoreCase("bye")) { 
        System.out.println("Bye bye now...");
        break;  
      }
      int number = Integer.parseInt(line); 
      n = Arrays.copyOf(n, n.length + 1); 
      n[n.length-1] = number; 
      System.out.println( One.toString( n ) ); 
    }
    // after the loop 
    Arrays.sort( n ); 
    System.out.println( One.toString( n ) );     
  }


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
Type:  [DrJava Input Box]
[  4 ]
Type:  [DrJava Input Box]
[  4 2 ]
Type:  [DrJava Input Box]
[  4 2 3 ]
Type:  [DrJava Input Box]
[  4 2 3 5 ]
Type:  1
[  4 2 3 5 1 ]
Type:  [DrJava Input Box]
Bye bye now...
[  1 2 3 4 5 ]


Let's do copyOf now. 

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

public class One {
  public static int[] copyOf(int[] whatever, int newLength) {
    int[] m = new int[newLength];
    for (int index = 0; index < Math.min(whatever.length, newLength); index++) {
      m[index] = whatever[index];  
    }
    return m;
  }
  public static String toString(int[] whatever) {
    String answer = ""; 
    for (int index = 0; index < whatever.length; index = index + 1) {
      answer = answer + " " + whatever[index];  
    }
    return "[ " + answer + " ]"; 
  }
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] n = new int[0]; 
    while (true) {
      System.out.print("Type: "); 
      String line = input.nextLine(); 
      if (line.equalsIgnoreCase("bye")) { 
        System.out.println("Bye bye now...");
        break;  
      }
      int number = Integer.parseInt(line); 
      n = One.copyOf(n, n.length + 1); 
      n[n.length-1] = number; 
      System.out.println( One.toString( n ) ); 
    }
    // after the loop 
    Arrays.sort( n ); 
    System.out.println( One.toString( n ) );     
  }


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
Type:  [DrJava Input Box]
[  4 ]
Type:  [DrJava Input Box]
[  4 2 ]
Type:  [DrJava Input Box]
[  4 2 1 ]
Type:  [DrJava Input Box]
[  4 2 1 6 ]
Type:  [DrJava Input Box]
[  4 2 1 6 3 ]
Type:  [DrJava Input Box]
Bye bye now...
[  1 2 3 4 6 ]


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

public class One {
  public static void sort(int[] whatever) {
    for (int num = 0; num < whatever.length; num++) {
      for (int index =0; index < whatever.length-1; index++) {
        if (whatever[index] < whatever[index+1]) {
          int temp = whatever[index]; 
          whatever[index] = whatever[index+1];
          whatever[index+1] = temp;
        }
      }
    }
  }
  public static int[] copyOf(int[] whatever, int newLength) {
    int[] m = new int[newLength];
    for (int index = 0; index < Math.min(whatever.length, newLength); index++) {
      m[index] = whatever[index];  
    }
    return m;
  }
  public static String toString(int[] whatever) {
    String answer = ""; 
    for (int index = 0; index < whatever.length; index = index + 1) {
      answer = answer + " " + whatever[index];  
    }
    return "[ " + answer + " ]"; 
  }
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int[] n = new int[0]; 
    while (true) {
      System.out.print("Type: "); 
      String line = input.nextLine(); 
      if (line.equalsIgnoreCase("bye")) { 
        System.out.println("Bye bye now...");
        break;  
      }
      int number = Integer.parseInt(line); 
      n = One.copyOf(n, n.length + 1); 
      n[n.length-1] = number; 
      System.out.println( One.toString( n ) ); 
    }
    // after the loop 
    One.sort( n ); 
    System.out.println( One.toString( n ) );     
  }


Here's how this works:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
Type:  [DrJava Input Box]
[  5 ]
Type:  [DrJava Input Box]
[  5 2 ]
Type:  [DrJava Input Box]
[  5 2 3 ]
Type:  [DrJava Input Box]
[  5 2 3 1 ]
Type:  [DrJava Input Box]
[  5 2 3 1 6 ]
Type:  [DrJava Input Box]
[  5 2 3 1 6 4 ]
Type:  [DrJava Input Box]
Bye bye now...
[  6 5 4 3 2 1 ]


--

double sumOfSquares = 0,
       sum = 0, 
       count = 0; 
while (true) {
  String line = ...;
  if (line.equals("bye")) 
    break; 
  double number = ... 
  count += 1; 
  sum += number; 
  sumOfSquares += number * number; 
}

System.out.println( Math.sqrt( (sumOfSquares - sum * sum / count ) / (count - 1) ) ); 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> "Tom".compareTo("tomato")
-32
> "A".compareTo("a")
-32
> 'T' + 0
84
> 'T' - 'A' == 't' - 'a' 
true