Grading done: 

  Exam 01 

  Labs 01, 02, 03, 04, 07

  Homework 01, 02

  Attendance: up to and including 07/03

To do this weekend: 

  Labs 05, 06, 07, 08 

  Homework 03, 04 

Also aiming for Homework 05 for Monday. 

--

I also will post assignments ahead. 

--

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> import java.util.Scanner;
> Scanner a = new Scanner("    Mary   had a   little      lamb.   ");
> a.hasNext()
true
> a.next()
"Mary"
> a
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=8][match valid=true][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
> a.hasNext()
true
> a.next()
"had"
> a.hasNext()
true
> a.next()
"a"
> a.next()
"little"
> a.hasNext()
true
> a.next()
"lamb."
> a.hasNext()
false
> a.next()
java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)


--

  mahazuga: Mary Ann
* serepate: Serena
* hgarciah: Henri
* balbakr: Bakr
* ruifan: Rui
* mzelenin: Matthew
* jnp2: Jay
* zeyang: Zejun
  zhang486: Jingyun
* yiwecao: Yiwei
* rthammon: Ryan
* wang686: Jiaxing
* dweissma: Andrew
* kevcao: Kevin
* ssalmero: Salmeron, Santiago (TA)
* luo23: Yawen
* runxzhao: Runxia
* dgerman: German, Dan-Adrian (Primary Instructor)
  creba: Chris

--

