Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Help understanding an exam question (very basic!)  RSS feed

 
Cj Fields
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all!

I had this question in a recent exam and I'd like to see the answer from a more experienced programmer.  I'm very new to Java so I'm hoping to learn where I went wrong.

(Q) Write the class Document with the following features:

- a private instance variable text of type String

- a constructor with a String argument, that initialises text to the value of the argument;

- a public instance method getText() that takes no argument and returns the value of text;  a public instance method merge() that takes an argument of type Document and returns no value. This method should append to the receiver's text a single space, followed by the text of the argument. For example if doc1 is an instance of Document with text "The rain in Spain" and doc2 is an instance of Document with text "falls mainly on the plain", then following the execution of doc1.merge(doc2) the value of text in doc1 will be "The rain in Spain falls mainly on the plain".

- a public instance method with signature boolean equals(Document d)that returns true if the receiver Document and the argument Document contain the same text, that is, if the two texts consist of the same characters in the same order. Otherwise the method should return false.
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I thought seeing the thread title, that you were sitting the exam this very moment
What did you write?
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cj Fields wrote:. . .
- a public instance method with signature boolean equals(Document d) . . .
I missed that bit. That question is of course incorrect, and all answers to that question will be incorrect. What follow is what you shou‍ld have been asked and what you shou‍ld have written:-
a public instance method with signature equals(Object) that returns a boolean, true if the receiver Document and the argument Document contain the same text, that is, if the two texts consist of the same characters in the same order. Otherwise the method should return false.
The name of the parameter and the return type are not part of the method signature.There are different ways to implement the hash code method. If you read its documentation, you will find it must be overridden alongside equals. You can add specified tags to the dpcumentation comments if you wish.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!