ECSE 321 – Intro. to Software Engineering
Winter 2013
ECSE 321 Assignment 2 Instructions: This assignment is worth a total of 40 marks. The assignment is due by noon (12pm) on Friday, April 5th 2013. The preferred method of submission is to submit a written hardcopy of your answers to the assignment box located at the second floor of Trottier building. The assignment is to be completed individually. You may discuss solutions with classmates, but the final work you submit must be written by you in your own words. Late assignments are subject to a penalty of 5% of the maximum available marks per day late. All late assignments are to be submitted in the instructor’s mailbox, located in McConnell 633, and a message should also be sent to instructor and TA that the assignment is there.
Question 1 – Designing by Contract (9 Marks) Based on the following description of the class Stack, write out some skeleton Java code using Contract4J syntax to define invariant, precondition, and postcondition rules. Recall that Contract4J uses the annotations @Contract, @Pre, @Post, and @Invar, and it also uses the variable $return to denote the value returned by a method. In Contract4J, you can also use the variable $old in a postcondition to refer to a value before the method is called, you can use the variable $this to refer to the current instance of a class, and you can use the variable $args[i] to refer to the ith argument of a method. For example, a postcondition for a method incrementBy(int amount) could be @Post (“$this.value = $old($this.value) + $args[0]”). A Stack is a datastructure for last-in first-out storage of Items. Stack has five public methods: • isEmpty() returns a boolean that is true if the stack is empty and false otherwise • count() returns an integer with the number of items in the stack • top() returns the topmost Item on the stack without removing it • push(Item) pushes a new Item onto the top of the stack • pop() returns the topmost Item on the stack and removes it Write a skeleton for class Stack that has two invariants (hint: the value returned by isEmpty() should always be consistent with count(); also, what is the value range of count()?), and isEmpty() has one postcondition, top() has one precondition, push() has three postconditions, and pop() has one precondition and one postcondition. Solution: @Contract public class Stack { @Invar (“(isEmpty()) == (count() == 0)”) @Invar (“count() >= 0”) @Post (“$return == (count() == 0)”) public boolean isEmpty() {...} public int count() {…}
1/6
ECSE 321 – Intro. to Software Engineering
Winter 2013
@Pre (“count() > 0”) // could also be “!isEmpty()” public Item top() {…} @Post (“count() = $old(count()) + 1”) @Post (“top() = $args[0]”) @Post (“!isEmpty()”) public void push(Item newItem) {…} @Pre (“count() > 0”) @Post (“count() = $old(count()) - 1”) public Item pop() {…} }
Question 2: Black-box Testing (14 Marks) We have a new marking system that takes a .in file as an argument and produces a sorted .out file as specified below. Input File: The input file format is as follows. Each line will contain the data for a single student. The input will contain the following fields. A semicolon separates each field. Input fields: Student number (9 digit number) Month of Birth (As a 3 character case insensitive string e.g. Jan, FEB, mar,... Dec) Date of Birth (1-31, but must be valid for the month) First name (String of letters without numbers) First letter of surname (Letter) Number of missed lectures (0-26) Slept during one or more lectures (true/false, written as T or F) Example: Student number: 199226129 Month of Birth: May Date of Birth: 26 First name: Kevin First letter of surname: C Number of missed lectures: 0 Never slept during lectures. Input line corresponding to the example: 199226129;May;26;Kevin;C;0;F If any input row is invalid, the program should print a warning message and continue with the next record. If the program encounters a more serious problem (e.g., unable to open input file), it will print an error message and exit gracefully.
Output File: The output file format is as follows. Each line will contain the data for a single student. The output will contain the following fields. Each field is separated by a semicolon.
2/6
ECSE 321 – Intro. to Software Engineering
Winter 2013
Output fields: Student number (9 digit number) Assignment 1 mark: (0-5) Assignment 2 mark: (0-25) Assignment 3 mark: (0-10) Exam mark: (0-60) Final grade: [F, D, C, B-, B, B+, A-, A] Comment: (String of arbitrary length) Example: Student number: 199226129 Assignment 1 mark: 5 Assignment 2 mark: 25 Assignment 3 mark: 10 Exam mark: 60 Final grade: A Comment: Excellent. Output line corresponding to the example: 199226129;5;25;10;60;A;Excellent
Questions: 1) What is the range of possible legitimate inputs for this system? (7 marks) Solution:
- Student number: integers 000000000 to 999999999 - Month of birth: strings “Jan”, “Feb”, “Mar”, “Apr”, “May”, Jun”, Jul”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec” (not case sensitive) - Date of birth: 1 to 31 (valid for the month) - First name: all possible combination of strings [A, Z]∪[a, z] of any size - First letter of surname: exactly one character from [A, Z]∪[a, z] - Number of missed lectures: integer 0 to 26 - Slept during one or more lectures: string “T” or “F” 2) Derive input test-cases for the program using boundary value analysis. (7 marks) Solution: Test studentID: - (blank); May; 26; Kevin; C; 0; T - tt; May; 26; Kevin; C; 0; T - 998; May; 26; Kevin; C; 0; T - -111; May; 26; Kevin; C; 0; T - 1000000000; May; 26; Kevin; C; 0; T Test month: - 111111111; (blank); 26; Kevin; C; 0; T - 111111111; 05; 26; Kevin; C; 0; T - 111111111; TTT; 26; Kevin; C; 0; T - 111111111; February; 26; Kevin; C; 0; T - 111111111; Feb; 26; Kevin; C; 0; T
3/6
ECSE 321 – Intro. to Software Engineering
Winter 2013
Test day: - 111111111; Jan; (blank); Kevin; C; 0; T - 111111111; Jan; 0; Kevin; C; 0; T - 111111111; Jan; -1; Kevin; C; 0; T - 111111111; Feb; 30; Kevin; C; 0; T - 111111111; Feb; 31; Kevin; C; 0; T - 111111111; Apr; 31; Kevin; C; 0; T - 111111111; Jun; 31; Kevin; C; 0; T - 111111111; Sep; 31; Kevin; C; 0; T - 111111111; Nov; 31; Kevin; C; 0; T - 111111111; Dec; 32; Kevin; C; 0; T - 111111111; Jan; o; Kevin; C; 0; T Test firstname: - 111111111; Feb; 26; (blank); C; 0; T - 111111111; Feb; 26; Kevin7; C; 0; T - 111111111; Feb; 26; Kevin#; C; 0; T - 111111111; Feb; 26; 999; C; 0; T Test first letter of last name: - 111111111; Feb; 26; Kevin; (blank); 0; T - 111111111; Feb; 26; Kevin; 9; 0; T - 111111111; Feb; 26; Kevin; #; 0; T - 111111111; Feb; 26; Kevin; ”w”; 0; T - 111111111; Feb; 26; Kevin; TT; 0; T Test number of missed lectures: - 111111111; Feb; 26; Kevin; C; (blank); T - 111111111; Feb; 26; Kevin; C; -1; T - 111111111; Feb; 26; Kevin; C; e; T - 111111111; Feb; 26; Kevin; C; 27; T Test slept during lecture: - 111111111; Feb; 26; Kevin; C; 0; (blank) - 111111111; Feb; 26; Kevin; C; 0; 0 - 111111111; Feb; 26; Kevin; C; 0; L - 111111111; Feb; 26; Kevin; C; 0; false - 111111111; Feb; 26; Kevin; C; 0; ,
Problem 3 – White-Box Testing (17 marks) Consider the following pseudo-code for a method that reads from a file that contains one floating-point value on each line, computes the maximum and minimum values encountered, and then displays information about the range.
void findMaxMin(List scoreList) { int numberOfScores = 0; float maxScore = NegativeInfinity; float minScore = PositiveInfinity; 1 float nextScore; Iterator iterator = scoreList.iterator();
4/6
ECSE 321 – Intro. to Software Engineering
2
Winter 2013
while (iterator.hasNext()) { nextScore = scoreList.next();
4 6
if (nextScore > maxScore) { maxScore = nextScore; } if (nextScore < minScore) { minScore = nextScore; } numberOfScores++;
3 5 7
8
}
9
if (numberOfScores < 1) { System.out.println(“No scores found in score list“); } else { System.out.println(numberOfScores + “ total scores found in score list”); System.out.println(“The maximum score is “ + maxScore); System.out.println(“The minimum score is “ + minScore); }
10 11
}
a.
Identify and label each conditional statement and each contiguous block of code (sequence of statements that always execute together). You may mark these directly on the code above [5 marks] Solution: see above
b.
Draw a logic flow diagram (flow chart) for this method in the space below. [6 marks] Solution: see diagram in the next page
c.
What is the smallest number of test cases that must be specified in order to test each path through this method? Justify your answer by describing each test in the space provided. [6 marks]
Solution: 5 tests are sufficient.
File is empty; for the path 1->2->9->10 File with one score (e.g. 50); for the path 1->2->3->4->5->6->7->8->9->11 File with two scores in increasing order (e.g. 50, 100), for the path 1->2->3->4->5->6->8>9->11 File with two scores in decreasing order (e.g. 50, 20), for the path 1->2->3->4->6->7->8>9->11 File with three scores where the last score is the median (e.g. 50, 20, 30), for the path 1>2->3->4->6->8->9->11
Note that the paths 1->2->3….->9->10 are infeasible, i.e. you cannot at the same time enter the while loop (block 2) and make the if statement of block 9 be true.
5/6
ECSE 321 – Intro. to Software Engineering
Winter 2013
Start
1
2 3 4 5 6 7 8
9 11
10
End
6/6