Howdy. 

Exam next week study MPL but also sample exams from the past.

  String tsew = "stue".charAt(0) + "tsue".charAt(0) + "ew";
  //            's'              + 't'              + "ew"
  //            "stew"
  System.out.println(tsew); // produces "stew" ?

Apparently this doesn't produce "stew" instead it produces 231ew so now we ask:

  (a) why? 

  because 'A' + 0 produces 65 whereas 'A' + "0" produces "A0"

  (b) how can you still get "stew"

  String tsew = "stue".charAt(0) + ( "tsue".charAt(0) + "ew" );
  //            's'              + ( 't'              + "ew" )
  //            "stew"
  System.out.println(tsew); // produces "stew" ?

Here's why:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> 'a' + 't'
213
> 'a' + "t"
"at"


Alternative exercise:

  Prove that there's a tomato in every automaton. 

Ben, first: 

  --

"automaton".substring(2, 8)--> "tomato" 
 012345678


System.out.println( (char) 81 ); 

Or this experiment:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int a = 81
> a
81
> (char) a
'Q'


Second exercise:

  int i = 3 , j = 5, a;
  // i is 3 and j is 5 and a is undefined 
  a = i++ + j++;
  //  3 and i becomes 4
  //        5 and j becomes 6
  //  3   + 5 
  System.out.println("(" + i + ", " + j + ", " + a + ")"); 
  //                  (    4    ,     6    ,     8  )

Here's why: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> int i = 3;
> i
3
> i
3
> i++
3
> i
4
> i++
4
> i
5
> i
5
> int j = 5
> j
5
> i
5
> i++ + ++j
11
> i
6
> j
6


Next let's start chapter 4:

class Something {
  
}

I save it in Something.java and can do this with it:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> new Something()
Something@13c4b81
> 4
4
> int n = 5;
> n
5
> n
5
> n
5
> n
5
> Something a;
> a
null
> a = new Something();
> a
Something@97cdfc
>

What if I try to model something more realistic.

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Dog a, b;
> a
null
> b
null
> a = new Dog();
> a
Dog@20a25
> b = new Dog()
Dog@6c5994
> b
Dog@6c5994


Perhaps I can be more specific in my modeling:

class Student {
  String name; // instance variable
  int age; // instance variable
}

Examples: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Student a, b;
> a
null
> b
null
> a = new Student()
Student@1e81cc9
> a.name
null
> a.age
0
> b = new Student()
Student@1520844
> b
Student@1520844
> b.name
null
> a.name = "Leslie"
"Leslie"
> a.name
"Leslie"
>

http://www.cs.indiana.edu/classes/a202-dger/sum2010/zo.pdf

Add instance methods to the description:

public class Student {
  String name; // instance variable
  int age; // instance variable
  public void talk() { // instance methods 
    System.out.println("Howdy.");  
  }
}

Here's how this works:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Student a = new Student()
> a.talk()
Howdy.
> Student b = new Student()
> a
Student@1f06324
> b
Student@196cd38
> a.talk()
Howdy.
> b.talk()
Howdy.


Can they distinguish themselves from the inside? 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Student a, b;
> a
null
> b
null
> a = new Student()
Student@1e80e4d
> a.talk()
Hi, I am Student@1e80e4d
> a.name
null
> a.name = "Laura"
"Laura"
> a.name
"Laura"


I can change the code as follows: 

public class Student {
  String name; // instance variable
  int age; // instance variable
  public void talk() { // instance methods 
    System.out.println("Hi, I am " + this.name);  
  }
}

This now runs as follows:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Student a =new Student()
> a.talk()
Hi, I am null
> a.name = "Alex"
"Alex"
> a.talk()
Hi, I am Alex
>

I can customize object creation as follows:

public class Student {
  String name; // instance variable
  int age; // instance variable
  Student(String name, int age) { // constructor 
    this.name = name; 
    this.age = age;
  }
  public void talk() { // instance method
    System.out.println("Hi, I am " + this.name + " and I am " + this.age + " years old.");  
  }
}

Here's how it runs:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Student a, b;
> a
null
> b
null
> a = new Student("Laura", 4);
> a
Student@e4387d
> a.talk()
Hi, I am Laura and I am 4 years old.
> b = new Student("Bob", 5);
> b
Student@ae20b6
> b.talk()
Hi, I am Bob and I am 5 years old.


So these are all the elements of successful modeling in Java: 

  -- classes contain blueprints
  -- blueprints are comprised of all instance members
  -- instance members are: instance variables and instance methods
  -- object creation can be customized with constructors
  -- this is a keyword for objects to refer to themselves

Problem: 

  write a class that models Point objects in the plane

  a Point contains two coordinates (x and y) both whole numbers 

  make sure Points can be created with coordinates given by the user

  Point objects should be able to determine how far they are to other Point objects

Code: 

class Point {
  int x, y;
  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public double distanceTo(Point other) {
    int dx = this.x - other.x;
    int dy = this.y - other.y; 
    return Math.sqrt( dx * dx + dy * dy ); 
  }
  public static void main(String[] args) {
    Point a, b; 
    a = new Point(0, 3); 
    b = new Point(4, 0); 
    System.out.println( a.distanceTo( b ) ); // 5.0 
    System.out.println( a.distanceTo( a ) ); // 0.0 
    Point c = new Point(1, 1); 
    System.out.println( c.distanceTo( new Point(0, 0) ) ); 
  }
}

Here's what I get if I run this:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Point
5.0
0.0
1.4142135623730951


--