Polymorphism program

Page 1

Polymorphism program Java lab GS311-CS602 package animal;

public class Animal {

/** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here bird b= new bird("pigeon","bird",70.0,0.45); fish f= new fish("shark","fish",60.0,89.0); lion l= new lion("lion","wild",50.0,65.0); animals a[]= new animals[3]; a[0]=b; a[1]=f; a[2]=l; for(int i=0; i<a.length;i++) {


System.out.printf("name of animal:%s\n type of anima:%s\n speed:%f\n" + " time:%f\n distance:%.2f\n",a[i].name, a[i].type,a[i].speed,a[i].time,a[i].getdistance()); }

} } public class animals { String name; String type; double speed; double time= 10.0; double distance; public animals(String n,String t,double s) { name=n; type=t; speed=s;


} public void setname(String n) { name=n; } public String getname() { return name; } public void settype(String t) { type =t; } public String gettype() { return type; } public void setspeed(double s) { speed=s; }


public double getspeed() { return speed; } public double getdistance() { return distance=time*speed; } } public class bird extends animals { double lenght; public bird(String n,String t,double s,double l) { super(n,t,s); //size of animal lenght = l; } public void setlenght(double l) { lenght=l; }


public double getlenght() { return lenght; } public double getdistance() { return super.getdistance()-lenght; }

} public class fish extends animals { double lenght; public fish(String n,String t,double s,double l) { super(n,t,s); //size of animal lenght = l; } public void setlenght(double l) {


lenght=l; } public double getlenght() { return lenght; } public double getdistance() { return super.getdistance()-lenght; } } public class lion extends animals { double lenght; public lion(String n,String t,double s,double l) { super(n,t,s); //size of animal lenght = l; } public void setlenght(double l)


{ lenght=l; } public double getlenght() { return lenght; } public double getdistance() { return super.getdistance()-lenght; }

}


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.