Hi i have the next task.
5. Create a hierarchy Dog, Frog, Cat, Kitten,
Tomcat and define suitable fields, properties,
constructors and methods according to the
following rules:
All of these are Animals. Kittens and tomcats
are cats. All animals are described by age,
name and gender. Kittens can be only female
and tomcats can be only male. Each animal
produces a sound (virtual method).
Create an array of different kinds of animals
and for each animal display its name, age
and its specific sound.
The only think i need it to set in class Kittens to be always ismale = false and in class tomcat ismale = true.
How can u do that. Tell me is something in my code isnt right or can be upgraded.
Thanks
package Zadacha_5;
public class Animals {
/*
* Create a hierarchy Dog, Frog, Cat, Kitten,
Tomcat and define suitable fields, properties,
constructors and methods according to the
following rules:
All of these are Animals. Kittens and tomcats
are cats. All animals are described by age,
name and gender. Kittens can be only female
and tomcats can be only male. Each animal
produces a sound (virtual method).
Create an array of different kinds of animals
and for each animal display its name, age
and its specific sound.
*/
private int age = 0;
private boolean isMale = false;;
private String name = "unnamed";
public Animals() {
}
public Animals(int age, boolean isMale, String name) {
// TODO Auto-generated constructor stub
this.age = age;
this.isMale = isMale;
this.name = name;
}
public void sound(){
}
public void printInfo(){
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isMale() {
return isMale;
}
public void setMale(boolean isMale) {
this.isMale = isMale;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package Zadacha_5;
public class Cats extends Animals{
public Cats(){
}
public Cats(int age, boolean isMale, String name){
super(age, isMale,name);
}
}
package Zadacha_5;
public class Kittens extends Cats{
public Kittens(int age, boolean isMale, String name) {
super(age ,isMale,name);
}
public void sound(){
System.out.println ("The kitten says: myau, myau");
}
public void printInfo(){
System.out.println ("Kitten name is: " + getName() + ", its a male: " + isMale()+ ", and its "+ getAge()+ " years old.");
}
}
package Zadacha_5;
public class TomCats extends Cats {
public TomCats(int age, boolean isMale, String name){
super(age, isMale, name);
}
public void sound(){
System.out.println ("The tomcat says: MYAW, MYAW");
}
public void printInfo(){
System.out.println ("TomCat name is: " + getName() + ", its a male: " + isMale()+ ", and its "+ getAge()+ " years old.");
}
}
package Zadacha_5;
public class Frog extends Animals{
public Frog(int age, boolean isMale, String name) {
super(age, isMale, name);
// TODO Auto-generated constructor stub
}
public void sound(){
System.out.println ("The frog says: kwac, kwac");
}
public void printInfo(){
System.out.println ("Frog name is: " + getName() + ", its a male: " + isMale()+ ", and its "+ getAge()+ " years old.");
}
}
package Zadacha_5;
public class Dog extends Animals{
public Dog(int age, boolean isMale, String name) {
super (age, isMale, name);
}
public void sound(){
System.out.println ("The dog says: bau, bau");
}
public void printInfo(){
System.out.println ("Dog name is: " + getName() + ", its a male: " + isMale()+ ", and its "+ getAge()+ " years old.");
}
}
package Zadacha_5;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Kittens kitten = new Kittens(1, false, "KITTEN");
TomCats tomcat = new TomCats(5, true, "TOMCAT");
Frog frog = new Frog(10, false, "FROG");
Dog dog = new Dog(15, true, "DOG");
Animals[] array = new Animals[]{kitten, tomcat, frog, dog};
for (Animals animal : array){
animal.printInfo();
}
}
}