by 
[Help |
API Docs |
Short Course|
Exercises]
For this exercise, you will define and use some Java methods. The methods are technically instance methods, but you can think of them as plain old functions or procedures from your previous programming experience.
Prerequisites
Skeleton Code
Tasks
Define a method called sayHello in class Methodcall, provided in the skeleton code, that has no
arguments and no return value. Make the body of the method simply print "Hello."
Make the start method of Methodcall call sayHello().
Define a method called addTwo that takes an integer argument, adds 2 to it, and returns that result.
In the start method of Methodcall, define a local integer variable and initialize it to the result of calling addTwo(3). Print out the variable; that is, print out the result of the method call. Define another local integer variable initialize it to the result of calling addTwo(19). Print out its result.
Where help exists, the task numbers above are linked to the step-by-step help page.
Solution Source
Demonstration
If you execute Methodcall from a command line, the output should be similar to the following:
Hello
addTwo(3) is 5
addTwo(19) is 21
Copyright 1996-2000 jGuru.com. All Rights Reserved.
|
|