Jiaxing Henri  Ryan Matthew Andrew Serena Zejun    Mary-Ann Yawen
Bakr    Runxia Rui  Kevin   Yiwei  Adrian Santiago Jingyun


Exam 01: testing that you can read Java (1, 2, 3, 4, 5, 6).

Exam 02: testing you can write Java

Exam 03: project readiness (event-handling, etc.)

Exam 04: final exam 

More sample exams to be posted, but you have one already. 

Tonight I will grade everything. 

A Study Guide will be posted Tue night. 

Read the book! Then focus on review, study guide. 

More assignments will be posted to help with Exam 01 preparation. 

Java programs are made out of classes. 

public class Point {
  

  public static void main(String[] args) {
    Point a, b; 
    a = new Point(3, 2); 
    b = new Point(1, 4); 
    System.out.println( a.distanceTo( b ) ); 

    System.out.println( a.distanceTo( Point.origin ) ); 

    System.out.println( Point.origin.distanceTo( b ) ); // System.out.println(...); 

    // what type does System.out have? 
    
  }
}

https://i2.wp.com/www.mechlectures.com/wp-content/uploads/2017/08/seven-habits-of-highly-effective-people_540b157e2f9da_w1500.jpg

https://thumbs.dreamstime.com/z/habits-combining-knowledge-desire-skills-good-53878929.jpg

Some thoughts:

  (1) System.out does not have type System

  Welcome to DrJava.  Working directory is C:\Users\dgerman
  > System.out
  java.io.PrintStream@2f4fe980
  >  

  (2) You can look it up

  https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#out

  (3) You can read about it in the book, older sample exams 

Let's start developing class Point:

(a) recall if you have no constructors you get one default no-arg constructor 

(b) if you have at least one cionstructor you are no longer in the default case 

public class Point {
  public static Point origin = new Point(0, 0); 
  private int x, y; // code outside class Point can't access/modify these 
  public Point(int x, int y) {
    this.x = x; 
    this.y = y;
  }
  public String report() {
    return "P(" + this.x + ", " + this.y + ")";  
  }
  public double distanceTo(Point other) {
    int dx = this.x - other.x;
    int dy = this.y - other.y;
    return Math.sqrt( Math.pow(dx, 2) + Math.pow(dy, 2) ); 
  }
  public static void main(String[] args) { // unit testing 
    Point a, b; 
    a = new Point(3, 2); 
    System.out.println( a.report() ); // expected: P(3, 2)
    b = new Point(1, 4); 
    System.out.println( b.report() ); // expected: P(1, 4)
    System.out.println( a.distanceTo( b ) ); // 2 * 1.4142... (something like 2.8284...)
    System.out.println( a.distanceTo( Point.origin ) ); // expected: Math.sqrt(13) or 3.605551275463989
    System.out.println( Point.origin.distanceTo( b ) ); // System.out.println(...); 
    // expected: Math.sqrt(17) 
    // what type does System.out have? 
    System.out.println( System.out ); // expected: java.io.PrintStream@98b050e 
    // book, exams, online API: 
    // https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#out
  }
}

Intermission. 

In Java we have: 

  primitive types (int, double, char, boolean) 

  reference types -- classes (also known as user defined types)

Values of reference types are objects. 

Some examples of reference types: 

  Scanner

  BigDecimal 

  Point

  BankAccount 

  JFrame 

  JComponent

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

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Horse()
> a.talk()
Howdy.


https://i5.walmartimages.com/asr/1ed3e6ec-792e-43e0-b1a9-787f8fec18b0_1.ac88b6fa8a1a4ea4cf4fdc9ad9b068cd.jpeg?odnHeight=450&odnWidth=450&odnBg=FFFFFF

Horse = { talk() }

Unicorn = Horse + { horn } = { talk(), horn } 

public class Unicorn extends Horse {
   
}

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


Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Unicorn a = new Unicorn()
> a
Unicorn@11ab508e


Here's polymorphism in action: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Unicorn()
> a
Unicorn@3da22dd1


This is inheritance:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Unicorn a = new Unicorn()
> a.talk()
Howdy.


But this doesn't work:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Unicorn a = new Horse()
Static Error: Bad types in assignment: from Horse to Unicorn


public class Unicorn extends Horse {
  public void sing() {
    System.out.println("That's me in the (uni-)corner..."); 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Unicorn()
> a.talk()
Howdy.
> a.sing()
Static Error: No method in Horse has name 'sing'
>

... but this is possible: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Unicorn()
> a.talk()
Howdy.
> a.sing()
Static Error: No method in Horse has name 'sing'
> ((Unicorn)a).sing()
That's me in the (uni-)corner...


public class Unicorn extends Horse {
  public void talk() {
    System.out.println("Bonjour...");  
  }
  public void sing() {
    System.out.println("That's me in the (uni-)corner..."); 
  }
}

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Horse a = new Horse()
> a.talk()
Howdy.
> Unicorn b = new Unicorn()
> b.sing()
That's me in the (uni-)corner...
> b.talk()
Bonjour...
> Horse c = new Unicorn()
> c
Unicorn@40852ab7
> c.talk()
Bonjour...


That completely explains the graphics exercises at the end of chapter 2, 3.

Now you need to focus on chapters 4, 5, 6 for the exam. 

--

Good points/questions by Runxia shortly after class ended: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> (new Unicorn())
Unicorn@4bdc3678
> (new Unicorn()).sing()
That's me in the (uni-)corner...
> Unicorn a = new Unicorn()
> a.sing()
That's me in the (uni-)corner...
> a.talk()
Bonjour...
> Horse b = new Unicorn()
> b
Unicorn@2a81b414
> b.sing()
Static Error: No method in Horse has name 'sing'
> ((Unicorn)b).sing()
That's me in the (uni-)corner...
> Unicorn c = new Unicorn()
> c.sing()
That's me in the (uni-)corner...
> Horse d = a;
> a.sing()
That's me in the (uni-)corner...
> d.sing()
Static Error: No method in Horse has name 'sing'


https://www.indystar.com/story/sports/college/indiana/2017/12/18/former-iu-star-yogi-ferrell-makes-fashion-statement/960695001/

https://www.gannett-cdn.com/-mm-/9bf0a780f19777f2f0a10acb4a8da69a9c03dbbc/c=109-0-1811-1280&r=x393&c=520x390/local/-/media/2017/12/18/INGroup/Indianapolis/636491922444712342-Grungy-Gentleman-x-Yogi-Ferrell-66.jpg

https://www.google.com/search?q=height+yogi+ferell&rlz=1C1GGRV_enUS803US803&oq=height+yogi+ferell&aqs=chrome..69i57j0l5.4319j1j7&sourceid=chrome&ie=UTF-8

... he looks like a student (horse) but is in fact a unicorn (student athlete)... 

Also:

import java.awt.Point;

public class Math {
  public static void main(String[] args) {
    System.out.println( java.lang.Math.max( 2, 3 ) );  
  }
}

We need to talk about packages as namespaces. 

--