1. Reference Variables¶
Part 1: Memory Map: Account Class¶
Part 1: Memory Map: Account Class
Note
To show solutions, you will need to toggle “Show Solutions” above.
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
Question 3
What would be output from the following code?
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 point to the same object and we updated
the hours worked to be 15.
Question 4
Question 5
Part 2: Memory Map: Payroll Class¶
Part 2: Memory Map: Payroll Class
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 acc : employeeAccounts) {
if (acc != null) {
total += acc.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
Write a method called calculateTotalPay inside of
Payroll.java that calculates the total pay for all employee
accounts.
Solution
One Possible Solution:
1public double calculateTotalPay() {
2 double total = 0;
3 for (Account acc : employeeAccounts) {
4 if (acc != null) {
5 total += acc.getHoursWorked() * acc.getHourlyRate();
6 } // if
7 } // for
8 return total;
9} // calculateTotalPay