Activity: RIPR (countPositive)

(no submission)

Purpose:
Instruction:

Consider the countPositive method and answer the questions.

You may make a copy of a worksheet and complete this activity, or write your answer on paper(s) or your computer.

/**
 * Count positive elements
 *
 * @param x array to search
 * @return count of positive elements in x
 * @throws NullPointerException if x is null
 */
line  1  public int countPositive (int[] x)  
line  2  { 
line  3     int count = 0;  
line  4     for (int i=0; i < x.length; i++)
line  5     { 
line  6        if (x[i] >= 0)
line  7           count++; 
line  8     } 
line  9     return count;
line 10   }
// Given test case
//   test input: x = [-4, 2, 0, 2] 
//   expected = 2 
  1. What is the fault? Explain what is wrong with the given code.
  2. If possible, find a test input that does not reach (i.e., not execute) the fault. If not, briefly explain why not.
  3. If possible, find a test input that reaches the fault, but does not result in an error. If not, briefly explain why not.
  4. If possible, find a test input that results in an error, but not a failure. If not, briefly explain why not.
  5. Find a test input that results in a failure. If not, briefly explain why not.
  6. Identify the first error state for the given test (how about your test cases from questions 4 and 5?)

Copyright © 2025 Upsorn Praphamontripong
Released under the Creative Commons License CC-BY-NC-SA 4.0 license.
Last updated 2025-08-28 11:24