Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Introduction to Software Engineering

Homework 9

Vakhtang Pheradze
1)
a)

Intermediate language code:


10: int factorial = 1;

20: if not (n>=0) go to 90

30: if not (n==0) go to 50

40: return 1;

50: factorial *= n;

60: n--;

70: if not (n>1) go to 110

80: go to 50

90: String errMsg = String.format("%d < 0", n);

100: throw new illegalArgimentException(errMsg);

110: return factorial;

b)
Statement Coverage:

To achieve statement coverage, we need to ensure that every statement in the code is executed at
least once.

Minimal Test Case for Statement Coverage:

Test Case: computeFactorial(3)

This test case covers all the statements in the method.

Branch Coverage:

Minimal Test Cases for Branch Coverage:

Test Case: computeFactorial(0)

This covers the branch where n == 0, and it returns 1.

Test Case: computeFactorial(5)

This covers the branch where n >= 0, and the loop is executed.

Test Case: computeFactorial(-2)

This covers the branch where n < 0, and an exception is thrown.

Paths Traversed:

For path coverage, we need to consider different paths through the code. The paths are outlined
below:

Path 1 (n >= 0): computeFactorial(5)

Enters the loop and completes it.

Path 2 (n == 0): computeFactorial(0)

Skips the loop and returns 1.

Path 3 (n < 0): computeFactorial(-2)

Throws an exception.

c)
Certainly, here's a rewritten version:

Obtaining complete path coverage for code containing loops poses a formidable challenge due to the
exponential increase in possible paths with each additional loop iteration. For instance, when the
loop runs multiple times, attempting to cover paths with 0, 1, 2, and 3 iterations becomes
impractical, leading to an exhaustive number of test cases.

In real-world scenarios, a pragmatic approach often involves prioritizing statement coverage and
branch coverage. These metrics strike a balance between testing thoroughness and feasibility. By
ensuring the execution of each statement and taking each branch at least once, one can attain
confidence in the fundamental functionality and logic of the code.

2)
a) Equivalence Classes and Representatives:

1. String Length <= maxWidth:

- Representative: "Hello"

- Description: This class includes strings where the length is less than or equal to maxWidth. The
function should return the input string unchanged.

2. String Length > maxWidth:

- Representative: "This is a longer string"

- Description: This class includes strings where the length is greater than maxWidth. The function
should abbreviate these strings to a length of maxWidth - 3 by appending "..." to the end.

3. maxWidth < 4:

- Representative: 3

- Description: This class includes cases where maxWidth is less than 4. The function should throw an
IllegalArgumentException.

b) Boundary Values:

- For the String Length <= maxWidth class:

- Minimum: Empty string ("")

- Maximum: String with length equal to maxWidth

- For the String Length > maxWidth class:

- Minimum: String with length equal to maxWidth + 1


- Maximum: String with a very large length

- For the maxWidth < 4 class:

- Minimum: 0

- Maximum: 3

3)

Rule Line Description


Number Number
10 14 The class name 'Triangle' is descriptive and follows Java naming conventions.
11 15 Instance variables 'a', 'b', 'c', 'boundingBox', 'color', and 'crit' are declared
with appropriate types.
12 18 The variable 'crit' is initialized to 0.
13 - No literals in the code that should be declared constant.
14 15, 17 The 'color' and 'boundingBox' variables could potentially be declared as
constants if their values are not intended to change.
15 15, 17 No indication that class variables should be local variables or vice versa.
16 15, 17 All instance variables ('a', 'b', 'c', 'boundingBox', 'color', 'crit') have private
visibility, which seems appropriate for this class.
17 - No indication of instance variables that should be class variables or vice
versa.
18 8-Jun The import statements for 'BoundingBox' and 'IPrimitive' are present, but it's
unclear whether they are used. Further code analysis is needed.
20 28 The constructor's name 'Triangle' is descriptive and follows Java naming
conventions.
21 48-56 The 'isInsidePrimitive' method takes a 'Point' parameter but does not check
for null input. Input validation is missing.
22 48-56 The 'isInsidePrimitive' method returns true or false based on conditions,
which seems correct. However, it lacks comments explaining its purpose and
logic.
23 47-72 All methods have default visibility, which is appropriate for this class.
24 - No indication of instance methods that should be class methods or vice
versa.
30 14-72 No JavaDoc comments are provided for the class and methods. JavaDoc
comments should be added to describe the purpose and usage of the class
and its methods.
40 - The indentation appears to be correct and consistent.
41 - All blocks have curly braces.
50 - No specific instances where values can be cached instead of recalculated.
51 66 The 'getColor' method always returns null, which may not be the intended
behavior. Add logic to return the actual color if it is set.
52 61 The 'getBoundingBox' method returns the 'boundingBox' instance variable
directly. Consider returning a copy or an immutable version to prevent
unintended modifications.

You might also like