Exercise: 

  String plum = "coconut"; // plum is a String 
  System.out.println((char)('Q' - plum.length())); 
                             81 - 7
                     (char) 74

                     'J' 
How do we know:

  if we know that 'A' has code 65 

  what is the value of 'Q' ?

  6    7         8
  ABCDEFGHIJKLMNOPQ                             
  56789012345678901
   
  it appears that 'Q' is 81

So that's how you do it.

There are two sample exams posted please look over them.

Let's work out another exercise:

  int i = 3, j = 5, a;

  a = i++ + j++;

  System.out.println( "(" + i + ", " + j + ", " + a + ")" ); 

Experiments: 

> int i
> int j
> int a
> i = 3
3
> j = 5
5
> i
3
> j
5
> i++
3
> i
4
> j
5
> j = j + 1
6
> j
6
> j++
6
> j
7
> i
4
> ++i
5
> i
5
> i++
5
> i
6
> ++i
7
> i
7
>

Let's try the problem again:

  int i = 3, j = 5, a;

  a = i++ + j++;
  //  3
  // and i becomes 4 
  //        5
  //        and j becomes 6       
  //  3 + 5
  // so a becomes 8 

  System.out.println( "(" + i + ", " + j + ", " + a + ")" ); 

  // (4, 6, 8) 

We try this in DrJava

>   int i = 3, j = 5, a;

  a = i++ + j++;
  //  3
  // and i becomes 4
  //        5
  //        and j becomes 6
  //  3 + 5
  // so a becomes 8

  System.out.println( "(" + i + ", " + j + ", " + a + ")" );

  // (4, 6, 8)

(4, 6, 8)


So we were right. 

Let's start modeling with classes:

class Something {
  
}

With this I can already create objects: 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 4 * 3
12
> Something a;
> a = new Something();
> a
Something@bd88626


Let's model something else:

class Dog {
   
}

Now we can create some instances of Dog objects:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Dog a, b;
> a = new Dog();
> b = new Dog();
> a
Dog@25d9f872
> b
Dog@734ffb78


Let's increase the complexity of our modeling:

class Student {
   String name;
   int age;
}

The file is Student.java and we compile it and we have:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Student a, b;
> a = new Student(); 
> b = new Student();
> a
Student@3f4deef5
> b
Student@351e3f2e
> a.name = "Laura";
> a.age = 8;
> b.name = "Alex";
> b.age = 7;
> a
Student@3f4deef5
> b
Student@351e3f2e
> a.name
"Laura"
> a.age
8
> b.name
"Alex"
> b.age
7
> a.age = a.age + 1;
> a.name
"Laura"
> a.age
9


I am asking myself: can these objects do something for me?

The answer is: yes. 

class Student {
   String name;
   int age;
   public void talk() {
     System.out.println(this);  
   }
}

I added an instance method that prints the keyword this. 

What is this? It is a way for any object to refer to itself. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Student a, b;
> a
null
> b
null
> a = new Student()
Student@3ee7db8d
> a
Student@3ee7db8d
> a.talk()
Student@3ee7db8d


So you see that they're able to identify themselves.

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Student a, b;
> a = new Student();
> b = new Student();
> a
Student@1db93d5f
> b
Student@3ac81f2c
> a.talk()
Student@1db93d5f
> b.talk()
Student@3ac81f2c


Two object, different addresses, each one knows its distinct identity. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Student a = new Student();
> a.name
null
> a.name = "Larry"
"Larry"
> a.name
"Larry"


Based on this we can try:

class Student {
   String name;
   int age;
   public void talk() {
     System.out.println(this.name);  
   }
}

Here's how this behaves:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Student a, b;
> a = new Student();
> b = new Student();
> a.talk()
null
> a.name
null
> a.name = "Jordan";
> a.talk()
Jordan
> a.name
"Jordan"


I can make this method a bit more verbose:

class Student {
   String name;
   int age;
   public void talk() {
     System.out.println("My name is " + this.name + " and I am " + this.age + " years old.");  
   }
}

With this I can do the following:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Student a, b;
> a = new Student();
> a.talk()
My name is null and I am 0 years old.
> a.name = "Leslie";
> a.age = 5;
> a.talk()
My name is Leslie and I am 5 years old.
> b
null
> b = new Student();
> b.talk()
My name is null and I am 0 years old.


Finally let's see if we can customize the objects as we create them:

class Student {
   String name; // instance variable (default value is: null)
   int age; // instance variable (default value is: 0)
   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 + " years old.");  
   }
}

How does this work:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Student a = new Student("Jonah", 3)
> a
Student@4450aa75
> a.talk()
My name is Jonah and I am 3 years old.


Exercise for today: 

  design a class Point to model points in the plane

  each Point object has an x and a y (integer coordinates) 

  I want Point objects to have a constructor that sets x and y

  each Point should be able to determine its distance to another Point

Answer:

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); 
  }
}

Example: 

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


--