Week 4 - Methods and Messages Flashcards

1
Q

What is a Javadoc comment?

A
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.
/**      
*
*/
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the three parts that make up the method header?

A

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 ().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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

a) /**
*Resets the receiver to it “home” position of 1.
*/
b) public void home()
c) {
this.setPosition(1);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the difference between a message and a method?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What do you need to do to add a new message to the protocol of an object?

A

Adding a message to the protocol requires editing the related class definition to add a new method and recompiling the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can an object send a message to itself?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Where and how must we place Javadoc comments?

A

Javadoc comments must be placed before the method header and be contained within a /** and */.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is run-time?

A

while a program is running if a message is sent to it it is said to be sent during run-time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write down a Java statement to create an instance of the class Frog referenced by a newly declared variable called aFrog.

A

Frog aFrog = new Frog();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

a new emthod, yellow() is required, write the code for this.

A
/**
*Sets the colour of the receiver to Yellow
*/
public void yellow()
{
this.setColour(OUColour.YELLOW);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the declared return type for the method getColour() in the class Frog?

A

The declared return type is OUColour.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

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.

A

public int myMethod()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

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.

A

public void myMethod()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write down the receivers and the arguments of the following message-sends.

a) frog1.sameColourAs(frog3);
b) frog4.setPosition(3);

A

a) frog1 is the receiver and frog3 is the argument

b) frog4 is the receiver and 3 is the argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the method signature?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

when we say that a method, or method name, has been overloaded, what do we mean?

A

When methods have the same name but different signatures are present. for example,

public void sameColourAs (Frog aFrog)
public void sameColourAs (Toad aToad)

17
Q

Why would these two methods not be allowed?

public void doSomething (int marks)
private int doSomething (int comission)

A

They have the same signature, namely doSomething(int). The access modifier and return type is not part of the signature, nor are the names of any formal arguments.

The Java compiler would report an error if you attempted to compile a class that defined two such methods.

18
Q

What are the signatures of the getColour() and setColour() methods in the Frog class?

A

Since the getter takes no arguments, its signature is getColor(). The setter has the signature setColour(OUColour).

19
Q

What is the signature of the method:

public void setPositionAndColour(int aPosition, OUColour aColour)

A

setPositionAndColour(int, OUColour)

note that the order of the arguments is important.

20
Q

Give two examples of class members.

A

Instance variables and methods are examples of what are called class members. Each instance variable and method is a class member.

21
Q

Summarise the the meaning of the access modifiers private and public.

A
  • if a member is declared as private, only code within the class itself can directly access it;
  • if a member is declared a public, code within any class (and in M250 code within the OUWorkspace) can directly access it.
22
Q

How is encapsulation implemented in Java?

A

In Java you can defined classes that are templates for the creation of objects. These objects then encapsulate both state and behaviour.

23
Q

How do you implement data hiding in Java?

A

Data Hiding is implemented by declaring instance variable as private.

24
Q

When does composition occur?

A

This occurs when objects of one class have instance variables that reference objects of other classes.