Howdy.

What does this print?  

String plum = "coconut";
System.out.println(plum.substring(0, "plum".length()));

The answer should be: 

coco

plum.substring(0, 4)

"coconut".substring(0, 4)
 0123456
 coco

Let's now do this:

  int i = 3, j = 5, a;
  // i is 3 j is 5 and a has no value (yet) 
  a = i++ + j++;
  // 
  System.out.println( "(" + i + ", " + j + ", " + a + ")" ); 


Let's examine this:

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


So let's look at the problem again:

  int i = 3, j = 5, a;

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

So here's the proof:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> int i = 6
> i
6
> i += 6
12
> i = i + 6
18
>   int i = 3, j = 5, a;

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

(4, 6, 8)
> ++i + j++ 
11
> i
5
> j
7


Please go and work out Homework Two and Lab Three in MPL. 

Now let's start discussing modeling with classes:

class Something {
   
}

This goes into a file Something.java and with it:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> new Something()
Something@70bb2d4a
> 4
4
> int n = 4
> n
4
> Something a;
> a = new Something();
> a
Something@764b36f7


So let us try to model something else: 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Dog a, b;
> a
null
> b
null
> a = new Dog();
> a
Dog@700e3e41
> b
null
> b = new Dog();
> b
Dog@7090fb7b


Why don't we try to model something more familiar: 

class Student {
   String name;
   int age;
}

This works as follows:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Student a, b;
> a
null
> a = new Student()
Student@a5ca180
> a.name
null
> a.age
0


Let's see if we can teach Student objects to do something for us:

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

class Student {
   String name;
   int age;
   public void talk() {
      System.out.println("There's a snake in my boot.");  
   }
}

Let's see how this works:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Student a, b;
> a = new Student();
> a
Student@1c328220
> b
null
> a.talk()
There's a snake in my boot.
> b
null
> b.talk()
java.lang.NullPointerException
> b = new Student()
Student@1de0b573
> b.talk()
There's a snake in my boot.
> b
Student@1de0b573


Now let's change the ta;lk method a bit:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Student a, b;
> a = new Student()
Student@6e8b39a6
> a
Student@6e8b39a6
> a.talk()
Hi this is Student@6e8b39a6 talking.
> a.name 
null
> a.name = "Laura"
"Laura"
> a.age = 6
6
> a
Student@6e8b39a6
> a.talk()
Hi this is Student@6e8b39a6 talking.
> a.name
"Laura"
> a.age
6
> a.age += 1
7
> a.age
7
> a.age += 5
12
> a.age
12
> b = new Student()
Student@dcca3e5
> b
Student@dcca3e5
> a
Student@6e8b39a6
> b.talk()
Hi this is Student@dcca3e5 talking.
> a.talk()
Hi this is Student@6e8b39a6 talking.
> a.name
"Laura"
> a.age
12
> b.name
null
> b.age
0
> b.name = "Larry"
"Larry"
> b.name
"Larry"
> b.age
0
> a.name
"Laura"
> a.age
12


So this is a keyword that objects use to refer to themselves. 

class Student {
   String name; // instance variable 
   int age; // instance variable 
   public void talk() { // instance method 
      System.out.println("Hi this is " + this.name + " talking.");  
   }
}

This is how this runs:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Student a, b;
> a = new Student()
Student@7d68fb6c
> a.talk()
Hi this is null talking.
> a.name = "Leslie"
"Leslie"
> a.talk()
Hi this is Leslie talking.
> b = new Student()
Student@498efce3
> b.name = "Alex"
"Alex"
> b.talk()
Hi this is Alex talking.
> a.talk()
Hi this is Leslie talking.


Let's make the model more complex by allowing for custom initialization:

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("My name is " + this.name + " and I am " + this.age);  
   }
}

Here's an example of this class in action: 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Student a, b;
> a
null
> b
null
> a = new Student("Tim", 3); 
> a.name
"Tim"
> a.age
3
> a.talk()
My name is Tim and I am 3
> b.talk()
java.lang.NullPointerException
> b
null
> b = new Student("Toni", 12)
Student@6d02460e
> b
Student@6d02460e
> b.talk()
My name is Toni and I am 12
> b.name
"Toni"
> b.age
12
> a.talk()
My name is Tim and I am 3


Exercise: 

  model a Point in plane (on the screen)

  every Point must have an x and a y

  coordinates on the screen are whole numbers

  Point objects should be created by specifying their coordinates

  every Point object should be able to determine the distance to 
                                              any other Point object

Answer:

class Point {
  int x, y; 
  Point(int x, int y) {
    this.x = x;
    this.y = y;
  }
  public double distanceTo(Point autre) { // other in French
     int dx = this.x - autre.x;
     int dy = this.y - autre.y;
     return Math.sqrt( dx * dx + dy * dy ); 
  }
}


Let's see it in action:


Welcome to DrJava.  Working directory is C:\Users\dgerman
> Point a, b;
> a = new Point(3, 0);
> b = new Point(0, 4);
> a.distanceTo(b)
5.0
> Point c = new Point(1, 1);
> c.distanceTo( new Point( 0, 0 ) )
1.4142135623730951


See you on Wednesday or in office hours. 

--