ASSIGNMENTS for Lab Session 5


  1. 1

    Write a class FaultyAdder with methods (possibly incorrect) to sum numbers.

  2. 2

    By using JUnit and the relative support in the NetBeans IDE, develop a test class for FaultyAdder with at least two distinct tests; then, run the test session.

  3. 3

    A dormitory object can be defined so that a Java thread can “toSleep” in it up to a wake-up event.
    Design and implement a Dormitory class with a given number of sleeping seats to be specified at construction time; provide it with the following methods:
    toSleep(), to be called to sleep in one of the available seats; in case no seat is available, it will block so to access the dormitory in future sleeping turns;
    trySleep(), to be called to sleep in the current sleeping turn; in case no seat is available, it doesn’t block, returning false, otherwise it succeeds and returns true;
    wakeUpAll() to wake up the current occupants and free their seats; in practice, a single sleeping turn is delimited by two successive invocations of wakeUpAll().
    Sketch also a driver program to show how this synchronization mechanism can be actually used.

  4. 4

    By using JUnit and the relative support in the NetBeans IDE, develop a test class for Dormitory, trying to write tests for the provided synchronization mechanism.