Homework2

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Homework2相关的知识,希望对你有一定的参考价值。

Fault:故障,可能导致系统或功能失效的异常条件

Error:错误,Error是能够导致系统出现Failure的系统内部状态。

Failure:失效,当一个系统不能执行所要求的功能时,即为Failure

找出一个错误的三个条件:reachability,infection,propagation

作业题:

问题:

1 Identify the fault.
2 If possible, identify a test case that does not execute the
fault. (Reachability)
3 If possible, identify a test case that executes the fault, but
does not result in an error state.
4 If possible identify a test case that results in an error, but
not a failure.

PROG1:

public int findLast (int[] x, int y) {
//Effects: If x==null throw
NullPointerException
// else return the index of the last element
// in x that equals y.
// If no such element exists, return -1
for (int i=x.length-1; i > 0; i--)
{
if (x[i] == y)
{
return i;
}
}
return -1;
}
// test: x=[2, 3, 5]; y = 2
// Expected = 0

Answer:

1 for 循环判断条件应为i>=0

2 数组为空,不执行For

3 数组为[1,2,3], y=2,执行了错误程序段,但没有产生错误

4 数组为[0,2,3], y=1,error

 

PROG2:

public static int lastZero (int[] x) {
//Effects: if x==null throw
NullPointerException
// else return the index of the LAST 0 in x.
// Return -1 if 0 does not occur in x
for (int i = 0; i < x.length; i++)
{
if (x[i] == 0)
{
return i;
}
} return -1;
}
// test: x=[0, 1, 0]
// Expected = 2

 

Answer:

1 查找的是数组中第一个0而不是最后一个,for (int i = x.length-1;i >=0;i--)

2 数组为空

3 数组中只含有一个0时  如x=[1,1,0]

4 不存在

 

 

         

 

以上是关于Homework2的主要内容,如果未能解决你的问题,请参考以下文章