0% found this document useful (0 votes)
2 views2 pages

Java Functions CheatSheet and Lab

The document provides a Java Functions Cheat Sheet that includes method declaration, return values, parameters, pass-by-value, scope, recursion, modular programming, and code reuse. It also outlines laboratory exercises for practical application, such as creating methods for summation, printing names, calculating squares, demonstrating variable scope, and building a simple calculator. The exercises emphasize the importance of reusable methods and recursion in programming.

Uploaded by

mangacrizeljoy89
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)
2 views2 pages

Java Functions CheatSheet and Lab

The document provides a Java Functions Cheat Sheet that includes method declaration, return values, parameters, pass-by-value, scope, recursion, modular programming, and code reuse. It also outlines laboratory exercises for practical application, such as creating methods for summation, printing names, calculating squares, demonstrating variable scope, and building a simple calculator. The exercises emphasize the importance of reusable methods and recursion in programming.

Uploaded by

mangacrizeljoy89
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 Functions Cheat Sheet & Laboratory Exercises

CHEAT SHEET
1. Method Declaration & Definition
static int add(int a, int b) {
return a + b;
}

2. Return Values
static int square(int x) {
return x * x;
}

static void display() {


[Link]("Hello");
}

3. Parameters & Arguments


static void greet(String name) {
[Link]("Hello " + name);
}

4. Pass-by-Value
static void change(int x) {
x = 10;
}

5. Scope
int globalVar = 10;

void test() {
int localVar = 5;
}

6. Recursion
static int factorial(int n) {
if (n == 0) return 1;
return n * factorial(n - 1);
}

7. Modular Programming
Break programs into reusable methods.
8. Code Reuse
Avoid repetition using reusable methods.

LABORATORY EXERCISES
1 Create a method that returns the sum of two integers.

2 Write a method that prints your name using parameters.

3 Create a method that calculates the square of a number.

4 Demonstrate pass-by-value using a variable.

5 Write a program showing local and global variable scope.

6 Create a recursive method to compute factorial.

7 Build a simple calculator using methods.

8 Create a method that finds the largest of three numbers.

9 Write a method that counts vowels in a string.

10 Create a reusable utility class with at least 3 methods.

You might also like