Week 4 - Methods and Messages Flashcards
What is a Javadoc comment?
It is a multi line comment that is used in the Java programming language, it allowed web page documentation for the method to be created automatically. /** * */
What are the three parts that make up the method header?
the first part is the access modifier - it tells the complier what other groups of objects can send the corresponding message.
the second part is the type of value returned - if any.
the final part is the name of the method, followed by ().
The following is the method home() in the class Frog.
/** *Resets the receiver to it "home" position of 1. */ public void home() { this.setPosition(1); }
write down the comment, the method header and the method body.
a) /**
*Resets the receiver to it “home” position of 1.
*/
b) public void home()
c) {
this.setPosition(1);
}
What is the difference between a message and a method?
A method is a piece of code that is executed when an object received a message. A message is the event that results from a message-send; it is a request doe an object to do something (execute a method).
What do you need to do to add a new message to the protocol of an object?
Adding a message to the protocol requires editing the related class definition to add a new method and recompiling the class.
How can an object send a message to itself?
In a method you can use the keyword this whenever you want to refer to the object currently executing the method. So this is a ‘self’ reference.
Where and how must we place Javadoc comments?
Javadoc comments must be placed before the method header and be contained within a /** and */.
What is run-time?
while a program is running if a message is sent to it it is said to be sent during run-time.
Write down a Java statement to create an instance of the class Frog referenced by a newly declared variable called aFrog.
Frog aFrog = new Frog();
a new emthod, yellow() is required, write the code for this.
/** *Sets the colour of the receiver to Yellow */ public void yellow() { this.setColour(OUColour.YELLOW); }
What is the declared return type for the method getColour() in the class Frog?
The declared return type is OUColour.
Write down the method header for a method that has the name myMethod(), has no arguments, and returns an int value. Objects of any class should be able to send the corresponding message.
public int myMethod()
Write down the method header for a method that has the name myMethod() that is accessible to any object, has no arguments, and has no return value.
public void myMethod()
Write down the receivers and the arguments of the following message-sends.
a) frog1.sameColourAs(frog3);
b) frog4.setPosition(3);
a) frog1 is the receiver and frog3 is the argument
b) frog4 is the receiver and 3 is the argument.
What is the method signature?
It is the name of a method including the parentheses and the type of any arguments used.
For example, the signature of setPosition() is setPosition(int). The signature shows clearly the type of the expected argument.