2013-05-08 4 views
-2
public class CellPhone { 

    //REMINDER: protected fields can be accessed directly by any 
    //   class which extends this one 
    protected String ownerName; 




    public CellPhone(String ownerNameIn) { 
     //initialize ownerName as ownerNameIn 
ownerName = ownerNameIn; 

    } 

    public String receiveCall(CellPhone sender) { 
     //return a String of the form: 
     // receiver's name " is receiving a call from " sender's name 
     //you can implement this by using the receiver to invoke receiveCall 
     // while passing in the current phone 

     String receiveCall = sender.ownerName + " is receiving a call from " + ownerName; 
     return receiveCall; 

    } 

    public String call(CellPhone receiver) { 
     //return a String by using the receiver to invoke receiveCall 
     // while passing in the current phone 

     return this.receiveCall(receiver); 


    } 
} 


package cellPhones; 

public class TextMessagingPhone extends CellPhone { 
    //number of messages owner can send and receive 
    //REMINDER: private fields can't be accessed by class which extends this one 
    private int availMessages; 

    public TextMessagingPhone(String owner) { 
     //Initialize ownerName as owner and availMessage as 15 by invoking the 
     // two-parameter constructor of this class. 


this(owner,15); 
    } 

    public TextMessagingPhone(String owner, int messageLimit) { 
     //initialize ownerName as owner and availMessage as messageLimit 
     //part of this will require invoking the superclass constructor 
     // and then setting the new instance variable 
super(owner); 
availMessages = messageLimit; 





    } 

    public String receiveText(TextMessagingPhone sender, String message) { 
     //The owner receives message from sender. 

     //decrease the number of messages available to receive 

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

     return receivedText; 


    } 

    public String sendText(TextMessagingPhone receiver, String message) { 
     //decrease the number of messages available to send 

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

      return invokingReceiveText; 
    } 
} 

package cellPhones; 

public class SmartPhone extends TextMessagingPhone { 

    public SmartPhone(String ownerIn) { 
     //Invoke the super class' copy constructor and send it owner 

     //NOTE: There's nothing else to do since SmartPhone adds no 
     //  new fields. 
     super(ownerIn); 

    } 



    public String displayPicture(String pictureSubject) { 
     //return a String of the form: 
     // owner's name " now displaying picture of " pictureSubject 


     String picture = ownerName + " now displaying picture of " + pictureSubject; 
     return picture; 
    } 


    /* 
    * This method OVERRIDES the inherited receiveCall method. 
    * Smartphones "display" a photo of the caller. 
    */ 
    public String receiveCall(CellPhone sender) { 
     //return a String built from: 
     // the result of calling displayPicture with the sender's owner's name 
     // concatenated with a dash and then concatenated with the 
     // result of invoking the superclass' receiveCall with the sender 
String call = this.displayPicture(ownerName) + "-" + sender.ownerName; 
return call; 

    } 



    public String receivePictureAndTextMessage(
      SmartPhone sender, String messageText, String picDescription) { 
     //owner receives messageText from sender with picDescription 

     //return a String built from: 
     // the result of calling displayPicture with the picDescription 
     // concatenated with a dash and then concatenated with the 
     // result of invoking the receiveText method with the sender 
     // and the messageText 

     String picText = this.displayPicture(picDescription) + "-" + this.receiveText(sender,messageText); 
     return picText; 
    } 



    public String sendPictureAndTextMessage(
      SmartPhone receiver, String messageText, String picDescription) { 
     //owner sends messageText to receiver with picDescription 

     //return a String built by having the receiver invoke the 
     // receivePictureAndTextMessage method, sending in the 
     // current phone, the messageText, and the picture description 

     return receiver.receivePictureAndTextMessage(receiver, messageText, picDescription); 
    } 
} 

Ошибка я получаю со вторым и третьим классами и он говорит «получил TEXT от [[email protected]]: Что делать RU> но: < ... получил TEXT от [Текст Синди phone]: Что делать?> ". Кто-нибудь знает, как я могу это исправить?Я получаю сообщение об ошибке для некоторых классов, которые я написал, но не могу понять, что я сделал не так?

+0

Вы сравнения по умолчанию 'выходной toString' с выходом, который вы создаете вручную , хотя на самом деле вы не включаете тесты, так что это предположение. Также невозможно прочитать свой код. Если вы собираетесь документировать методы, используйте фактические Javadocs, а не внедряйте их внутри метода. –

+0

ну чего вы ожидали? Почему он выводит то, что вы говорите, что ожидаете? – ApproachingDarknessFish

ответ

2

Вам необходимо напечатать TextMessagingPhone.ownerName, а не объект TextMessagingPhone. Или вы можете переопределить метод toString(), чтобы вернуть ownerName.

2

Каждый раз, когда вы видите печать следующим образом: [[email protected]] означает, что вы печатаете идентификатор экземпляра (в этом случае отправитель).

Вам необходимо переопределить метод ToString() в этом классе и вызвать sender.toString() вместо того, чтобы просто sender в

String receivedText = ownerName + " has received TEXT from " + sender + ":" + message;

+0

Я понял, спасибо! –

Смежные вопросы