Sunday, August 4, 2013

Dog Program

Program:

package chap03;

public class Dog {
String name;
   public static void main(String[] args) {
       Dog dog1 = new Dog();
       dog1.bark();
       dog1.name = "Bart";
     
       Dog[] myDogs = new Dog[3];
       myDogs[0] = new Dog();
       myDogs[1] = new Dog();
       myDogs[2] = dog1;
     
       myDogs[0].name = "Fred";
       myDogs[1].name = "Marge";
     
       System.out.print("last don't name is ");
       System.out.println(myDogs[2].name);
     
       int x = 0;
       while (x < myDogs.length) {
           myDogs[x].bark();
           x = x+1;
       }
   }
   public void bark() {
       System.out.println(name + " says Ruff!");
   }
 
   public void eat() { }
 
   public void chaseCat() { }

}

O/P:

null says Ruff!
last don't name is Bart
Fred says Ruff!
Marge says Ruff!

Bart says Ruff!

No comments:

Post a Comment