Object methods
- Change an object's state.
-
Example: Scale a rectangle.
- Get dependent values
-
Example: Calculate a rectangle's perimeter.
- Combined
-
Scale a rectangle and calculate its new perimeter.
|
|
|
width=66 height=44 |
public ❶ void ❷ scale❸ (int factor ❹) { ❺ width *= factor; ❻ height *= factor; } |
[access modifier] ❶ return_type ❷ methodName ❸ ([arguments] ❹) {❺ [statement(s)] ❻ } |
Optional access
control modifier either of |
|
The method's return type either of:
|
|
The method's name. |
|
Arguments being required for execution. |
|
Start of method's body. |
|
The method's implementation. |
|
|
|
Perimeter=110 |
No. 90
Compile time error
Q: |
Try to compile the following snippet:
You'll encounter a “Missing return statement” error. What's wrong here? On contrary the following code compiles and executes perfectly well:
|
A: |
The compiler effectively complains about a missing
On the other hand the return statement in
Albeit executing well the above code is flawed: Calling e.g.
|
No. 91
An Address
class
Q: |
Consider the following UK postal address sample code and desired execution result:
Create an appropriate |
||||
A: |
Our Address class requires six attributes of type String and
a
|