1. Reference Variables Lesson

Memory Map Warmup

Memory Map Warmup

To see sample solutions, toggle "Show Solutions" above.

Sample solutions not included, because "Show Solutions" was not toggled before printing.

Warm Up Question

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

public static void main(String[] args) {
   int num = 7;
   String phrase = "Hello, world";
} // main
Solution

Your memory map should look similar to the following:

Part 1: Memory Map: Account Class

Part 1: Memory Map: Account Class

To see sample solutions, toggle "Show Solutions" above.

Sample solutions not included, because "Show Solutions" was not toggled before printing.

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

Listing 1 in Account.java
 1public class Account {
 2
 3   private String accountType;
 4   private int hoursWorked;
 5   private double hourlyRate;
 6
 7   public Account(String accountType, int hoursWorked, double hourlyRate) {
 8      // assume appropriate error handling...
 9      this.accountType = accountType;
10      this.hoursWorked = hoursWorked;
11      this.hourlyRate = hourlyRate;
12   } // Account
13
14   public int getHoursWorked() {
15      return this.hoursWorked;
16   } // getHoursWorked
17
18   public double getHourlyRate() {
19      return this.hourlyRate;
20   } // getHourlyRate
21
22} // Account
Question 1

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

Account acct1 = new Account("Salary", 40, 50.0);
Solution

Your memory map should look similar to the following:

Question 2

Update the memory map that represents the state of the variables after the following code executes. Note the first line is the same. Your new drawing should show the effect of executing the second line:

Account acct1 = new Account("Salary", 40, 50.0);
Account acct2 = new Account("Hourly", 20, 25.0);
Solution

Your memory map should look similar to the following:

Question 3

Update the memory map that represents the state of the variables after the following code executes. Note the first two lines are the same. Your new drawing should show the effect of executing the third line:

Account acct1 = new Account("Salary", 40, 50.0);
Account acct2 = new Account("Hourly", 20, 25.0);
acct1 = acct2;
Solution

Your memory map should look similar to the following:

Question 4

What would be output from the following code?

Note: You should assume that the method setHoursWorked correctly updates the hoursWorked variable of the calling object.

Account acct1 = new Account("Salary", 40, 50.0);
Account acct2 = new Account("Hourly", 20, 25.0);
acct1 = acct2;
acct1.setHoursWorked(15); // assume the setter exists and works as expected
System.out.println(acct1.getHoursWorked());
System.out.println(acct2.getHoursWorked());
Solution

The program would output:

15
15

since both references refer to the same object and we updated the hours worked to be 15.

Question 5

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

Account[] accounts = new Account[3];
Solution

Your memory map should look similar to the following:

Question 6

Draw the memory map that represents the state of the variables after the following code executes. Then, write down what you expect to be output when the code runs.

Account[] accounts = new Account[3];

accounts[0] = new Account("Salary", 40, 50.0);
accounts[1] = new Account("Hourly", 20, 25.0);

accounts[1] = accounts[0];

System.out.println(accounts[1].getHoursWorked());
Solution

Your memory map should look similar to the following:

Part 2: Memory Map: Payroll Class

Part 2: Memory Map: Payroll Class

To see sample solutions, toggle "Show Solutions" above.

Sample solutions not included, because "Show Solutions" was not toggled before printing.

You should assume the class below exists and is in scope for the questions in this subsection. You should also assume that the Account class from the previous subsection exists and is in scope.

Listing 2 in Payroll.java
1public class Payroll {
2   private Account[] employeeAccounts;
3
4   public Payroll(Account[] accounts) {
5      this.employeeAccounts = accounts;
6   } // Payroll
7
8} // Payroll
Question 1

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

Account[] accounts = new Account[3];

accounts[0] = new Account("Salary", 40, 50.0);
accounts[1] = new Account("Hourly", 10, 15.0);

Account accounts2 = new Account("Contractor", 30, 60.0);

Payroll payroll = new Payroll(accounts);
Solution

Your memory map should look similar to the following:

Question 2

Write a method called calculateTotalHoursWorked inside of Payroll.java that calculates the total hours worked for all employee accounts.

Solution

Here is one possible solution:

public int calculateTotalHoursWorked() {
   int total = 0;
   for (Account account : employeeAccounts) {
      if (account != null) {
         total += account.getHoursWorked();
      } // if
   } // for
   return total;
} // calculateTotalHoursWorked
Question 3

What will be output if we execute the following line after the code in the previous question:

Listing 3 Executed after payroll has been created
System.out.println(payroll.calculateTotalHoursWorked());
Solution

The program should output 50 for the total hours worked.

Question 4
Starter Code
bundle1302 payroll
- downloading cs1302-payroll bundle...
- verifying integrity of downloaded files using sha256sum...
- extracting downloaded archive...
- removing intermediate files...
subdirectory cs1302-payroll successfully created
Example Class Diagram, Memory Map, and Driver Code

