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.
I thought seeing the thread title, that you were sitting the exam this very moment
What did you write?
I missed that bit. That question is of course incorrect, and all answers to that question will be incorrect. What follow is what you should have been asked and what you should have written:-
Cj Fields wrote:. . .
- a public instance method with signature boolean equals(Document d) . . .
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.
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.
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
The WEB SERVICES and JAX-RS Coursehttps://coderanch.com/t/690789/WEB-SERVICES-JAX-RS