piowit

aaa

Oct 16th, 2012
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. import java.util.Random;
  2. public class lab3 {
  3.  
  4.  
  5.    
  6.     public static void main(String[] args) {
  7.        
  8.         class Animal{
  9.             String name;
  10.             double mass;
  11.             int no_legs;
  12.             int no_eyes;
  13.             //method countExtremities();
  14.            
  15.            
  16.         }  
  17.        
  18.         class Mammal extends Animal{
  19.             int no_arms;
  20.            
  21.             public Mammal(){
  22.                 mass = Math.random();
  23.                 no_legs = 2;
  24.                 no_eyes = 2;
  25.                 no_arms = 2;
  26.             }
  27.            
  28.         }  
  29.        
  30.         class Human extends Mammal{
  31.            
  32.             public Human(String nam){
  33.                 super.name = nam;
  34.             }
  35.         }  
  36.        
  37.         class Cat extends Mammal{
  38.             public Cat(String nam){
  39.                 super.name = nam;
  40.             }
  41.         }  
  42.        
  43.         class Dog extends Mammal{
  44.             public Dog(String nam){
  45.                 super.name = nam;
  46.             }
  47.         }
  48.        
  49.         class Bird extends Animal{
  50.             public Bird(){
  51.             mass = Math.random();
  52.             no_legs = 2;
  53.             no_eyes = 2;
  54.             }
  55.         }
  56.        
  57.         class Sparrow extends Bird{
  58.             public Sparrow(String nam){
  59.                 super.name = nam;
  60.             }
  61.         }
  62.        
  63.         class Hummingbird extends Bird{
  64.             public Hummingbird(String nam){
  65.                 super.name = nam;
  66.             }
  67.         }
  68.        
  69.         class Snail extends Animal{
  70.             public Snail(){
  71.             mass = Math.random();
  72.             no_legs = 0;
  73.             no_eyes = 1;
  74.             }
  75.         }
  76.        
  77.         class Pulmonata extends Snail{
  78.             public Pulmonata(String nam){
  79.                 super.name = nam;
  80.             }
  81.         }
  82.        
  83.         Animal myanimal[] = new Animal[20];
  84.         myanimal[0] = new Human("Lucy");
  85.         myanimal[1] = new Human("Jim");
  86.         myanimal[2] = new Human("Sally");
  87.         myanimal[3] = new Human("Will");
  88.         myanimal[4] = new Cat("cat1");
  89.         myanimal[5] = new Cat("cat2");
  90.         myanimal[6] = new Dog("dog1");
  91.         myanimal[7] = new Sparrow("sparrow1");
  92.         myanimal[8] = new Hummingbird("hummingbird1");
  93.         myanimal[9] = new Hummingbird("hummingbird2");
  94.         myanimal[10] = new Pulmonata("pulmonata1");
  95.         myanimal[11] = new Pulmonata("pulmonata2");
  96.         int i=0;
  97.         for(Object anim : myanimal){
  98.             System.out.println(myanimal[i].name + " " + myanimal[i].mass + " " + myanimal[i].no_legs + " " + myanimal[i].no_eyes );
  99.             i++;
  100.         }
  101.        
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment