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:
- creation: that, given an integer n, creates a
Wardrobe object with n hooks that initially are all
empty;
- getNumHooks: that returns the number of hooks of the wardrobe;
- assignHook: that, given the code c of an item (a
string), occupies the first empty hook (i.e., the empty hook with the
smallest number) with the item c and returns the number of the
hook, if there is an empty hook; if there is no empty hook, then -1 is
returned and the wardrobe is not modified;
- emptyHook: that, given the number n of a hook, makes
hook n empty;
- getItem: that, given the number n of a hook, returns
the code of the item currently occupying hook n, if the hook is
occupied; null otherwise;
- searchItem: that, given the code c of an item, returns
the number of the hook occupied by the item c, if it exists; -1
otherwise;
- getNumOccupiedHooks: that returns the number of occupied hooks
in the wardrobe;
- reorderWardrobe: that rearranges the items in the wardrobe in
such a way that all hooks up to a certain number are occupied, and the
remaining ones are all free.
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);
}
}