Lec 6 - Methods
Lec 6 - Methods
JAVA PROGRAMMING
(BICT 102)
Lecture 6
Methods
Ho Technical University 1
Methods
sum += i;
System.out.println("Sum from 1 to 10 is " +
sum);
sum = 0;
for (int i = 20; i <= 37; i++)
sum += i;
System.out.println("Sum from 20 to 37 is " +
sum);
sum = 0;
for (int i = 35; i <= 49; i++)
sum += i;
System.out.println("Sum from 35 to 49 is " +
Ghana Technology University College
sum);
Sep 27, 202 3
Methods
int result = 0;
for (int i = firstInt; i <= secondInt; i++)
result += i;
return result;
}
public means
any class can requires the
Name of the
use this method word static
method
{
System.out.println("My address is ");
{
public static void main (String [ ] args) {
{
Name of argument
int result = myAge*myAge
System.out.println("When you double my age,
you get " + result);
}
squareAge(30);
}
{
int result = 0;
Parameters
result = i + j;
Overloading Methods
{
System.out.println(s + " " + num + "
times");
}
}
public class DisplayOverloading
{
public static void main(String args[])
{
Dog fido = new Dog();
fido.bark("woah!");
fido.bark("woah!",10);
}}
Sep 27, 202 Ho Technical University 20
Recap
protected)
• The return type for the method
• The method name
• An open parentheses
• An optional list of method arguments (you separate
the arguments with commas if there is more than one
• A closing parentheses