hide circle
hide empty members
set namespaceSeparator none
skinparam classAttributeIconSize 0
skinparam genericDisplay old
skinparam defaultFontName monospaced
skinparam defaultFontStyle bold
skinparam class {
    BackgroundColor LightYellow
    BackgroundColor<<interface>> AliceBlue
}
left to right direction

Payroll o--> Account: employeeAccounts

class Account {
   - accountType: String
   - hoursWorked: int
   - hourlyRate: double
   + <<new>> Account(accountType: String, hoursWorked: int, hourlyRate: double)
   + getAccountType(): String
   + setAccountType(accountType: String): void
   + getHoursWorked(): int
   + setHoursWorked(hoursWorked: int): void
   + getHourlyRate(): double
   + setHourlyRate(hourlyRate: double): void
}

class Payroll {
   - employeeAccounts: Account[]
   + <<new>> Payroll(accounts: Account[])
   + <color:blue>**calculateTotalHoursWorked(): int**</color>
   + <color:red>**calculateTotalPay(): double**</color>
}

Fig. 1 Account and Payroll classes

Listing 4 Example Code Involving Payroll and Account Classes
 4Account[] accounts = new Account[3];
 5accounts[0] = new Account("Salary", 40, 50.0);
 6accounts[1] = new Account("Hourly", 10, 15.0);
 7
 8Account accounts2 = new Account("Contractor", 30, 60.0);
 9accounts[2] = accounts2;
10
11Payroll payroll = new Payroll(accounts);
12
13int totalHours = payroll.calculateTotalHoursWorked();
14double totalPay = payroll.calculateTotalPay();
15
16System.out.println("Total hours worked: " + totalHours);
17System.out.println("Total pay: $" + totalPay);
Step 1 of 3: line 16
 4Account[] accounts = new Account[3];
 5accounts[0] = new Account("Salary", 40, 50.0);
 6accounts[1] = new Account("Hourly", 10, 15.0);
 7
 8Account accounts2 = new Account("Contractor", 30, 60.0);
 9accounts[2] = accounts2;
10
11Payroll payroll = new Payroll(accounts);
12
13int totalHours = payroll.calculateTotalHoursWorked();
14double totalPay = payroll.calculateTotalPay();
15
16System.out.println("Total hours worked: " + totalHours);
17System.out.println("Total pay: $" + totalPay);
Code visualization diagram for lectures/lesson1:460 (line 16)

Example Code Involving Payroll and Account Classes Note: The debugger breakpoint is on line 16. [code listing]

> (no output yet)

>_ Console Output

 4Account[] accounts = new Account[3];
 5accounts[0] = new Account("Salary", 40, 50.0);
 6accounts[1] = new Account("Hourly", 10, 15.0);
 7
 8Account accounts2 = new Account("Contractor", 30, 60.0);
 9accounts[2] = accounts2;
10
11Payroll payroll = new Payroll(accounts);
12
13int totalHours = payroll.calculateTotalHoursWorked();
14double totalPay = payroll.calculateTotalPay();
15
16System.out.println("Total hours worked: " + totalHours);
17System.out.println("Total pay: $" + totalPay);
Code visualization diagram for lectures/lesson1:460 (line 17)

Example Code Involving Payroll and Account Classes Note: The debugger breakpoint is on line 17. [code listing]

Total hours worked: 80

>_ Console Output

 4Account[] accounts = new Account[3];
 5accounts[0] = new Account("Salary", 40, 50.0);
 6accounts[1] = new Account("Hourly", 10, 15.0);
 7
 8Account accounts2 = new Account("Contractor", 30, 60.0);
 9accounts[2] = accounts2;
10
11Payroll payroll = new Payroll(accounts);
12
13int totalHours = payroll.calculateTotalHoursWorked();
14double totalPay = payroll.calculateTotalPay();
15
16System.out.println("Total hours worked: " + totalHours);
17System.out.println("Total pay: $" + totalPay);
Code visualization diagram for lectures/lesson1:460 (line 18)

Example Code Involving Payroll and Account Classes Note: The debugger breakpoint is on line 18. [code listing]

Total hours worked: 80
Total pay: $3950.0

>_ Console Output

In your groups, write the method named calculateTotalPay inside of Payroll.java so that it calculates and returns the total pay for all accounts, as demonstrated in the example above.

Sample Solution

One Possible Solution:

Listing 5 in Payroll.java
 1public double calculateTotalPay() {
 2   double total = 0;
 3   for (Account account : employeeAccounts) {
 4      if (account != null) {
 5         total += account.getHoursWorked()
 6                * account.getHourlyRate();
 7      } // if
 8   } // for
 9   return total;
10} // calculateTotalPay