Free University of Bozen-Bolzano
Bachelor in Production Engineering
Introduction to Programming - A.A. 2005/2006

Exam exercise
Class Wardrobe

We want to manage information on a wardrobe. Each clothing item in the wardrobe is identified by a code (a string). A Wardrobe contains a fixed number of hooks, numbered sequentially starting from 0. Each hook in the wardrobe can be empty or occupied by an item. The Wardrobe objects support the following functionalities:

Part 1. Write a Java class Wardrobe to represent Wardrobe objects.

Solution: representation of the objects, skeleton of the class, class Wardrobe

Alternative solution: representation of the objects, skeleton of the class, class Wardrobe2


Part 2. Realize a static method saveItemAssignment, client of the class Wardrobe that, given a wardrobe g, writes on standard output, one per line, the number of the hook and the code of the item that occupies it, for all occupied hooks in the wardrobe g.

Solution

Alternative solution


Part 3. Describe how memory is managed when methods are activated and when their activation terminates. Specifically, describe when local variables and formal parameters are created and destroyed, and when and how they are initialized. Illustrate the concepts you explain on the following example:

public class Example {
  public static double m(int p) {
    double d;
    d = p / 2.0;
    return d;
  }
  public static void main (String[] args) {
    int a = 5, b = 4;
    double r;
    r = m(a + b);
    System.out.println(r);
  }
}