2011-05-04 87 views
0

我的一些課程沒有通過自動化測試。不幸的是,所述測試沒有提供關於其失敗原因的任何有用信息。這是我的幾個類的代碼。如果你能告訴我我出錯的地方,我會非常感激。評論應該解釋每種方法應該做什麼。這段代碼在哪裏出錯?

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

如果沒有關於失敗的信息,很難告訴你什麼是錯的。你提到他們沒有通過測試......嗯......什麼測試? – 2011-05-04 19:38:45

+2

你有沒有考慮過給ownerName分配一些東西? – Fredrik 2011-05-04 19:39:05

+0

你可以發佈你正在運行的測試,哪些測試失敗? – Jerome 2011-05-04 19:39:45

回答

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; 
} 
+0

'TextMessagingPhone'也是如此。 – 2011-05-04 19:57:07

+0

是固定的。非常感謝! – user738647 2011-05-04 20:04:45

2
public CellPhone(String owner) { 

     } 

你不指定任何東西ownerName ......

public CellPhone(String owner) { 
      ownerName = owner; 
     } 
+0

很好的接收,也許這是它 – 2011-05-04 19:45:10

+0

耶是我的壞。我解決了這個問題,但那不是導致這個失敗的原因 – user738647 2011-05-04 19:55:00

0

入住這

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

您正在使用 「發件人」,這是對字符串表達式中的對象。在公開或者定義getOwnerName並使用它之後使用sender.ownerName應該可以工作。同樣的錯誤重複了幾次!