在.equals函数之后使用什么参数来使示例测试通过?

时间:2018-02-05 21:25:54

标签: java

为什么

public class CodePair {
    /* Attribute Declarations */

    private char c;
    private String code;

    /* Constructor initializes the character and code
     *  in the pair 
     */
    public CodePair(char c, String code) {
        this.c = c;
        this.code = code;       
    }

    public String getCode() {
        return this.code;
    }

    public char getCharacter() {
        return this.c;
    }

    /**
     * setCharacter method sets the CodePairs character value
     * @param c
     */

    public void setCharacter(char c) {
        this.c = c;
    }

    /**
     * setCode method sets the CodePairs code value
     * @param code
     */

    public void setCode(String code) {
        this.code = code;
    }

    /**
     * equals determines whether two CodePairs have the same character value
     * @param anotherPair other CodePair object that this is compared to
     * @return true if they have the same character
     */

    public boolean equals (CodePair anotherPair) {  
    if (this.c == anotherPair.c)
        return true;
    else
        return false;

    }

}





public class ArrayCode {
    /* Attribute Declarations */


    private CodePair[] codeList;
    private int numPairs;

    /* Constructor */

    public ArrayCode (int size) {
        codeList = new CodePair[size];
        numPairs = 0;

    }

    public void add (CodePair pair) {
        if(numPairs == codeList.length)
            expandCapacity();   
        codeList[numPairs] = pair;
        numPairs++;

    }

    private void expandCapacity() {
        if(codeList.length <= 100) {
            CodePair[] largerList = new CodePair[codeList.length*2];
            for(int i = 0; i <codeList.length; i++)
                largerList[i] = codeList[i];            
            codeList = largerList;
        }

        else {
            CodePair[] largerList = new CodePair[codeList.length + 20];
            for(int i = 0; i <codeList.length; i++)
                largerList[i] = codeList[i];        
            codeList = largerList;

        }   

    }

    private void reduceCapacity() {
        if((numPairs*4)<codeList.length) {
            CodePair[] smallerList = new CodePair[codeList.length/2];
            for(int i = 0; i <codeList.length; i++)
                smallerList[i] = codeList[i];
            codeList = smallerList; 
        }
    }

    public void remove(CodePair pairToRemove) {
        int i = 0;
        while ((i<numPairs) && !codeList[i].equals(pairToRemove)) {
            i++;
        }
        if(i != numPairs) {
            codeList[i] = codeList[numPairs - 1];
            codeList[numPairs - 1] = null;
            numPairs --;
            reduceCapacity();

        }       
    }

    public int findCode(String code) {

        int i = 0;
        while ((i<numPairs) && !codeList[i].equals(code)) {
            i++;
        }
        if(codeList[i].equals(code)) {
            return i;
        }
        else {
            return -1;
        }

    }

    public int findCharacter(char c) {

        int i = 0;
        while ((i<numPairs) && !codeList[i].equals(c)) {
            i++;
        }
        if (codeList[i].equals(c)) {
            return i;
        }
        else {
            return -1;
        }

    }

    public String getCode(int i) {

        if(i < 0 || i >= numPairs) {

        return null;
        }
        else {
            return codeList[i].getCode();
        }
    }

    public char getCharacter(int i) {

        if (i < 0 || i >= numPairs) {

            return 0;
        }
        else {
            return codeList[i].getCharacter();
        }   
    }

    public int getSize() {

        return codeList.length;

    }

    public int getNumPairs() {

        return numPairs;
    }   
}

此代码不适用于此测试?我认为它与我的.equals参数以及返回有关(不确定它是否应该只是我)但我很难过为什么它不起作用。我是否错误地声明了变量?任何帮助都会受到高度赞赏,我对Java非常陌生,所以可能存在非常愚蠢和明显的错误。我想我也在某种程度上搞乱了ArrayCode类中的构造函数,这可能就是为什么大多数方法都没有工作/做他们应该做的事情。

这两个类都在单独的文件中,但我在帖子中将它们粘贴在一起,因此更容易看到。还有其他测试也失败但我想如果我能解决这个问题,它将帮助我指出正确的方向来解决其他问题。

Test:

try {
    test = true;
    for (int i = 1; i <= 20; ++i) {
    if (list.findCharacter((char)i) != (i-1)) test = false;
    if (list.findCode(Integer.toBinaryString(i)) != (i-1)) test = false;
    }

    if (test) System.out.println("Test 3 passed");
    else System.out.println("Test 3 failed");
}
catch (Exception e) {
    System.out.println("Test 3 failed");
}

测试2失败:

try {
    test = true;
    list.remove(new CodePair((char)10,"1010"));
    if (list.findCode("1010") != -1 || list.findCharacter((char)10) != -1)
    test = false;

    if (list.getSize() != 20 || list.getNumPairs() != 19) test = false;

    if (test) System.out.println("Test 5 passed");
    else System.out.println("Test 5 failed");
}
catch (Exception e) {
    System.out.println("Test 5 failed");
}

2 个答案:

答案 0 :(得分:0)

Char是原始类型,请改用“==”。

codeList [i] == c

答案 1 :(得分:0)

您正在将CodePair类型的对象与charString进行比较。但是,您还没有覆盖.equals(Object),因此调用委托给==,对于不同类型,该值始终为false。我认为你打算在两种情况下比较CodePair对象的字段。这可以按如下方式完成:

codeList[i].getCode().equals(code); // Strings compared by .equals()

codeList[i].getCharacter() == c; // Primitive chars compared by ==