Thursday, 29 March 2012

Late Binding Lecture

Late Binding
When:
when a parent type reference varible holds a reference to a child type object.
e.g.
A refA2 = new B();
//refA2  is a reference to an object of class B
//A is a parent class
//B is a child class
What:
A parent reference varible can hold a reference to a child object
===
Another code example
class Burger{}
class ChickenBurger extends Burger{}
class BeefBurger extends Burger{}
class EggBurger extends Burger{}
class AlooBurger extends Burger{}
class DumDum{
 public void eatBurger(EggBurger eb){}
}
class Person{
 public void eatBurger(Burger b){}
}
Person p1 = new Person();
p1.eatburger(new ChickenBurger());
DumDum d1 = new DumDum();
//d1.eatBurger(new ChickenBurger());
===
Why:
to avoid creating multiple functions for operations of different objects derived from same parent class


 

Late Binding Code



A refA2 = new B();
//refA2  is a reference to an object of class B


Example :

class Burger { 
  public int mazaa(){
    return 0;
  }
}

class ChickenBurger extends Burger {
  public int mazaa(){
    return 100;
  }
}
class BeefBurger extends Burger {
    public int mazaa(){
    return 80;
  }
}
class EggBurger extends Burger {
    public int mazaa(){
    return 70;
  }
}
class AlooBurger extends Burger {
    public int mazaa(){
    return 50;
  }
  public String Speciality(){
    return "Pure Vegetarian";
  }
}
class DumDum {
  public void eatBurger(EggBurger eb) {   
    if (eb instanceof EggBurger)
      println("DumDum likes Egg burger");
  }
  public void eatBurger(ChickenBurger cb) {
    if (cb instanceof ChickenBurger)
      println("DumDum likes Chicken burger");
  }
}
class Person {
  public void eatBurger(Burger b) {
    String ans;
    if (b instanceof ChickenBurger)
      println("Enjoying chicken burger");
    else if (b instanceof BeefBurger)
      println("Enjoying Beef burger");
    else if (b instanceof EggBurger)
      println("Enjoying Egg burger");
    else if (b instanceof AlooBurger)
      println("Enjoying Aloo burger");
    else
      println("Enjoying SOME burger");
  }
}
void setup() {
  Person p1 = new Person();
  Burger b = new ChickenBurger(); 
  println(b.mazaa());
  println(b.Speciality());
  p1.eatBurger(b);
  DumDum d1 = new DumDum();
  d1.eatBurger(new EggBurger());
}
void draw() {
}

Wednesday, 28 March 2012

Early Binding

dial number
inform receiver
if receiver accepts
 inform sender to initiate
else
 inform sender to call later


Binding
Terminology
1 Refernce Variable
2 Reference which is actually an address
3 referred object
4 data type of reference variable
5 data type of referred object
What:
Binding is the association of reference with the reference variable
e.g.
class A{} // class definition
A refA (Reference variable A) = new A(); (Referred Object )
//refA  is a reference to an object of class A
class B extends A{} //class definition
B refB = new B();
//refB  is a reference to an object of class B

in the above code snippets, refA and refB are reference variables which are associated with their respective object in the memory.
the data type of reference variable and reference to an object must match
e.g. refA has a data type A and it contains a variable of type A (a class is a user defined data type)
===
All of the above is based on the concept called
Early Binding
What:
the reference variable and reference are both of the same data type.





Lecture 16 : Enumerated Loop 

For-each Loop


Purpose


The basic for loop was extended in Java 5 to make iteration over arrays and other collections more convenient. This newer for statement is called the enhanced for or for-each (because it is called this in other programming languages). I've also heard it called the for-in loop.

Use it in preference to the standard for loop if applicable (see last section below) because it's much more readable.

Series of values. The for-each loop is used to access each successive value in a collection of values.

Arrays and Collections. It's commonly used to iterate over an array or a Collections class (eg, ArrayList).

Iterable<E>. It can also iterate over anything that implements the Iterable<E> interface (must define iterator() method). Many of the Collections classes (eg, ArrayList) implement Iterable<E>, which makes the for-each loop very useful. You can also implement Iterable<E> for your own data structures.

General Form


The for-each and equivalent for statements have these forms. The two basic equivalent forms are given, depending one whether it is an array or an Iterable that is being traversed. In both cases an extra variable is required, an index for the array and an iterator for the collection.

For-each loopEquivalent for loop
for (type var : arr) {
    body-of-loop
}
for (int i = 0; i < arr.length; i++) { 
    type var = arr[i];
    body-of-loop
}
for (type var : coll) {
    body-of-loop
}
for (Iterator<type> iter = coll.iterator(); iter.hasNext(); ) {
    type var = iter.next();
    body-of-loop
}

Example - Adding all elements of an array


Here is a loop written as both a for-each loop and a basic for loop.

double[] ar = {1.2, 3.0, 0.8};
int sum = 0;
for (double d : ar) {  // d gets successively each value in ar.
    sum += d;
}

And here is the same loop using the basic for. It requires an extra iteration variable.

double[] ar = {1.2, 3.0, 0.8};
int sum = 0;
for (int i = 0; i < ar.length; i++) {  // i indexes each element successively.
    sum += ar[i];
}

Where the for-each is appropriate


