I need some help please! I\'m working with java programming and I need to create
ID: 3808520 • Letter: I
Question
I need some help please! I'm working with java programming and I need to create one of each type of animal and displays the animal’s toString method.
You are to add the following:
- Animals have a Weight.
- Animals have a Height.
- Dog is an Animal.
- Dogs have a Name.
- Dogs have a Breed.
- Dogs have a DOB.
- Cat is an Animal
- Cats have a Name.
- Cats have 9 lives, so you need to keep track of the remaining lives once a cat dies.
- Bird is an Animal
- Birds have a wing span
- Birds have a canFly (some birds cannot fly)
Explanation / Answer
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Animal{
public int weight, height;
@Override
public String toString(){
return "Weight : "+weight+" Height : "+height;
}
}
class Dog extends Animal{
private String name, breed;
public Dog(String name, String breed, Date dob,int weight, int height){
this.name = name;
this.breed = breed;
super.weight = weight;
super.height = height;
this.dob = dob;
}
Date dob;
@Override
public String toString(){
return "Name : "+name+" Breed : "+breed+" DOB "+dob+" "+super.toString();
}
}
class Cat extends Animal{
private String name;
int lives;
Cat(String name, int lives, int weight, int height){
this.name = name;
this.lives = lives;
super.weight = weight;
super.height = height;
}
@Override
public String toString(){
return "Name "+name+" Lives "+lives+" "+super.toString();
}
}
class Bird extends Animal{
int wingSpan;
boolean canFly;
public Bird(int wingSpan, boolean canFly, int weight, int height){
this.wingSpan = wingSpan;
this.canFly = canFly;
super.weight = weight;
super.height = height;
}
@Override
public String toString(){
return "Wingspan "+wingSpan+" canFly "+canFly+" "+super.toString();
}
}
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Dog d = new Dog("Snoopy","Germen Shepherd",new Date(),33,2);
System.out.println("DOG : "+d);
Cat c = new Cat("Kitty",5,15,1);
System.out.println("CAT : "+c);
Bird b = new Bird(5,true,20,2);
System.out.println("BIRD : "+b);
}
}
OUTPUT :