zoukankan      html  css  js  c++  java
  • homework2

    第一题:

    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
    

     

    第一问问得是找出程序的fault,

    fault是代码中的静态错误。就是找到代码中的错误,第一题呢,是找到数组中等于y的数组下标最大的元素的位置,但是在for循环中,循环条件i>0 会导致下标为0 的元素无法被测试,导致程序出现问题。

    第二问是找到一个不会触发错误的测试用例,只要代码不会运行到那个位置就好, 可以是x=null;y=0;会产生nullpointer exception。

    第三问是找到一个测试用例,会运行到产生错误的位置,但是不会产生错误的状态,只要那个等于y的数组最大下标不是0就好,可以x=[1,2,3,4,5,6,7,8,9]; y=9; 预期结果和最终结果都是8

    第四问是找到一个测试用例,会产生错误,但是不会产生失效。一个是x=[1,2,3];y=0;预期和最终结果都是-1

     

    public static intlastZero(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 (inti= 0; i< x.length; i++)
    {
    if (x[i] == 0)
    {
    return i;
    }
    } return -1;
    }
    // test: x=[0, 1, 0]

    第一问:他的目的是找到数组中,下标最大的0的位置,但是它遍历数组的方向是从低到高,选出的的是数组中下标最小的0的位置。

    第二问:没有这种用例

    第三问:x=[0,1,2,3] 预期和最终结果都是0

    第四问:x=[1,2,3,4] 预期和最终结果都是-1

  • 相关阅读:
    CF821E 【Okabe and El Psy Kongroo】
    BZOJ1231: [Usaco2008 Nov]mixup2 混乱的奶牛
    P1896 [SCOI2005]互不侵犯
    QBXT Day 2 记录
    CF467C George and Job
    【luogu P3373 线段树2】 模板
    【luogu P1306 斐波那契公约数】 题解
    【luogu T24743 [愚人节题目5]永世隔绝的理想乡】 题解
    【luogu P1903 [国家集训队]数颜色】 题解
    莫队算法~讲解
  • 原文地址:https://www.cnblogs.com/ningge/p/5263266.html
Copyright © 2011-2022 走看看