Programming Exercises

graphics/pe_icon.gif

5.1

Create different versions of a program that finds all the primes below 100. Create one version that uses only the for loop (i.e., no while or do-while). Create another version that uses only the while loop.

5.2

Here is a skeleton of a system for simulating a nuclear power plant. Implement the methods in the class named Control. Modify the method declarations if necessary. The Javadoc comments for each method give a description of what the implementation should do. Some of the methods in the other classes have unspecified implementations. Assume that these methods have been properly implemented and provide hooks to the rest of the system.

/** A PowerPlant with a reactor core. */
public class PowerPlant {
    /** Each power plant has a reactor core. This has package
        accessibility so that the Control class that is defined in
        the same package can access it. */
    Reactor core;

    /** Initializes the power plant, creates a reactor core. */
    PowerPlant() {
        core = new Reactor();
    }

    /** Sound the alarm to evacuate the power plant. */
    public void soundEvacuateAlarm() {
        // ... implementation unspecified ...
    }

    /** Get the level of reactor output that is most desirable at this time.
        (Units are unspecified.) */
    public int getOptimalThroughput() {
        // ... implementation unspecified ...
        return 0;
    }

    /** The main entry point of the program: sets up a PowerPlant
        object and a Control object and lets the Control object run the
        power plant. */
    public static void main(String[] args) {
        PowerPlant plant = new PowerPlant();
        Control ctrl = new Control(plant);
        ctrl.runSystem();
    }
}

/** A reactor core that has a throughput that can be either decreased or
    increased. */
class Reactor {
    /** Get the current throughput of the reactor. (Units are
	    unspecified.) */
    public int getThroughput() {
        // ... implementation unspecified ...
        return 0;
    }

    /** @returns true if the reactor status is critical, false otherwise. */
    public boolean isCritical() {
        // ... implementation unspecified ...
        return false;
    }

    /** Ask the reactor to increase throughput. */
    void increaseThroughput() throws ReactorCritical {
        // ... implementation unspecified ...
    }

    /** Ask the reactor to decrease throughput. */
    void decreaseThroughput() {
        // ... implementation unspecified ...
    }
}

/** This exception class should be used to report that the reactor status is
    critical. */
class ReactorCritical extends Exception {}

/** A controller that will manage the power plant and make sure that the
    reactor runs with optimal throughput. */
class Control {
    PowerPlant thePlant;

    public Control(PowerPlant p) {
        thePlant = p;
    }

    /** Run the power plant by continuously monitoring the
        optimalThroughput and the actual throughput of the reactor. If
        the throughputs differ by more than 10 units, adjust the reactor
        throughput. If the reactor status becomes critical, the evacuate alarm is
        sounded and the reactor is shut down.
        <p>The runSystem() method can handle the reactor core directly
        but calls methods needAdjustment(), adjustThroughput(), and shutdown()
        instead. */
    public void runSystem() {
        // ... provide implementation here ...
    }

    /** Reports whether the throughput of the reactor needs adjusting.
        This method should also monitor and report if the reactor status becomes
        critical.
        @return true if the optimal and actual throughput values
        differ by more than 10 units. */
    public boolean needAdjustment() {
        // ... provide implementation here ...
    }

    /** Adjust the throughput of the reactor by calling increaseThroughput() and
        decreaseThroughput() methods until the actual throughput is within 10
        units of the target throughput. */
    public void adjustThroughput(int target) {
        // ... provide implementation here ...
    }

    /** Shut down the reactor by lowering the throughput to 0. */
    public void shutdown() {
        // ... provide implementation here ...
    }
}