JAVA FUNCTIONS(METHODS) WORKSHEET
Java Methods are blocks of code that perform a specific task.
A method allows us to reuse code, improving both efficiency and
organization.
All methods in Java must belong to a class.
Class Assignment
Section A – Fill in the Blanks (3 Questions)
Instructions: Complete the missing parts of the code for the given function definitions.
Q1. Fill in the blanks to complete the function that calculates the area of a circle.
public class Circle {
public static double getAreaOfCircle(_______ radius) {
double area = _______ * radius * radius;
return area;
public static void main(String[] args) {
double result = getAreaOfCircle(5);
[Link]("Area = " + _______);
Q2. Complete the function to calculate the perimeter of a rectangle.
public static double getPerimeter(_______, _______) {
double perimeter = 2 * (length + breadth);
return _______;
Q3. Complete the code to display the perimeter of a circle.
public static double getPerimeterOfCircle(double radius) {
double perimeter = 2 * _______ * radius;
return perimeter;
}
Section B – Code Writing Using Functions (5 Questions)
Instructions: Write the required function definitions and main methods based on each
question.
Q4.
Write a function getSquare(int n) that returns the square of the number passed as a
parameter.
Q5.
Write a void function printSum(int a, int b) that takes two integers as input and prints
their sum (does not return it).
Q6.
Write a function getMax(int a, int b) that returns the greater number.
Q7.
Write a function getAverage(double x, double y, double z) that returns the average
of three numbers.
Q8.
Write a void function displayMessage(String name) that prints
"Hello <name>, welcome to Java!"
Section C – Menu-Driven Programs Using Functions (2 Questions)
Instructions: Write complete menu-driven programs as described.
Q9.
Write a menu-driven Java program using functions that performs the following operations
until the user chooses to exit:
1⃣ Add two numbers
2⃣ Subtract two numbers
3⃣ Multiply two numbers
4⃣ Exit
Each operation should be written as a separate function.
Q10.
Write a menu-driven program using functions for area calculation:
1⃣ Area of Circle
2⃣ Area of Rectangle
3⃣ Area of Triangle
4⃣ Exit
Each operation should be written as a separate function.