码迷,mamicode.com
首页 > 其他好文 > 详细

Homework2

时间:2016-03-10 21:58:01      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

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

标签:

原文地址:http://www.cnblogs.com/llexozO/p/5263561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!