1. Reference Variables

Memory Map: Account Class

Memory Map: Account Class

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:

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

Your memory map should look similar to the following:

Question 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);
Solution

Your memory map should look similar to the following:

Memory Map: Payroll Class

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.

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 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:

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

Write a method called calculateTotalPay inside of Payroll.java that calculates the total pay for all employee accounts.

Solution

One Possible Solution:

Listing 4 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

Memory Map: TicTacToe

Memory Map: TicTacToe

Question 1

Draw the memory map for the code below:

Listing 5 In TicTacToeGame.java
 1public class TicTacToeGame {
 2
 3   private char[][] board;
 4   private int turns;
 5
 6   /** Constants to improve readability */
 7   public static final char X = 'X';
 8   public static final char O = 'O';
 9   public static final char EMPTY = ' ';
10
11   public TicTacToeGame() {
12
13      board = new char[3][3];
14      turns = 0;
15
16      for (int r = 0; r < 3; r++ ) {
17         for (int c = 0; c < 3; c++ ) {
18            board[r][c] = TicTacToeGame.EMPTY;
19         } // for
20      } // for
21
22   } // TicTacToeGame
23
24} // TicTacToeGame
Listing 6 In TicTacToeDriver.java
 1public class TicTacToeDriver {
 2
 3   public static void main(String[] args) {
 4
 5      TicTacToeGame ttt = null;
 6      ttt = new TicTacToeGame();
 7
 8   } // main
 9
10} // TicTacToeDriver
Solution
Question 2

Implement the method below according to the specifications in the comment above the method.

Listing 7 In TicTacToeGame.java
/**
 * Makes a move for the specified player, {@code p}, at location
 * ({@code r}, {@code c}) within the game board. If the move is invalid,
 * the method has no effect on the underlying board. The move is invalid
 * when the space is already occupied or the space is out of bounds.
 * Note: This method also increases the number of turns played if the move
 * is successful.
 *
 * @param p the character representing the player making the move.
 * @param r the row where the move is being made.
 * @param c the column where the move is being made.
 */
public void playMove(char p, int r, int c) {

   // TODO: implement me. Replace the line below with your implementation of the method.
   throw new UnsupportedOperationException("playMove is not implemented");

} // playMove
Question 3

Add a call to playMove in your TicTacToeDriver.java file and update your memory map accordingly (after this call is made).

Example
Listing 8 In TicTacToeDriver.java
3TicTacToeGame ttt = null;
4ttt = new TicTacToeGame();
5ttt.playMove(TicTacToeGame.X, 1, 1);
Solution
code visualization

Fig. 1 In ``TicTacToeDriver.java``

Example
Listing 9 In TicTacToeDriver.java
3TicTacToeGame ttt = null;
4ttt = new TicTacToeGame();
5ttt.playMove(TicTacToeGame.X, 1, 1);
6ttt.playMove(TicTacToeGame.O, 0, 0);
Solution
code visualization

Fig. 2 In ``TicTacToeDriver.java``

Example
Listing 10 In TicTacToeDriver.java
3TicTacToeGame ttt = null;
4ttt = new TicTacToeGame();
5ttt.playMove(TicTacToeGame.X, 1, 1);
6ttt.playMove(TicTacToeGame.O, 0, 0);
7ttt.playMove(TicTacToeGame.X, 0, 3);
Solution
code visualization

Fig. 3 In ``TicTacToeDriver.java``

Question 4

Implement the method below according to the specifications in the comment above the method.

Listing 11 In TicTacToeGame.java
/**
 * Returns whether the specified character, {@code p}, has
 * won the game.
 *
 * @return {@code true} if {@code p} has won the game and {@code false}
 * otherwise.
 * @param p the character to check.
 */
public boolean isWinner( char p ) {

   // TODO: implement me. Replace the line below with your implementation of the method.
   throw new UnsupportedOperationException("isWinner is not implemented");

} // isWinner