Problem 1: Define Assertion Terms: Pre Conditions, Post Conditions, and Invariants, Giving

Total Page:16

File Type:pdf, Size:1020Kb

Problem 1: Define Assertion Terms: Pre Conditions, Post Conditions, and Invariants, Giving

Problem 1: Define assertion terms: pre conditions, post conditions, and invariants, giving examples of the difference between pre/post conditions and invariants. Solution: Precondition: a condition that should be true for a method to execute correctly. Example: int findMin(int [] inputArray){ return inputArray [0]; }

For this method, the precondition needs to be defined as: inputArray.length >=1 and inputArray is sorted We can also define it mathematically as: (inputArray.length > 1 AND  i s.t., 0 <= i <= inputArray.length, inputArray[i] <= inputArray [i+1])) OR (inputArray.length = 1).

Postcondition: a condition that will be true after a method terminates. Example: void sort(int [] inputArray){ … … } The postcondition will be: inputArray is sorted Similar to the precondition, we can also define it mathematically.

Invariant (class invariant): a condition that should always be true during the execution (should be true for each object of the class). Example: Assume that we have a class Person and the class has an attribute of type int with name currentMoney. We can define a class invariant: the value of currentMoney should never be less than 0. In this class there may be different methods which may modify the value of this attribute. However, this invariant ensures that none of them assigns a value less than 0.

1 Problem 3: List the problem solving techniques discussed in lecture and give an example problem where each would be useful. Solution:  Write the problem in your own words Ex: The problem can be given as a mathematical formula as: “Given int [] a, s.t. a.length > 0, find i s.t.  j a[i] <= a[j]” We can easily rephrase this problem as: “Find the index of the minimum element in a given array a”  Try to reach the solution by solving a simpler problem Ex: In order to find the 3rd minimum number in an array, we can start with creating a solution for finding the minimum element. By using this solution, the harder problem can be solved. There are multiple ways of solving the problem once we know how to find the minimum. For instance, we can copy the array into a temporary array. Then we find the minimum and change its value to MAX_VALUE of type; we find the minimum again and repeat the same procedure. When we find the minimum a third time, this will be third minimum in the original array.  Try to reach the solution by solving a harder problem Ex: In order to find the second minimum number in an array, you can first sort the array, and then get the element in the second position.  Start from the solution and go backwards Ex: If the problem is to find a solution to the sudoku game, we can start with solving the problem by hand, and trying to find the steps to go from the initial state to the final state. (See http://en.wikipedia.org/wiki/Sudoku for a description of this game.)  Use iterative/recursive refinements Ex: these techniques can be used whenever we need to perform repetitive steps to reach the solution. Sorting, searching are simple examples for using this technique.

2 Problem 4: Analyze, design, implement a solution to randomly generating six two-digit numbers. Solve the problem by parsing the random number mathematically and as a String. Also, show how these solutions could be created iteratively and recursively.

Solution : Iterative mathematical parsing

public static int[] mathParseIterative(){ double randomNumber = Math.random(); int [] numbers = new int[6]; for(int i=0; i<6; i++){ randomNumber *= 100; int current = (int) randomNumber;

// ensure that the number is a 2-digit number while(current < 10){ randomNumber = randomNumber - current; randomNumber *= 100; current = (int) randomNumber; }

numbers[i] = current; randomNumber = randomNumber - current; } return numbers; }

3 Recursive String parsing

public static int[] stringParseRecursive(){ double randomNumber = Math.random(); int [] numbers = new int[6]; String s = (randomNumber + "").substring(2);

return stringParseRecursive(s, 0, numbers); }

public static int[] stringParseRecursive(String s, int index, int [] numbers){

// base case if (index == 6) return numbers;

// if the string is consumed before generating all 2-digit numbers // create a new random number and continue // recursive case 1 else if(s.length() < 2){ double randomNumber = Math.random(); s = (randomNumber + "").substring(2); return stringParseRecursive(s, 0, numbers); }

// recursive case 2 else{ int current = Integer.parseInt(s.substring(0,2)); s = s.substring(2); if (current < 10) return stringParseRecursive(s, index, numbers); else{ numbers[index++] = current; return stringParseRecursive(s, index, numbers); } } }

4 Problem 5: Determine whether and where two squares overlap on the xy-coordinate system. (Assume that both squares lie horizontally)

Solution: This will be a practice of using problem solving techniques to solve a problem. We can start by solving a simpler problem. Then we can extend this solution to solve the harder problem. The simpler problem will be to find if any of the sides that are parallel to the x-axis overlap.

Let’s define the points for both squares:

x1y1 x2y2 x5y5 x6y6

x3y3 x4y4 x7y7 x8y8

We can first check if any of the following are the same: (y1 and y5; y1 and y7; y3 and y5; y3 and y7) If none of these are the same, the sides parallel to the x axis do not overlap. For the ones that are the same, we need to check if the x intervals overlap. For instance, if y1 and y5 are the same, there is a possibility that the top sides of the squares will overlap. In order to find that, we need to check if the intervals [x1, x2] and [x5, x6] overlap. There are 4 cases in which these overlap:

5 1. x1 <= x5 <= x2 <= x6 (overlap in the interval [x5, x2]) 2. x1 <= x5 <= x6 <= x2 (overlap in the interval [x5, x6]) 3. x5 <= x1 <= x6 <= x2 (overlap in the interval [x1, x6]) 4. x5 <= x1 <= x2 <= x6 (overlap in the interval [x1, x2])

Once we have this solution, we can use the same idea to find if the sides that are parallel to the y-axis are overlapping. By solving these two simpler problems, we will eventually find all the overlapping intervals for all sides.

6

Recommended publications