How do I add this field properly?

I am working on this bootcamp task that is designed for me to practice inheritance. The base class looks like this:

public class Animal {
private int numTeeth = 0;
private boolean spots = false;
private int weight = 0;

public Animal(int numTeeth, boolean spots, int weight){
    this.setNumTeeth(numTeeth);
    this.setSpots(spots);
    this.setWeight(weight);
}

public int getNumTeeth(){
    return numTeeth;
}

public void setNumTeeth(int numTeeth) {
    this.numTeeth = numTeeth;
}

public boolean getSpots() {
    return spots;
}

public void setSpots(boolean spots) {
    this.spots = spots;
}

public int getWeight() {
    return weight;
}

public void setWeight(int weight) {
    this.weight = weight;
}

I am supposed to create a sub class called lion that inherits from the Animal class with the following specific requirements:

  1. Add a field for lion t ​ype ( ​ cub, male, female).

  2. Add a method in this class which sets the lion type based on its weight (note that the weight is a derived field from the superclass).

  3. If the weight is less than 80kg, it’s type should be a cub. If less than 120kg, it should be female. If greater than 120kg, it is a male.

  4. Include a method that will print out a description of a lion object.

I’ve been trying to figure this out for hours but the best I could come up with is the following:

public class Lion extends Animal {
    public Lion(int numTeeth, boolean spots, int weight) {
        super(numTeeth, spots, weight);
    
        }
        
    void lionDisplay() {
        String type = new String();
        {
        if (getWeight() >= 120) {
            type.equals("Male");
        }   
        else if (getWeight() < 120 && getWeight() >= 80) {
            type.equals("Female");  
        }
        else if (getWeight() < 80) {
            type.equals("Cub");
        }
        System.out.println("Animal = Lion");
        System.out.println("Teeth = " + getNumTeeth());
        System.out.println("Spots = " + getSpots());
        System.out.println("Weight = " + getWeight());
        System.out.println("Type = " + type);
    }
    }
    }
public class AnimalLauncher {
    public void main(String[] args) {
        
        Lion leo = new Lion(30, false, 90);
        leo.lionDisplay();
        
    
    }
}

}

I know I am doing something wrong but I don’t know what it is. For some reason I cannot get the ‘type’ to display at all.

This is the output I am getting:

Animal = Lion
Teeth = 30
Spots = false
Weight = 90
Type = 

Answer

I suggest adding type as a class attribute

public class Lion extends Animal {
    private String type;
    
    
    public Lion(int numTeeth, boolean spots, int weight) {
        super(numTeeth, spots, weight);
        this.type =  type;
    }

Add a setter method to change the value

public void setType(String type){
        this.type = type;
    }

And in the lionDisplay method

void lionDisplay() {
       
       
            if (getWeight() >= 120) {
                setType("Male");
            }
            else if (getWeight() < 120 && getWeight() >= 80) {
                setType("Female");
            }
            else if (getWeight() < 80) {
                setType("Cub");
            }
            System.out.println("Animal = Lion");
            System.out.println("Teeth = " + getNumTeeth());
            System.out.println("Spots = " + getSpots());
            System.out.println("Weight = " + getWeight());
            System.out.println("Type = " + type);
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *