首页 > 其他 > 详细

HOMEWORK2

时间:2017-03-03 01:16:35      阅读:188      评论:0      收藏:0      [点我收藏+]
1 public int findLast (int[] x, int y) {
 2 //Effects: If x==null throw NullPointerException
 3 // else return the index of the last element
 4 // in x that equals y.
 5 // If no such element exists, return -1
 6 for (int i=x.length-1; i > 0; i--)
 7 {
 8 if (x[i] == y)
 9 {
10 return i;
11 }
12 }
13 return -1;
14 }
15 // test: x=[2, 3, 5]; y = 2
16 // Expected = 0
 public static int lastZero (int[] x) {
 2 //Effects: if x==null throw NullPointerException
 3 // else return the index of the LAST 0 in x.
 4 // Return -1 if 0 does not occur in x
 5     for (int i = 0; i < x.length; i++)
 6 {
 7     if (x[i] == 0)
 8 {
 9     return i;
10 }
11 } 
12     return -1;
13 }
14 // test: x=[0, 1, 0]
15 // Expected = 2

1.Identify the fault.

第一个程序中的

for (int i=x.length-1; i > 0; i--)

i>0,使得程序不能完全遍历数组中的所有元素。应该更正为i>=0;此时才能完全遍历数组中的所有元素。

第二个程序中

for (int i = 0; i < x.length; i++)

出现了问题,此时查找是从头开始查找,如果输入中有多个0出现则会返回第一个0所处的位置,而题目要求是返回最后一个0所在的位置,因此应该将循环条件改为:

for (int i = x.length-1; i >=0; i--)

2. If possible, identify a test case that does not execute the fault. (Reachability)

第一个程序中可以选用:

// test: x=[5,2,4,7,9]; y = 7
// Expected =3

 第二个程序中可以选用:

// test: x=[5,0,4,7,9];
// Expected =1

3. If possible, identify a test case that executes the fault, but does not result in an error state.

第一个程序可以使用:

// test: x=[2,3,5,8,9,6]; y = 10
// Expected =-1

 第二个程序中可以选用:

// test: x=[5,2,4,7,9]; 
// Expected =-1

4.If possible identify a test case that results in an error, but not a failure

第一个程序可以使用

// test: x=[2, 3, 5]; y = 2
// Expected = 0

 第二个程序中可以选用:

// test: x=[5,2,4,0,0]; 
// Expected =4

HOMEWORK2

原文:http://www.cnblogs.com/lylyj/p/6493240.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!