我在这个代码中哪里出错了?

时间:2011-05-04 19:36:59

标签: java

我的一些课程没有通过自动化测试。不幸的是,所述测试没有提供有关它们失败原因的任何有用信息。这是我的几个类的代码。如果你能告诉我哪里出错了,我真的很感激。评论应该解释每种方法应该做什么。

public class CellPhone {
    protected String ownerName;

        public CellPhone(String owner) {
           ownerName = owner;


        }

    public String receiveCall(CellPhone sender) {
        // returns a String of the form:
        // owner's name " is receiving a call from " sender's name
        String receivingCall = ownerName + " is receiving a call from " + sender;

        return receivingCall;
    }

    public String call(CellPhone receiver) {
        // returns a String by using the receiver to invoke receiveCall
        // while passing in the current phone
        String invokingReceiveCall = receiver.receiveCall(receiver);

        return invokingReceiveCall;

    }
}

public class TextMessagingPhone extends CellPhone {
    private int availMessages;

    public TextMessagingPhone(String owner, int messageLimit) {
        // invokes the superclass constructor
        super(owner);
        // sets the new instance variable
        availMessages = messageLimit;
    }

    public TextMessagingPhone(String owner) {
        // invokes the other constructor of this class with 15 as the message limit
        this(owner, 15);
    }

    public String receiveText(TextMessagingPhone sender, String message) {
        // decreases the number of messages available to send
        availMessages--;

        // returns a String of the form:
        // owner's name " has received TEXT from " sender's name ":" message
        String receivedText = ownerName + " has received TEXT from " + sender + ":" + message;

        return receivedText;
    }

    public String sendText(TextMessagingPhone receiver, String message) {
        // decreases the number of messages available to send
        availMessages--;

        // returns a String by using the receiver to invoke receiveText
        // while passing in the current phone and the message
        String invokingReceiveText = receiver.receiveText(receiver, message);

        return invokingReceiveText;
    }
}

3 个答案:

答案 0 :(得分:2)

 public CellPhone(String owner) {

        }

您没有为ownerName ...

指定任何内容
 public CellPhone(String owner) {
            ownerName = owner;
        }

答案 1 :(得分:2)

当手机拨打电话时,它会将接收器作为参数传递,因此接收方认为它正在从自身接收。它也永远不会从传递的发件人那里获得名称。尝试:

public String receiveCall(CellPhone sender) {        
    // returns a String of the form:        
    // owner's name " is receiving a call from " sender's name        
    String receivingCall = ownerName + " is receiving a call from " + sender.getName();        
    return receivingCall;    
}

public String call(CellPhone receiver) {        
    // returns a String by using the receiver to invoke receiveCall        
    // while passing in the current phone        
    String invokingReceiveCall = receiver.receiveCall(this);        
    return invokingReceiveCall;    
}

public String getName() {
    return ownerName;
}

答案 2 :(得分:0)

检查

        String receivingCall = ownerName + " is receiving a call from " + sender;

您正在使用“sender”,它是字符串表达式中的对象。在将public.ownerName公开或定义getOwnerName并使用它之后使用它应该有效。同样的错误再重复几次!