Practice Problems:   Tracing Object Creation and Method Execution
1 The Problems
2 Answers

Practice Problems: Tracing Object Creation and Method Execution

Imagine that you are creating a new smartphone application for helping people manage their fitness goals. You would need to track activities (running, swimming, etc), clients (people with fitness goals), and specific workouts (an activity done for a certain period of time). Among other things, your app would need to help check whether a proposed collection of workouts would meet a client’s fitness goals.

This file defines classes for such an application (it simplifies things a bit, assuming that everyone would burn the same calories per hour of an activity).

In this set of exercises, we aren’t asking you to write code. Instead, we are going to ask you several questions about how the program runs, which objects get created, and how those objects can be referenced and used. You may wish to work this on paper, keeping in mind the four areas of our memory mapping file.

Within the file, you will see several comments of the form

  // **** THIS IS POINT X IN THE FILE ****

The questions below will sometimes ask you about a specific numbered point based on these labels.

1 The Problems

  1. Assume you have compiled the file, but not yet run it. Which classes, objects, and names are in the memory map at this point?

  2. Assume you have compiled the file, and hit run, but not run any tests or entered expressions at the interactions prompt. Which classes, objects, and names are in the memory map at this point?

  3. When would Java reach/execute the line marked "POINT 2" in the file? (During compilation, after running the file, after evaluating a specific expression–if so, which one, etc)

  4. At the interactions prompt. you enter

      Examples e = new Examples();

    After this expression evaluates, which classes, objects, and names are in the memory map?

  5. Next, you enter the following at the interactions prompt:

      e.lily.doWorkoutsMeetTarget(new Workout(e.jogging, 30),

                                  e.quickYoga)

    1. What is the sequence (in order) of calls to new, calls to methods, and arithmetic computations that get performed while evaluating this expression? Assume the file had been compiled and the previous expression (Examples e = new Examples();) had already been run.

    2. When execution gets to the point marked "POINT 1" (as part of the call to doWorkoutsMeetTarget), which classes, objects, and names are in the memory map?

    3. When the expression finishes (and the answer has printed out at the prompt), which classes, objects, and names are in the memory map?

2 Answers

Here is a link to the answers for these questions.