1. Reference Variables Lesson¶
Memory Map Warmup¶
Memory Map Warmup
Sample solutions not included, because "Show Solutions" was not toggled before printing.
Part 1: Memory Map: Account Class¶
Part 1: Memory Map: Account Class
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:
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
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);
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;
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
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());
Part 2: Memory Map: Payroll Class¶
Part 2: Memory Map: Payroll Class
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.
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);
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:
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
Fig. 1 Account and Payroll classes¶
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:
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