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();
		
		}
	}

}

Kittens can be male or female.
Tomcats are always male, so it makes no sense to require boolean isMale in the constructor - just leave out that parameter and hard wire isMale=true in the constructor.
You may also want to override the setMale method for Tomcat to raise an error if the parameter is set to false.

is that all?

package Zadacha_5;
 
public class TomCats extends Cats {
 
	public TomCats(int age, boolean isMale, String name){
		super(age, isMale=true, 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.");
 
	}
 
}

> is that all?

Does that code even compile?
Hint: The way you are invoking the constructor of the super-class is erroneous.

it works great after that change. Where is my error?

> Where is my error?

I could have sworn I saw a double equals there ( isMale==true ). Disregard my previous comment.

Oh and BTW, you've got your class naming wrong there. The class names should ideally reflect a simulation of a real-world entity. Animals is just a collection of Animal entity. Along the same line, Cats are just a group of Cat entities. The state of your class (variables) reflects that you are trying to model a single entity(Animal) but your class name says otherwise (Animals). Drop the plurals and you should be good to go.

Also instead of having an explicit printInfo() method, override the toString() method of the Object class. This would give help you write code like System.out.println(myCat); instead of myCat.printInfo() .

Suppose I call
new Tomcat(3, false, "Pussy"); // wrong, but no error raised
or
new Tomcat(3, true, "Pussy").setMale(false); // wrong, but no error raised

Suppose I call
new Tomcat(3, false, "Pussy"); // wrong, but no error raised

The assignment in the super calll would overwrite the passed in value of the isMale property so it really shouldn't be a problem.

Problem is that you allow the user to call your methods with invalid parameters, and you don't raise an error. You should remove the isMale parameter from the Tomcat constructor, and override setMale to flag an eror if its parameter is false.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.