Practice Problems:   Tracing Object Creation and Method Execution – Solutions

Practice Problems: Tracing Object Creation and Method Execution – Solutions

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

    Answer: The four classes in the file – Activity, Client, Workout, and Examples are in the known classes area. There are no objects and no known names.

  2. Question: 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?

    Answer: The four classes in the file – Activity, Client, Workout, and Examples are in the known classes area. There are no objects and no known names. Running runs the main method in the Main class, but does not run anything else.

  3. Question: 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)

    Answer: Java would only reach point 2 when it created an object from the Examples class. That can happen if you type

      new Examples()

    at the interactions prompt (or in another class for which you’ve made an object). In this specific example, running tests would NOT make an object of the Examples class because there are no @Test-annotated methods in the class. If you added test methods, then Java would reach point 2 as part of running the tests.

  4. Question: At the interactions prompt. you enter

      Examples e = new Examples();

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

    Answer:
    • Known classes contains Activity, Client, Workout, and Examples. If you also had the Main.java file in your project when you compiled, the Main class would also be known.

    • Objects contains the five objects created with new in the Examples class. There is an Activity object with the description "a brisk run", and so on. Your answer to this should NOT mention the names running, calmYoga, etc. Those names are not part of the objects area – they are in the Known Names area.

      This separation between objects and known names is important. If I were at the interactions prompt and entered

        new Client("Pat", 550, 2)

      there would be a new object in the objects space, but no new name in the Known Names space. This implies that I would have no way to access or use this new object (because I failed to associate a name with it). In order to access the object, I have to associate a name using =, as in

        Client patClient = new Client("Pat", 550, 2)

      (Here, I used the known name patClient to illustrate that there is no connection between the known name in memory and the name field within the object).

    • Known Names contains running, calmYoga, jogging, lily, and quickYoga. Each of these points to/references the object on the right side of the corresonding = sign.

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

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

                                  e.quickYoga)

    1. Question: 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 expression (Examples e = new Examples();) had already been run.

      Answer:

      • new Workout(e.jogging, 30)

      • doWorkoutsMeetTarget(<the new workout object>, e.quickYoga)

      • day1.calBurned()

      • day2.calBurned()

      This illustrates that when you make a method call, first the arguments get computed, then you call the method, then you evaluate within the method. I’ve written <the new workout object> in the second line because the object only gets created once (in the first line), then gets passed as the argument when it is time to call the method.

    2. Question: 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?

      Answer:

      • Known classes contains Activity, Client, Workout, and Examples. Running the code does not change the known classes once the code is compiled.

      • Objects contains all of the objects from question 4, an object from the Examples class (because we made a new Examples object at the top of question 4), and a Workout object with jogging as the activity and 30 as the duration (the one created with the new command).

      • Known Names contains
        • e, which refers to the Examples class object.

        • this, which refes to the client object with "Lily" in the name field

        • day1, which refers to the new Workout object created as part of the method call.

        • day2, which refers to the object named quickYoga in the Examples class.

        Note that the names in the Examples class (running, calmYoga, etc) are NOT in the Known Names area! If you want to use one of the objects associated with those names (say, the running object), you need to write

          e.running

        If you write just running anywhere outside of the Examples class, Java won’t be able to find it. Names created within classes are only visible within objects in the class. Outside the class, you need to access the name through an object of that class (such as e).

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

      Answer: The collections of classes and objects have not changed. The known names, however, contains only e (the Examples class object). The other names, which are either parameters to the method or this, go away after the method has returned.