Day 1: Introduction to Java

icon picker
Day 6: Functions and Methods

Welcome to Day 6 of our Java programming journey! Today, we're going to dive into the world of functions and methods. Functions, also known as methods in Java, are essential building blocks of any program. They allow you to organize your code into reusable blocks, making your code more modular and easier to maintain.

What are Functions/Methods?

In Java, a function is called a "method." A method is a block of code that performs a specific task. It can take input, process it, and return a result. Methods are used to break down complex problems into smaller, manageable pieces. This makes your code more readable, maintainable, and efficient.

Defining and Calling Methods

Let's start by defining a simple method in Java:
javaCopy code
public class MyMethods {
// Method definition
public static void sayHello() {
System.out.println("Hello, world!");
}

public static void main(String[] args) {
// Calling the method
sayHello();
}
}

In the code above, we define a method called sayHello(), and then we call it from the main() method. When you run this program, it will print "Hello, world!" to the console.

Method Parameters and Return Values

Methods can also take parameters (inputs) and return values (outputs). Here's an example:
javaCopy code
public class Calculator {
// Method definition with parameters and a return value
public static int add(int num1, int num2) {
return num1 + num2;
}

public static void main(String[] args) {
int result = add(5, 3); // Calling the method with arguments
System.out.println("The result is: " + result);
}
}

In this example, the add() method takes two integer parameters (num1 and num2) and returns their sum. We call the method with arguments (5 and 3) and store the result in the result variable.

Method Overloading

Java supports method overloading, which means you can define multiple methods with the same name in the same class, as long as they have different parameter lists. Here's an example:
javaCopy code
public class OverloadedMethods {
// Method with two int parameters
public static int add(int num1, int num2) {
return num1 + num2;
}

// Method with two double parameters
public static double add(double num1, double num2) {
return num1 + num2;
}

public static void main(String[] args) {
int intResult = add(5, 3);
double doubleResult = add(2.5, 3.7);
System.out.println("Int result: " + intResult);
System.out.println("Double result: " + doubleResult);
}
}

In this example, we have two add() methods—one that takes integers and another that takes doubles. Java determines which method to call based on the argument types.

Exercise Questions

Write a Java method called calculateSquare that takes an integer as a parameter and returns its square.
Create a method named isEven that takes an integer as input and returns true if the number is even and false if it's odd.
Define a method called printArray that accepts an array of integers and prints each element.
Overload the calculateSquare method to also accept a double as a parameter and return the square.

Exercise Answers

Here's the calculateSquare method:
javaCopy code
public static int calculateSquare(int num) {
return num * num;
}

The isEven method:
javaCopy code
public static boolean isEven(int num) {
return num % 2 == 0;
}

The printArray method:
javaCopy code
public static void printArray(int[] arr) {
for (int num : arr) {
System.out.println(num);
}
}

Overloaded calculateSquare method:
javaCopy code
public static double calculateSquare(double num) {
return num * num;
}

Now, you have a solid understanding of functions/methods in Java, along with some practice exercises to further enhance your skills. Happy coding!




void test(){

}
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.