Altho the enhanced for loop can make code much clearer, it can't be used in some common situations.

  • Only access. Elements can not be assigned to, eg, not to increment each element in a collection.
  • Only single structure. It's not possible to traverse two structures at once, eg, to compare two arrays.
  • Only single element. Use only for single element access, eg, not to compare successive elements.
  • Only forward. It's possible to iterate only forward by single steps.
  • At least Java 5. Don't use it if you need compatibility with versions before Java 5.

Mobile Processing Program

PScrollBar scrollbar;
PContainer screen;

void setup() {
//// put a scrollbar on the right side of the screen
scrollbar = new PScrollBar();
scrollbar.setBounds(width - 4, 0, 4, height);

//// let the container fill the rest of the screen
screen = new PContainer();
screen.scrolling = true;
screen.scrollbar = scrollbar;
screen.setBounds(0, 0, width - 4, height);

int y = 0;

PLabel label = new PLabel("Random 1");
label.calculateBounds(4, y, width - 12, PContainer.HEIGHT_UNBOUNDED);
screen.add(label);
y = label.y + label.height + 4;

int r1 = random(10);

PTextField textfield = new PTextField(Integer.toString(r1));
textfield.calculateBounds(4, y, width - 12, PContainer.HEIGHT_UNBOUNDED);
screen.add(textfield);
y = textfield.y + textfield.height + 8;



label = new PLabel("Random 2");
label.calculateBounds(4, y, width - 12, PContainer.HEIGHT_UNBOUNDED);
screen.add(label);
y = label.y + label.height + 4;

int r2 = random(10);

textfield = new PTextField(Integer.toString(r2));
// textfield.password = true;
textfield.calculateBounds(4, y, width - 12, PContainer.HEIGHT_UNBOUNDED);
screen.add(textfield);

int result = 0;

String s = "";
int o = random(1,4);
if(o == 1){
s = "+";
result = r1 + r2;

}
if(o == 2){
s = "-";
result = r1 - r2;
}
if(o == 3){
s = "*";
result = r1 * r2;
}
if(o == 4){
s = "/";
result = r1 / r2;
}




label = new PLabel("Operator");
label.calculateBounds(4, y+40, width - 12, PContainer.HEIGHT_UNBOUNDED);
screen.add(label);
y = label.y + label.height + 4;

textfield = new PTextField(s);
// textfield.password = true;
textfield.calculateBounds(4, y, width - 12, PContainer.HEIGHT_UNBOUNDED);
screen.add(textfield);


label = new PLabel("Answere");
label.calculateBounds(4, y+40, width - 12, PContainer.HEIGHT_UNBOUNDED);
screen.add(label);
y = label.y + label.height + 4;



textfield = new PTextField("");
// textfield.password = true;
textfield.calculateBounds(4, y, width - 12, PContainer.HEIGHT_UNBOUNDED);
screen.add(textfield);

label = new PLabel("Result");
label.calculateBounds(4, y+40, width - 12, PContainer.HEIGHT_UNBOUNDED);
screen.add(label);
y = label.y + label.height + 4;

textfield = new PTextField("");
// textfield.password = true;
textfield.calculateBounds(4, y, width - 12, PContainer.HEIGHT_UNBOUNDED);
screen.add(textfield);



//// initialize the container (which initializes
//// all of its children and the scrollbar)
screen.initialize();
screen.acceptFocus();
}

void draw() {
//// draw the container (which draws its children)
screen.draw();
}

void keyPressed() {
//// let the container handle the input, it will
//// pass it down to the focused child
screen.keyPressed();
}

void keyReleased() {
//// let the container handle the input, it will
//// pass it down to the focused child
screen.keyReleased();
}

Monday, 19 March 2012

parrot class inheritance


void setup(){
  Parrot p1= new Parrot();  // Reference to the Object of name P1
  TalkingParrot tp1= new TalkingParrot();
  p1.Speaking();
  p1.Eating();
  tp1.Speaking();
  
}
void draw() {
}

////////////////
class Parrot {
// Class of Parrot . or Parent or Super Class
  public void Walking() {   // Method
    println("I am walking");
  }
  public void Eating() {
    println("I am hngry...I want to eat!!");
  }
  public void Sleeping() {
    println("Time to sleep zZzZZZZZ...");
  }
  public void Speaking() {
    println("tain tain tain...");
  }
}

//////////////
class TalkingParrot extends Parrot{   // Inheritance 
  public void Speaking() {   // Method OverRiding
    println("Take me out of this cage right now!!!");
  }

Friday, 16 March 2012

3. Bouncy Boxes OpenGL Code First Third Half Class (Square)


void mousePressed()
{
  for(int k=0;k<numSquares;k++)
  {
    xOff=mouseX-s[k].x;
    yOff=mouseY-s[k].y;
    if((xOff<=(s[k].hs))&&(xOff>=-(s[k].hs))&&(yOff<=(s[k].hs))&&(yOff>=-(s[k].hs)))
    {chooser=k;break;}
    else{chooser=numSquares;}
  }
  if(chooser!=numSquares)
  {
   s[chooser].x=mouseX;
   s[chooser].y=mouseY;
        
   s[chooser].xVel=mouseX-pmouseX;
   s[chooser].yVel=mouseY-pmouseY;
   s[chooser].age=0;
  }
}
 
void mouseReleased()
{
 chooser=numSquares;
}