1.5. Additional Practice Exercises

1.5.1. 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
  1. Draw the memory map that represents the state of the variables after the following code executes:

    SalaryAccount acct = new SalaryAccount(8, 20.0);
    
  2. Draw the memory map that represents the state of the variables after the following code executes:

    3SalaryAccount acct1 = new SalaryAccount(8, 20.0);
    4SalaryAccount acct2 = new SalaryAccount(10, 22.5);
    5SalaryAccount acct3 = new SalaryAccount(6, 18.75);
    
SalaryAccount Class Solutions
  1. Your memory map should look similar to the following:

  2. Your memory map should look similar to the following:

1.5.2. Account Class

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

Listing 1.6 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
  1. Draw the memory map that represents the state of the variables after the following code executes:

    Account[] accounts = new Account[3];
    
  2. Draw the memory map that represents the state of the variables after the following code executes:

    Account[] accounts = new Account[2];
    
    accounts[0] = new Account("Salary", 40, 50.0);
    accounts[1] = new Account("Hourly", 10, 15.0);
    
Account Class Solutions
  1. Your memory map should look similar to the following:

  2. Your memory map should look similar to the following:

1.5.3. 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.

Listing 1.7 in Payroll.java
 1public class Payroll {
 2   private Account[] employeeAccounts;
 3
 4   public Payroll(Account[] accounts) {
 5      this.employeeAccounts = accounts;
 6   } // Payroll
 7
 8   public int calculateTotalHoursWorked() {
 9      int total = 0;
10      for (Account acc : employeeAccounts) {
11         if (acc != null) {
12            total += acc.getHoursWorked();
13         } // if
14      } // for
15      return total;
16   } // calculateTotalHoursWorked
17} // Payroll
  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);
    
  2. What will be output if we execute the following line after the code in the previous question:

    Listing 1.8 Executed after payroll has been created
    System.out.println(payroll.calculateTotalHoursWorked());
    
  3. Write a method called calculateTotalPay inside of Payroll.java that calculates the total pay for all employee accounts.

Payroll Class Solutions
  1. Your memory map should look similar to the following:

  2. The program should output 50 for the total hours worked.

  3. One Possible Solution:

    Listing 1.9 in Payroll.java
    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