AP Computer Science

Free response

Three questions from the class slides on methods, classes, constructors, accessors, mutators, and toString. Each question is worth 9 points. What you type is saved in this browser.

Question 1 · 9 points

Methods, parameters, and return

main is already written inside Fun. Add the two static methods it calls. Both methods are in the same class as main, so the calls do not use an object or a dot.

public class Fun {
    public static void main(String[] args) {
        System.out.println(product(6, 7));
        tag("lab", 2);
        product(3, 4);
    }

    // write product and tag here
}
  1. 3 points. Write product. The header is public static int product(int a, int b). Return the product of the two parameters. Do not print inside product. Do not declare a or b again, and do not use product as a variable name.
  2. 3 points. Write tag. The header is public static void tag(String word, int n). Print word and n joined together with no space. tag("lab", 2) prints lab2.
  3. 3 points. main calls product(3, 4) and does not print or store that result. What, if anything, from that call appears in the output? Then write the complete output of main.

Question 2 · 9 points

Classes, objects, and constructors

Write the complete class Pet. Do not write main. The runner below is a separate file, and it is the file that runs.

public class PetRunner {
    public static void main(String[] args) {
        Pet blank = new Pet();
        Pet milo = new Pet("Milo", 4, 12.5);
    }
}
  1. 3 points. Declare these private instance variables: name (String), age (int), and weight (double). Their scope is the whole class.
  2. 3 points. Write the default constructor public Pet(). It has the same name as the class and no return type. Initialize every instance variable: name to "", age to 0, and weight to 0.0. Assign the existing fields. Do not declare them again inside the constructor.
  3. 3 points. Write a second constructor, public Pet(String name, int age, double weight). Initialize every instance variable from the parameters. The parameter names match the fields, so use this to store the values in the instance variables. The two constructors overload each other.

Question 3 · 9 points

Accessors, mutators, and toString

Add methods to the Pet class from Question 2. name, age, and weight stay private, so other classes reach them only through methods.

  1. 2 points. Write the accessor public int getAge(). It returns age and does not change any data.
  2. 2 points. Write the mutator public void setWeight(double weight). It changes the instance variable weight to the parameter. Use this so the field is the variable that changes.
  3. 3 points. Write public String toString() with no parameters. Return a string that contains the name, age, and weight. For a pet created with ("Milo", 4, 12.5), return Milo is 4 years old and weighs 12.5 pounds.
  4. 2 points. The runner below runs against a Pet that has getAge and setWeight but no toString. What is printed? Then say what the first line becomes after part (c) is added.
Pet milo = new Pet("Milo", 4, 12.5);
System.out.println(milo);
System.out.println(milo.getAge());
milo.setWeight(13.0);