Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Assignment - Week 7 (exceptions)

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

b. Write some code to test the toPositiveInt method:


- First with some manual code (in a main() method), testing all 3 scenarios (not a
number, a number but negative, a valid positive number)
- Then write a JUnit test for it, covering also all 3 cases! (not only the positive one)
- Question: how would you test the exception cases, to make sure the
expected type of exception is really thrown? (and only when needed)

c. Write another (static) method ‘​List<Integer> toPositiveInt(List<String> 


list)​ ’ (has same name as first one, so it overloads it)
- It will receive a list of string values, which may be numbers
- It tries to convert each string value to a positive number:
- Use the first method toPositiveInt() to try to convert each string value
- In case a value cannot be converted to a number (one of the 2 types of
exceptions are thrown..), simply ignore it and don’t add it to the result list
- but would be useful to print an error message about this
- Note that this method should never throw exceptions itself, but it may just return
a smaller list (even empty) with only the numbers for which conversion from
string was successful

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?...

1. Sensors and Measurements


We’ve got the following interface:

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 

Implement three types of sensors:


1. A ​constant​ sensor:
a. Is on all the time
b. The methods on and off do nothing
c. The class has a constructor with an int parameter and the measure method
returns the value received as parameter
2. A ​thermometer​ sensor
a. After creation the thermometer is off
b. When the measure method is called it returns a random int between -30 and 30
c. If the thermometer is off the measure method will throw an exception
3. An ​average​ sensor
a. Contains multiple instances of sensors
b. Has an additional method addSensor which adds a new sensor
c. The average sensor is on when all of it’s sensors are on
d. When the average sensor switched on all of its sensors are switched on if not
already on
e. When the average sensor is switched off some of its sensors are switched off (at
least one)
f. The measure method will return an average of the measured values of all its
registered sensors
g. If the measure method is called when the sensor is off or when there are no
sensors registered to the average sensor throw an exception

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

Write the following classes:

1. A ​PowerPlant​ class that:


a. Receives as parameter in the constructor an int that signifies the desired output
of the nuclear plant.
b. Has a method that returns the desired output
c. Has a method soundAlart() which prints an alarm message to console

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.

3. Half and print


a. Declare an ​interface​ named ​Function​ that has a single method named ​evaluate​ that
takes an int parameter and returns an int value.

b. Create a ​class​ named ​Compute​, with a method compute() that:


- takes 2 input parameters:
- an array of int values
- an instance of type Function (so may pass here an instance of any class
which implements that interface)
- returns: an array of same size as input array, but it contains as values the result
of applying the function passed as parameter to each of the elements of the input
(it basically transforms each value in input array by applying the given Function)
- note: the method should NOT affect the array of input values given as parameter
(by updating any of them), but build and return a separate new array!

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?)

You might also like