public class Runxia {
  public static void main(String[] args) {
    System.out.println( java.util.Arrays.toString( args ));  
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> java Runxia 0 0 0 1 0 1 0 0 P
[0, 0, 0, 1, 0, 1, 0, 0, P]


----

import java.util.*; 

public class Runxia {
  public static void main(String[] args) {
    ArrayList<String> input = new ArrayList<String>(); 
    Scanner in = new Scanner(System.in);
    System.out.print("Enter command: ");
    String cmd = in.nextLine(); 
    input.add(cmd);
    while (true) {
      System.out.print("Enter command: ");
      cmd = in.nextLine(); 
      if (cmd.equals("bye")) break; 
      else input.add(cmd);
    }
    System.out.println( input ); 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> run Runxia
Enter command:  [DrJava Input Box]
Enter command:  [DrJava Input Box]
Enter command:  [DrJava Input Box]
Enter command:  [DrJava Input Box]
Enter command:  [DrJava Input Box]
Enter command:  [DrJava Input Box]
Enter command:  [DrJava Input Box]
Enter command:  [DrJava Input Box]
Enter command:  [DrJava Input Box]
Enter command:  [DrJava Input Box]
[0, 0, 0, 1, 0, 1, 0, 0, P]


--

Here's another one: 

import java.util.*; 

public class Runxia {
  public static void main(String[] args) {
    while (true) { 
      ArrayList<String> input; 
      Scanner in = new Scanner(System.in);
      System.out.print("Enter sequence of commands: ");
      String cmd = in.nextLine(); 
      if ("bye".equals(cmd)) break;
      input = new ArrayList<String>(); 
      Scanner stapler = new Scanner(cmd); 
      while (stapler.hasNext())
        input.add(stapler.next()); 
      System.out.println("You have entered: " + input); 
    }
  }
}

Here's how this works:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> run Runxia
Enter sequence of commands:  [DrJava Input Box]
You have entered: [a, b, c, d]
Enter sequence of commands:  [DrJava Input Box]
You have entered: [0, 0, 0, 1]
Enter sequence of commands:  [DrJava Input Box]
> run Runxia
Enter sequence of commands:  [DrJava Input Box]
You have entered: []
Enter sequence of commands:  [DrJava Input Box]
You have entered: [0, 0, 0, 1, P]
Enter sequence of commands:  [DrJava Input Box]
You have entered: [D, R]
Enter sequence of commands:  [DrJava Input Box]
> run Runxia
Enter sequence of commands:  [DrJava Input Box]
> run Runxia
Enter sequence of commands:  [DrJava Input Box]
You have entered: [w, h, a, t, i, s, g, o, i, n, g, o, n, ?]
Enter sequence of commands:  [DrJava Input Box]


--

So now we have three questions:

(a) how do we sort?

(b) what is constructor chaining?

(c) what is event handling and how do we use it?

--

(a) 

// https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

import java.util.ArrayList; 
import java.util.Collections; 

public class Student implements Comparable<Student> {
  public int compareTo(Student other) {
    if (this.age < other.age) return -1; // I am younger I go first 
    else if (this.age > other.age) return 1; 
    else return 0; 
  }
  private String name;
  private int age; 
  private double gpa; 
  public Student(String name, int age, double gpa) {
    this.name = name;
    this.age = age;
    this.gpa = gpa; 
  }
  public String toString() {
    return name + "/" + age;  
  }
  public static void main(String[] args) {
    ArrayList<Student> a = new ArrayList<Student>(); 
    a.add(new Student("Lukas"  , 10, 3.2)); 
    a.add(new Student("Laura"  , 10, 3.2)); 
    a.add(new Student("Larry"  , 10, 3.2)); 
    a.add(new Student("Lynn"   ,  9, 3.2)); 
    a.add(new Student("Leslie" , 10, 3.2));     
    System.out.println( a ); 
    Collections.sort( a ); 
    System.out.println( a ); 
  }
}

This produces:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> run Student
[Lukas/10, Laura/10, Larry/10, Lynn/9, Leslie/10]
[Lynn/9, Lukas/10, Laura/10, Larry/10, Leslie/10]


Now I make a change: 

// https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html

import java.util.ArrayList; 
import java.util.Collections; 

public class Student implements Comparable<Student> {
  public int compareTo(Student other) {
    if (this.age < other.age) return -1; // I am younger I go first 
    else if (this.age > other.age) return 1; 
    else return this.name.compareTo(other.name); 
    // https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
  }
  private String name;
  private int age; 
  private double gpa; 
  public Student(String name, int age, double gpa) {
    this.name = name;
    this.age = age;
    this.gpa = gpa; 
  }
  public String toString() {
    return name + "/" + age;  
  }
  public static void main(String[] args) {
    ArrayList<Student> a = new ArrayList<Student>(); 
    a.add(new Student("Lukas"  , 10, 3.2)); 
    a.add(new Student("Laura"  , 10, 3.2)); 
    a.add(new Student("Larry"  , 10, 3.2)); 
    a.add(new Student("Lynn"   ,  9, 3.2)); 
    a.add(new Student("Leslie" , 10, 3.2));     
    System.out.println( a ); 
    Collections.sort( a ); 
    System.out.println( a ); 
  }
}

Here's the output: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> run Student
[Lukas/10, Laura/10, Larry/10, Lynn/9, Leslie/10]
[Lynn/9, Larry/10, Laura/10, Leslie/10, Lukas/10]


(b) 

public class Horse {
  protected String name;  
  public Horse(String name) {
    this.name = name;  
  }
  public void talk() {
    System.out.println( "Hi, I am " + this.name );  
  }
  public static void main(String[] args) {
    Horse a = new Horse("Larry");  
    a.talk();
  }
}

public class Unicorn extends Horse {
  public Unicorn(String name) {
    super(name);  
  }
  public void talk() {
    System.out.println("Je m'appelle " + this.name);  
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> Horse a = new Horse("Larry")
> a.talk()
Hi, I am Larry
> Horse b = new Unicorn("Misty");
> b.talk()
Je m'appelle Misty


(c) 

import javax.swing.JFrame; 

public class One {
  public static void main(String[] args) {
    JFrame a = new JFrame("Mouse events..."); 
    a.setVisible(true); 
    a.setSize(300, 500); 
  }
}

--

import javax.swing.JFrame; 

public class One {
  public static void main(String[] args) {
    JFrame a = new JFrame("Mouse events..."); 
    a.setVisible(true); 
    a.setSize(300, 500); 
    a.addMouseMotionListener( new Listener() ); 
  }
}

import java.awt.event.MouseMotionListener; 
import java.awt.event.MouseEvent; 

public class Listener implements MouseMotionListener {
  public void mouseMoved(MouseEvent e) { 
    int x = e.getX(), y = e.getY();
    System.out.println("(" + x + ", " + y + ")"); 
  } 
  public void mouseDragged(MouseEvent e) { 
    System.out.println("Mouse dragged.");
  } 
}

--

Typical output:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop\07-05-2018
> run One
(291, 393)
(285, 391)
(280, 390)
(274, 388)
(269, 387)
(265, 385)
(250, 380)
(247, 379)
(243, 379)
(240, 379)
(237, 377)
(219, 371)
(216, 371)
(214, 370)
(212, 369)
(211, 369)
(210, 368)
(207, 365)
(206, 365)
(205, 363)
(201, 361)
(198, 359)
(188, 346)
(188, 343)
(187, 342)
(186, 339)
(185, 337)
(183, 334)
(177, 315)
(177, 313)
(177, 311)
(177, 308)
(177, 306)
(179, 293)
(181, 292)
(181, 291)
(183, 287)
(184, 286)
(184, 285)
(191, 275)
(193, 272)
(195, 270)
(196, 268)
(197, 267)
(212, 256)
(215, 255)
(216, 253)
(219, 253)
(220, 252)
(221, 250)
(227, 247)
(228, 247)
(229, 246)
(230, 245)
(232, 243)
(245, 230)
(246, 229)
(247, 228)
(248, 225)
(249, 224)
(249, 223)
(249, 221)
(249, 219)
(248, 217)
(247, 216)
(246, 214)
(233, 200)
(228, 196)
(226, 194)
(224, 194)
(221, 191)
(218, 189)
(203, 180)
(201, 178)
(199, 178)
(198, 177)
(197, 176)
(190, 172)
(189, 172)
(188, 172)
(186, 172)
(185, 172)
(184, 172)
(171, 176)
(169, 176)
(167, 178)
(166, 178)
(164, 179)
(161, 180)
(160, 180)
(159, 180)
(159, 181)
(158, 182)
(157, 182)
(139, 193)
(135, 197)
(130, 199)
(126, 200)
(124, 203)
(118, 208)
(118, 210)
(117, 211)
(116, 211)
(116, 213)
(115, 214)
(107, 224)
(106, 225)
(106, 226)
(106, 227)
(106, 228)
(104, 232)
(104, 233)
(103, 233)
(102, 233)
(102, 235)
(101, 235)
(101, 239)
(101, 241)
(101, 242)
(101, 243)
(101, 245)
(101, 250)
(102, 251)
(102, 252)
(103, 252)
(103, 253)
(103, 254)
(105, 256)
(105, 257)
(106, 257)
(107, 258)
(108, 259)
(112, 261)
(113, 262)
(114, 262)
(115, 262)
(116, 262)
(117, 262)
(120, 262)
(121, 262)
(122, 262)
(123, 263)
(124, 263)
(128, 263)
(130, 263)
(132, 263)
(134, 263)
(136, 263)
(137, 262)
(142, 259)
(143, 259)
(144, 258)
(144, 257)
(145, 257)
(152, 254)
(154, 253)
(156, 253)
(156, 252)
(157, 252)
(158, 252)
(159, 251)
(160, 251)
(161, 251)
(162, 251)
(163, 251)
(163, 254)
(163, 255)
(163, 256)
(163, 257)
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
Mouse dragged.
(139, 251)
(138, 242)
(138, 241)
(138, 239)
(138, 238)
(138, 237)
(138, 227)
(138, 225)
(136, 223)
(136, 221)
(135, 218)
(134, 215)
(78, 168)
(74, 163)
(69, 157)
(66, 153)
(64, 148)
(62, 145)
(61, 145)
(60, 144)
(58, 143)
(57, 142)
(55, 141)
(49, 137)
(49, 136)
(48, 136)
(47, 134)
(47, 133)
(44, 124)
(44, 122)
(44, 119)
(44, 117)
(44, 116)
(44, 113)
(43, 104)
(40, 99)
(37, 92)
(36, 91)
(36, 89)
(35, 87)
(34, 86)
(32, 85)
(32, 83)
(29, 83)
(27, 81)
(19, 75)
(18, 75)
(18, 74)
(17, 74)
(16, 73)
(13, 72)
(13, 71)
(13, 73)
(13, 74)
(13, 76)
(12, 76)
(9, 76)
(8, 76)
(9, 76)
(12, 76)
(16, 77)
(68, 102)
(70, 110)
(71, 118)
(71, 112)
(71, 106)
(70, 99)
(59, 76)
(57, 75)
(56, 75)
(55, 75)
(54, 75)
(52, 75)
(52, 74)
(51, 74)
(49, 74)
(47, 72)
(46, 71)
(42, 68)
(42, 67)
(41, 66)
(40, 65)
(39, 64)
(36, 61)
(36, 60)
(35, 59)
(34, 59)
(34, 58)
(33, 57)
(32, 55)
(31, 54)
(30, 54)
(30, 53)
(29, 53)
(28, 52)
(27, 50)
(26, 50)
(26, 49)
(25, 49)
(25, 48)
(23, 46)
(23, 45)
(23, 44)
(22, 43)
(22, 42)
(21, 41)
(21, 40)
(21, 39)
(20, 39)
(19, 38)
(19, 37)
(18, 37)
(17, 37)
(16, 37)
(15, 37)
(14, 37)
(13, 37)
(13, 36)
(13, 35)
(12, 35)
(12, 34)
(11, 34)
(10, 34)
(10, 33)
(10, 32)
(11, 32)
(12, 32)
(13, 32)
(56, 35)
(64, 42)
(70, 51)
(74, 58)
(77, 64)
(74, 79)
(70, 79)
(63, 78)
(53, 75)
(41, 69)
(11, 49)
(257, 32)
(251, 36)
(246, 39)
(242, 42)
(240, 44)
(239, 46)
(241, 46)
(243, 46)
(245, 46)
(248, 45)
(262, 36)
(263, 36)
(264, 35)
(265, 35)
(265, 34)
(266, 33)
(267, 31)


--