8:00 AM 3/10/2014

Java programs are made out of classes.

A class is a container. 

It contains a blueprint also static members. 

By member I mean method or variable.  

http://www.cs.indiana.edu/classes/c212/spr2011/notes/labNotes04.html

If you instantiate a blueprint you get an object. 

A blueprint is a vision. You can have 0, 1 or more objects.

Objects are real. Instantiations of the blueprint. 

A Scanner is a Bird and does work for you. The object. Not the class. 

By designing a class I am modeling.

A model is a simplification. Your program is like a simulation. 

No doubt you can do a lot without modeling.

A lot of static features are useful: Integer.parseInt(...) 

But the essence you cannot escape is designing objects. 

Create a class: I am modeling. 

The second stage (chapter 9) is called the class extension mechanism.

This is a way to model in stages. 



class Horse { // class 
  String name; // instance variable 
  Horse(String name) { // constructor 
    this.name = name; 
  }
  void talk() { // instance method 
    System.out.println( "Howdy I'm " + this.name ); 
  }
}


class Unicorn extends Horse { // all the features from Horse are here 
  Unicorn(String name) {
    super(name);
  } 
  // by inheritance a Unicorn talks 
  void sing() {
    System.out.println( " That's me in the (uni)corner... "); 
  } 
}


class Program {
  public static void main(String[] args) {
    // Horse a = new Horse(); // default constructor no longer there  
    Horse a = new Horse("Morning Glory"); 
    a.talk(); 
    // Unicorn b = new Unicorn(); // won't work because constructor chaining     
    Unicorn b = new Unicorn("Rain"); 
    b.talk(); 
    b.sing(); 
    Horse c = new Unicorn("Rainbow"); // polymorphism 
    c.talk(); 
    ((Unicorn)c).sing();    
  }
}

Classes are used for modeling

Constructors used for initialization of objects' variables. 

You can model in stages (class extension mechanism)

This brings up: 

  (a) inheritance 



  (b) polymorphism 

  (c) constructor chaining

http://www.cs.indiana.edu/classes/a201-dger/sum2002/notes/TwentyEight.html

  (d) dynamic method lookup 

Horse = { talk() }

Unicorn = Horse U { sing() } = ...{ talk() } U { sing() } = { talk(), sing() }

But what if I say: 

Unicorn = Horse U { talk(), sing() }

How many ways can a Unicorn talk? 

  (e) shadowing of variables 

What is a Point? 

What is a Line? 

What is a Triangle? 

http://en.wikipedia.org/wiki/Heron%27s_formula

What is a Circle? 

What is a Rectangle? 

Is it easier to teach Rectangles to determine if they overlap compared with Circles? 

What is a Fraction? 

What is BlueJ? 

http://www.bluej.org/download/files/bluej-311.jar

What is Greenfoot? 

http://www.greenfoot.org/download

http://www.greenfoot.org/book

https://www.cs.indiana.edu/classes/c212-dgerman/spr2014/gfmk.pdf

http://3.bp.blogspot.com/-fScHHxYcpMo/UY7VpUWQsZI/AAAAAAAABw0/hvApzKVYXk4/s1600/NORTHERN-FLYING-SQUIRREL-3.jpg

http://mhga.ca/phpWebSite/images/MHGA/awesome/hang-gliding-004.jpg

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


--

class Horse {
    String name;
    Horse(String name) {
        this.name = name;
    }
}

class Unicorn extends Horse {
    Unicorn(String name) {
        super(name);
    }
}


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

class Line {
    Point a, b;
    Line(Point a, Point b) {
        this.a = a;
        this.b = b;
    }
    double length() {
        return this.a.distanceTo(this.b);
    }
}

9:27 AM 3/10/2014

Java programs are made out of classes. 

Classes are containers.

They contain members (methods and variables). 

Classes contain blueprint (vision). 

http://s6.thisnext.com/media/largest_dimension/C0C80083.jpg

http://www.cs.indiana.edu/classes/c212/spr2011/notes/labNotes04.html

http://www.cs.indiana.edu/classes/c212/fall2010/notes/lectureSix.ppt

Designing a class means modeling. 

Let's design a Horse:

class Horse {

}


In Java we can model in stages:

class RockingHorse extends Horse {
  Rockers r; 
}

You can add wings, horn, rockers and get something new out of it. 

What is BlueJ? 

What is Greenfoot? 

The class extension mechanism:

   inheritance

   polymorphism

   dynamic method lookup 

   shadowing of variables 

   constructor chaining 

Interfaces and abstract classes will also be important. 

What is a Point?

What is a Line? 

What is a Triangle? How can it calculate its area?

What is a Circle? 


class Circle {
      Point center;
      int radius;
      boolean overlap(Circle other) {
          ...
      }
}