Professional Documents
Culture Documents
Assignment - Week 7 (Exceptions) : 0. Warm-Up
Assignment - Week 7 (Exceptions) : 0. Warm-Up
0. Warm-up
a. Write a (static) method ‘int toPositiveInt(String value) ’
- It takes as input parameter a String value and tries to convert it to a positive
integer and return it
- In case of errors, it should throw 2 different types of exceptions (you’ll need to
define 2 separate exception classes for it)
- One for the case the string value is not a number at all
- A different one for the case the value is a number, but a negative one
d. After you complete this version of toPositiveInt with List param, also write a few JUnit
tests for it, trying to test all cases you can think of for the input (like: empty list, list of only
valid numbers, list with valid and invalid values..)
- Question: assuming you have the tests written well/completely for 1st method (at
point b), how detailed need your test to be for this 2nd method?
e. Run again your tests for b) d), but this time with Coverage. What is the coverage percent
for each of the 2 versions of the toPositiveInt() method? Do you have any important
code/logic which remained untested? Can you write some new tests to cover those spots
and increase your coverage?
- Question: what do you think is a good minimal coverage percent to aim for, in
practice? (like: 10%, 25%, 50%, 80%, 90%, 99%, 100% ? )
- Question: Is it realistic to aim/require 100% coverage for all code?...
public interface
Sensor{
boolean isOn(); //returns true if the sensor is on
void on(); //switches the sensor on
void off(); //switches the sensor off
int measure();//returns sensor reading if on, throws exception if off
}
Write a class that creates multiple instances of the constant and thermometer sensors, adds
them to an average sensor instance, and calls the methods of each to test the functionality
specified above.
2. Nuclear reactor
A nuclear power plant is comprised of the following parts:
- A reactor and
- A plant controller
2. A Reactor class:
a. The class constructor receives a int that specifies power output level at which the
reactor goes critical.
b. Starts with a power output of 0.
c. Has a method which returns the current output as an int value.
d. Has two methods that increase and decrease the output (with a random value
between 1 and 100).
i. The increase method will throw an exception if the reactor is going critical
(current power output becomes greater than critical level)
ii. The decrease method should not decrease the output to negative values
(but only down to 0)
3. A PlantController class:
a. Receives a PowerPlant and a Reactor instance in its constructor
b. Has a needAdjustment() method that returns true when the difference between
the reactor output and plant desired output differs by a value larger than 10
c. Has a method adjust() which adjust the output by repeatedly calling the
increase() method of the reactor as long as needed (until needAdjustment()
returns false)
d. Has a shutdown() method that repeatedly calls the decrease method of the
reactor until the output is 0
e. Has a run() method that runs constantly and checks whether the reactor needs
adjustment and calls the adjust method. If the reactor goes critical call the sound
alarm method and shutdown the reactor.
Write a test class which creates a power plant with desired output is 120, using an reactor for
which the critical threshold is 150. The test should also create a plant controller, on which it
should call the run() method.
Run the test a few times, and check that only one of these 2 valid scenarios happen:
- The power plant reaches a stable output (with difference between it and the desired
output being less than 10 units), and alarm is not sounded / the power remains running
- OR: after a few increases, the reactor goes critical; then the alarm should be sounded
and the reactor should be shut down, and after a few decrease steps it should reach
current output of 0.
Note: for easier testing, methods should also print some info about the current status of the
power plant / reactor, after each step that changes their states.
c. Create a class named HalfFunction that implements the Function interface. Make the
implementation of the method evaluate() return the value obtained by dividing the int
argument by 2.
d. Create a class named PrintFunction that implements the Function interface; its
implementation of evaluate() method should simply print out the give int value, and then
also return it (as is, no changes).
e. Write in the main() method of a new class (may name it HalfAndPrint) some test code
which does the following:
- creates an array of some int values from 1 to 10
- print the array using an instance of the PrintFunction class from above (and an
instance of the Compute class)
- halves all the values of the array and prints the resulting values, using the
Compute, HalfFunction and PrintFunction classes.
f. Optional: write a JUnit test to test the functionality of Compute and HalfFunction classes
- Question: can you write a similar test for the PrintFunction class? Is it easier or
harder to do than for HalfFunction class? (why?)