1.5. Practice Exercises

Exercise 1: Person Class
Person Class

Consider the code for a Person class below, which is used throughout this activity.

 1public class Person {
 2
 3   private String name;
 4   private int age;
 5
 6   public Person(String name, int age) {
 7      this.name = name;
 8      this.age = age;
 9   } // Person
10
11   public String getName() {
12       return this.name;
13   } // getName
14
15   public void setName(String name) {
16       this.name = name;
17   } // setName
18
19   public int getAge() {
20       return this.age;
21   } // getAge
22
23   public void setAge(int age) {
24       this.age = age;
25   } // setAge
26
27} // Person
Question 1

Answer the following questions about the Person class:

  1. Identify the class's instance variables. How many instance variables are there?

  2. Identify the class's constructors. How many constructors are there?

  3. Identify the class's methods. How many methods are there?

  4. Which methods and constructors declare parameters or local variables? Count both kinds together for each method or constructor.

Note

Java's language specification distinguishes parameters from local variables. Both belong to an individual call and appear in its frame in our memory map diagrams, but parameters receive their initial values from a call's arguments. When identifying variable declarations that are local to a method in source code, count parameters and local variables together.

Solution
  1. name and age – (2)

  2. public Person(String name, int age) – (1)

  3. getName(), setName(String name), getAge(), and setAge(int age) – (4)

  4. Counting parameters and local variables together, setName(String name) and setAge(int age) each declare 1, the constructor declares 2, and getName() and getAge() declare 0. All of these declarations are parameters; none of the methods or the constructor declare additional local variables in their bodies.

Question 2

Draw a diagram that depicts what the program's memory looks like after executing the code below.

Person nobody = null;
Person susan = new Person("Susan", 23);
Solution
Code visualization diagram for java/variables/practice:102 (end of main)

Final captured memory state. [download] [open]

Question 3

Draw a memory map diagram depicting the state of the variables after the first half of the code below executes. Once your memory map is complete, write down what the output of the second half should be.

 4// first half
 5Person nobody = null;
 6Person susan = new Person("Susan", 23);
 7Person bill = new Person("Bill", 22);
 8Person p = bill;
 9
10// second half
11System.out.println(nobody);
12System.out.println(susan.getName());
13System.out.println(bill.getAge());
14System.out.println(p.getAge());
Solution
Code visualization diagram for java/variables/practice:136 (end of main)

Final captured memory state. [download] [open]

Question 4

Draw a new memory diagram that takes into consideration the new code in the middle. After that, write down the output of the print statements near the bottom.

 4// these four lines have not changed
 5Person nobody = null;
 6Person susan = new Person("Susan", 23);
 7Person bill = new Person("Bill", 22);
 8Person p = bill;
 9
10// new code
11susan.setName("Bill");
12bill.setName("Susan");
13
14// print statements
15System.out.println(susan.getName());
16System.out.println(bill.getName());
17System.out.println(p.getName());
18System.out.println(nobody);
Solution

The interactive visualization stepper below shows the state of memory before and after the name modifications, as well as the accumulated console output after all print statements execute:

Step 1 of 3: line 11
Code visualization diagram for java/variables/practice:178 (line 11)

Memory state at the breakpoint on line 11. [download] [open]

> (no output yet)

>_ Console Output

Code visualization diagram for java/variables/practice:178 (line 15)

Memory state at the breakpoint on line 15. [download] [open]

> (no output yet)

>_ Console Output

Code visualization diagram for java/variables/practice:178 (line 19)

Memory state at the breakpoint on line 19. [download] [open]

Bill
Susan
Susan
null

>_ Console Output

Note

You may be surprised by the fact that this compiles, runs without crashing, and produces output, given that nobody is used in the last print statement and the value of nobody is null.

It's okay to print null. This value is only a problem when you try to use it as a calling object.

Question 5

Using the updated memory map you drew for the last question, write down the output of the code below.

// these four lines have not changed
Person nobody = null;
Person susan = new Person("Susan", 23);
Person bill = new Person("Bill", 22);
Person p = bill;

// these four lines have not changed
susan.setName("Bill");
bill.setName("Susan");

// ignore the other print statements from earlier
// ...
System.out.println(nobody.getName());
Solution

This program crashes at runtime with a NullPointerException when nobody.getName() is executed because the value of nobody is null, which means nobody does not refer to an object:

Code visualization diagram for java/variables/practice:224 (end of main)

Final captured memory state. [download] [open]

Exercise 2: SalaryAccount Class
SalaryAccount Class

You should assume the class below exists and is in scope for the questions in this subsection:

Listing 1.5 in SalaryAccount.java
 1public class SalaryAccount {
 2
 3   private int hours;
 4   private double rate;
 5
 6   public SalaryAccount(int hours, double rate) {
 7      // assume appropriate error handling...
 8      this.hours = hours;
 9      this.rate = rate;
10   } // SalaryAccount
11
12} // SalaryAccount
Question 1

Draw the memory map that represents the state of the variables after the following code executes:

SalaryAccount acct = new SalaryAccount(8, 20.0);
Solution

Your memory map should look similar to the following:

Code visualization diagram for java/variables/practice:287 (end of main)

Final captured memory state. [download] [open]

Question 2

Draw the memory map that represents the state of the variables after the following code executes:

4SalaryAccount acct1 = new SalaryAccount(8, 20.0);
5SalaryAccount acct2 = new SalaryAccount(10, 22.5);
6SalaryAccount acct3 = new SalaryAccount(6, 18.75);
Solution

Your memory map should look similar to the following:

Code visualization diagram for java/variables/practice:317 (end of main)

Final captured memory state. [download] [open]