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
}
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.
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 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.
Sample answer
(a) and (b)
public static int product(int a, int b) {
return a * b;
}
public static void tag(String word, int n) {
System.out.println(word + n);
}
(c) product(3, 4) returns 12, and that value is not printed or stored, so the call adds nothing to the output.
42
lab2
println(product(6, 7)) prints 42. tag prints lab2 itself because it is void and the print is inside the method.
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);
}
}
3 points. Declare these private instance variables: name (String), age (int), and weight (double). Their scope is the whole class.
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 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.
Sample answer
public class Pet {
private String name;
private int age;
private double weight;
public Pet() {
name = "";
age = 0;
weight = 0.0;
}
public Pet(String name, int age, double weight) {
this.name = name;
this.age = age;
this.weight = weight;
}
}
new Pet() calls the constructor with no parameters. new Pet("Milo", 4, 12.5) calls the constructor with three parameters. this.name = name stores the parameter in the field. name = name would assign the parameter to itself and leave the field unchanged.
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.
2 points. Write the accessor public int getAge(). It returns age and does not change any data.
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 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.
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);
Sample answer
(a), (b), and (c)
public int getAge() {
return age;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String toString() {
return name + " is " + age + " years old and weighs " + weight + " pounds";
}
(d) With no toString, printing the object shows an address such as Pet@521f7h. The next line is 4. setWeight(13.0) changes weight and prints nothing.
Pet@521f7h
4
After part (c), System.out.println(milo) calls toString, so the first line is Milo is 4 years old and weighs 12.5 pounds. The second line is still 4. The weight is 13.0 after setWeight, and that new weight appears only if the pet is printed again.