Constructor Preconditions
Precondition #1
In many cases, your code will not work with certain inputs. A frequent
example: How will your method
behave if it is passed a null pointer? Have a look at the javadoc of the BoundedArray
constructor. It clearly mentions the precondition as 'The input array to the constructor cannot be null.'
You will find the following code in the constructor checking this precondition.
//Precondition #1 The input array to the constructor cannot be null. if(array==null){ throw new NullPointerException("Constructor precondition " + "not met: Input array cannot be null"); }
A unit test case for this precondition is provided in the main method:
case 1: { // 1: Constructor array=null System.out.println("Passing null array. Expecting error statement."); int[] testArray=null; BoundedArray b=new BoundedArray(testArray, 0, 100); } break;Run the program, and type in the number
1
(to run test case #1).
A null array is passed in the constructor and an error message
is expected which you will find in the output console.
Precondition #2
The BoundedArray class in its constructor takes in an array and two integers,
minVal
and maxVal
. This class has the following two class invariants:minVal
must be less than or equal tomaxVal
.- All values of the array must be in the range
minVal
andmaxVal
, INCLUSIVE.
The following code is present in the existing constructor. This checks 'precondition #2' which ensures that the 'class invariant #1' is not yet violated.
//Precondition #2 'minVal' must be less than or equal to 'maxVal' if(maxVal < minVal){ throw new RuntimeException("Constructor precondition " + "not met: 'minVal' IS NOT less than or equal to 'maxVal'."); }The test case to check this precondition can be found in the main method
case 2: { // 2: Constructor minVal>maxVal System.out.println("Passing minVal>maxVal. Expecting error statement."); int[] testArray={1,2,3,4,5}; BoundedArray b=new SearchArray(testArray1, 10, 9); } break;Run the code, and type in the number
2
to run testcase number 2. The argument minVal is passed as 10 and maxVal as 9,
clearly violating the second precondition. An error message is expected in this test case and you should see an exception thrown in the console. This
just tells you that the precondition checking code is in place and working.
Precondition #3
Objective 1 for this lab
Your first objective today is to write code in the constructor which ensures that third precondition is not violated. We need to make sure that every element of the array is greater than or equal to 'minVal' and less that or equal to 'maxVal'
Insert your code in the following section of the constructor:
// // Precondition #3 All values of the array must be in the range // 'minVal' and 'maxVal' INCLUSIVE // INSERT CODE HEREYou should then write two test cases in main() to check that. Your test case should go in the following sections of main():
case 3: { // 3: Constructor array elements < minVal // INSERT CODE HERE } break; case 4: { // 4: Constructor array elements > maxVal // INSERT CODE HERE } break;