<< Back to Object Oriented Programming Portfolio

Cat and Owner Assignment


Cat class

public class Cat
{
    public String name;
    public double happyLevel;

    public Cat(String name, double happyLevel)
    {
        this.name = name;
        this.happyLevel = happyLevel;
    }

    public String getName()
    {
        return this.name;
    }

    public double getHappyLevel()
    {
        return this.happyLevel;
    }

    public boolean isHappy()
    {
        return this.happyLevel >= 3.5;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public void setHappyLevel(double happyLevel)
    {
        this.happyLevel = happyLevel;
    }
}

Owner class

public class Owner
{
    private Cat cat;

    public Owner(Cat cat)
    {
        this.cat = cat;
    }

    public void petCat()
    {
        if(this.cat != null && this.cat.happyLevel < 5.0)
        {
            this.cat.happyLevel += 0.3;
        }
        else if(this.cat != null)
        {
            this.cat.happyLevel = 5.0;
        }
    }

    public boolean isCatHappy()
    {
        return this.cat != null && this.cat.isHappy();
    }

    public String shoutCatName()
    {
        if(this.cat != null)
        {
            return "Come here, " + this.cat.name;
        }
        
        return "No cat to call.";
    }

    public Cat getCat()
    {
        return this.cat;
    }
}