?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Easy

Defining Variables and Methods of a Class

APCSA-KNJEVT

Consider the following class.

public class Pet 
{
    private String type, name;
    private int age;
    private int weight;

    /**
     * 
     * @param typ type of animal
     * @param nam name of animal
     * @param howOld age of pet
     * @param wt weight
     */
    public Pet(String typ, String nam, int howOld, int wt)
    {
        type = typ;
        name = nam;
        age = howOld;
        weight = wt;
    }

    public int getAge()
    {
        return age;
    }

    public int getWeight()
    {
        return weight;
    }

    // returns true if pet is over 50 lbs
    public boolean isHeavy()
    {
        if (weight > 50)
            return true;
        return false;
    }
}

A pet has a name, type of pet, age, and weight. Other classes will need to instantiate a new pet, retrieve any of the four characteristics of the pet, and be able to change the pet’s age and weight under certain situations. Also, a method needs to determine if the pet is heavy (over 50 pounds).

Which of the following improvement(s) need to be made to this class so that it meets the requirements described?

    I.   Additional accessor (getter) methods need to be defined
    II.  Mutator (modifier) methods need to be defined to allow modification of
         the age and weight
    III. The age and weight instance variables need to be public, not private
A

I only.

B

II only.

C

III only.

D

I and II only.

E

I and III only.