Java ch5
Java ch5
CHAPTER FIVE
METHODS IN JAVA
Overview
In general, a method is a way to perform some task. Similarly, the method in Java is a
collection of instructions that performs a specific task. It provides the reusability of code. We
can also easily modify code using methods. A method is a block of code or collection of
statements or a set of code grouped together to perform a certain task or operation. It is used
to achieve the reusability of code. We write a method once and use it many times. We do
not require writing code again and again.
It also provides the easy modification and readability of code, just by adding or removing a
chunk of code. The method is executed only when we call or invoke it.
The most important method in Java is the main() method.
Method Declaration
The method declaration provides information about method attributes, such as visibility,
return-type, name, and arguments. It has six components that are known as method header,
as we have shown in the following figure.
Method Signature: Every method has a method signature. It is a part of the method
declaration. It includes the method name and parameter list.
1. Access Specifier: Access specifier or modifier is the access type of the method. It
specifies the visibility of the method. Java provides four types of access specifier:
o Public: The method is accessible by all classes when we use public specifier in our
application.
o Private: When we use a private access specifier, the method is accessible only in the
classes in which it is defined.
o Protected: When we use protected access specifier, the method is accessible within the
same package or subclasses in a different package.
o Default: When we do not use any access specifier in the method declaration, Java uses
default access specifier by default. It is visible only from the same package only.
2. Return Type: Return type is a data type that the method returns. It may have a primitive
data type, object, collection, void, etc. If the method does not return anything, we use void
keyword.
3. Method Name: It is a unique name that is used to define the name of a method. It must
be corresponding to the functionality of the method. Suppose, if we are creating a method
Lab Exercises
5 Gambella University, Department of Computer Science Academic Year – 2015 E.C
Advanced Programming CH#5 – Methods in Java By: Lecturer Abebe A. (MSc, Computer Science)
1. Write a Java method to find the smallest number among three numbers.
Test Data:
Input the first number: 25
Input the Second number: 37
Input the third number: 29
Expected Output:
The smallest value is 25.0
2. Write a Java method to compute the average of three numbers.
Test Data:
Input the first number: 25
Input the second number: 45
Input the third number: 65
Expected Output:
The average value is 45.0
3. Write a Java method to display the middle character of a string.
Note: a) If the length of the string is odd there will be two middle characters.
b) If the length of the string is even there will be one middle character.
Test Data:
Input a string: 350
Expected Output:
The middle character in the string: 5
4. Write a Java method to count all vowels in a string.
Test Data:
Input the string: w3resource
Expected Output:
Number of Vowels in the string: 4
5. Write a Java method to count all words in a string.
Test Data:
Input the string: The quick brown fox jumps over the lazy dog.
Expected Output:
Number of words in the string: 9
6. Write a Java method to compute the sum of the digits in an integer.
Test Data:
Input an integer: 25
Expected Output:
The sum is 7
7. Write Java methods to calculate the area of a triangle.
Expected Output:
Input Side-1: 10
Input Side-2: 15
Input Side-3: 20
The area of the triangle is 72.6184377413890