0% found this document useful (0 votes)
8 views5 pages

Java Methods Lab Activity Guide

This document outlines a Java lab activity focused on understanding and applying Java methods. Students will learn to declare and call methods, differentiate between methods with and without return values, and apply these methods to mathematical computations through various tasks. The lab includes creating methods for greetings, basic arithmetic operations, factorial computation, array summation, and prime number checking, with specific submission instructions provided.

Uploaded by

kinsleynacua820
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Java Methods Lab Activity Guide

This document outlines a Java lab activity focused on understanding and applying Java methods. Students will learn to declare and call methods, differentiate between methods with and without return values, and apply these methods to mathematical computations through various tasks. The lab includes creating methods for greetings, basic arithmetic operations, factorial computation, array summation, and prime number checking, with specific submission instructions provided.

Uploaded by

kinsleynacua820
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Lab Activity: Java Methods and Their

Applications
Engr. Jamie Eduardo Rosal ,MSCpE

Objective

By the end of this activity, students will be able to:

1.​ Understand how to declare and call methods in Java.​

2.​ Differentiate between methods with and without return values.​

3.​ Pass arguments to methods and use returned results.​

4.​ Apply methods to structure real-world mathematical computations.​

I. Introduction to Java Methods


A method is a block of code that performs a specific task and can be reused.​
It improves code organization, readability, and reusability.

1. Method Syntax
returnType methodName(parameter1, parameter2, ...) {​
// method body​
return value; // only if returnType is not void​
}

●​ returnType — data type of the value returned (e.g., int, double, void if none)​

●​ methodName — name of the method​


●​ parameters — values passed into the method (optional)​

2. Example: Method Without Return Value


public static void greetUser() {​
[Link]("Hello, welcome to the Math Program!");​
}

3. Example: Method With Return Value


public static int addNumbers(int a, int b) {​
int sum = a + b;​
return sum;​
}

Usage:

int result = addNumbers(5, 10);​


[Link]("Sum: " + result);

4. Example: Method With Parameters


public static double computeArea(double radius) {​
return [Link] * radius * radius;​
}

Usage:

double area = computeArea(5.0);​


[Link]("Area of circle: " + area);
II. Lab Activity Tasks

Task 1: Greetings Method

●​ Create a method named greetUser() that prints a greeting message.​

●​ Call this method at the start of your program.​

Task 2: Basic Arithmetic Calculator

●​ Create four methods:​

○​ add(int a, int b)​

○​ subtract(int a, int b)​

○​ multiply(int a, int b)​

○​ divide(double a, double b)​

●​ Each method should perform the respective arithmetic operation and return the result.​

●​ Ask the user for two numbers and display the results of each operation.​

Task 3: Factorial Using a Method

●​ Create a method named factorial(int n) that computes and returns the factorial of
n.​

●​ Call the method and display the result.​


Task 4: Array Summation Method

●​ Create a method named sumArray(int[] arr) that accepts an array and returns the
sum of all its elements.​

●​ Initialize an array with 5 numbers and display the sum.​

Task 5: Prime Number Checker

●​ Create a method named isPrime(int n) that returns true if n is prime and false
otherwise.​

●​ Ask the user for a number, call the method, and display the result.​

III. Sample Output


Hello, welcome to the Math Program!​
Enter two numbers: 10 5​
Addition: 15​
Subtraction: 5​
Multiplication: 50​
Division: 2.0​

Enter a number for factorial: 5​
Factorial: 120​

Sum of array elements: 75​

Enter a number to check prime: 7​
7 is prime.
IV. Submission Instructions
1.​ Save your program as [Link].​

2.​ Implement all 5 tasks in the same file.​

Push your code to GitHub in a repository named:​


3.​ MethodsLab_LastName_FirstName
4.​ Include:​

○​ Your .java file​

○​ A [Link] file with sample outputs​

○​ Screenshot folder of terminal runs​

5.​ Submit the GitHub repository link to your instructor.

You might also like