0% found this document useful (0 votes)
30 views7 pages

Day 5 and 6

Introduction to basic java

Uploaded by

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

Day 5 and 6

Introduction to basic java

Uploaded by

Irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

1.

Methods in Java

A method is a block of code that performs a specific task. You define a method once, and
then it can be called (or invoked) multiple times throughout the program, helping in code
reusability and modularization.

Syntax of a Method:

returnType methodName(parameters) {

// method body

return value; // if the method returns something

 returnType: The type of value the method returns. It can be any data type or void if it does
not return anything.
 methodName: The name of the method. It should be meaningful and follow camel case
(e.g., calculateSum).
 parameters: Input values passed to the method. If no input is required, the parameter list is
empty.

Example of a Simple Method:

public class Main {

public static void main(String[] args) {

greet(); // calling the method

// Method definition

public static void greet() {

System.out.println("Hello, Welcome to Java Programming!");

In this example, the greet() method doesn't take any arguments and doesn't return any value
(its return type is void).

2. Parameters and Return Types


Method with Parameters:

You can pass values to methods through parameters.

Example:

public class Main {

public static void main(String[] args) {

int a = 10;

int b = 20;

int sum = addNumbers(a, b); // calling method with arguments

System.out.println("Sum: " + sum);

// Method definition with parameters

public static int addNumbers(int x, int y) {

return x + y; // returns the sum of x and y

Here, the method addNumbers takes two int parameters and returns their sum.

Return Statement:

The return statement is used to return a value from the method to the caller.

Example:

public static int multiply(int a, int b) {

return a * b;

The return type of the method is int, and it returns the product of a and b.

3. Method Overloading
3. Method Overloading allows you to define multiple methods with the same name but
different parameter lists. This is useful when you want the same functionality with different
types of input.

Example:

public class Main {

public static void main(String[] args) {

System.out.println(add(10, 20)); // calls the method with two int parameters

System.out.println(add(5.5, 3.3)); // calls the method with two double parameters

// Overloaded method for integers

public static int add(int a, int b) {

return a + b;

// Overloaded method for doubles

public static double add(double a, double b) {

return a + b;

In this example, both methods are named add, but they differ in the type of parameters they
accept (one accepts integers, the other accepts doubles).

4. Recursion

Recursion is a process where a method calls itself in order to solve a problem. A recursive
function typically has two parts:

 Base case: The condition under which the recursion ends.


 Recursive case: The part where the function calls itself to solve a smaller part of the
problem.

Example: Factorial Using Recursion:


public class Main {

public static void main(String[] args) {

int num = 5;

int result = factorial(num); // calling the recursive method

System.out.println("Factorial of " + num + " is " + result);

// Recursive method to find factorial

public static int factorial(int n) {

if (n == 0) {

return 1; // base case: factorial of 0 is 1

} else {

return n * factorial(n - 1); // recursive case

In this example, the method factorial calls itself to compute the factorial of a number.

5. Practice Problems (Day 5–6)

Problem 1: Calculate the Sum of Two Numbers Using a Method

Write a method that takes two numbers as arguments and returns their sum.

public class Main {

public static void main(String[] args) {

int sum = addNumbers(10, 20); // calling the method

System.out.println("Sum: " + sum);

// Method to calculate the sum of two numbers

public static int addNumbers(int a, int b) {


return a + b;

Problem 2: Check if a Number is Prime

Write a method that checks if a given number is prime.

public class Main {

public static void main(String[] args) {

int num = 29;

boolean result = isPrime(num); // calling the method

if (result) {

System.out.println(num + " is a prime number.");

} else {

System.out.println(num + " is not a prime number.");

// Method to check if a number is prime

public static boolean isPrime(int n) {

if (n <= 1) {

return false;

for (int i = 2; i <= Math.sqrt(n); i++) {

if (n % i == 0) {

return false;

return true;

}
Problem 3: Fibonacci Series Using Recursion

Write a recursive method to print the Fibonacci series up to 10 terms.

public class Main {

public static void main(String[] args) {

int n = 10;

for (int i = 0; i < n; i++) {

System.out.print(fibonacci(i) + " ");

// Recursive method to calculate the Fibonacci series

public static int fibonacci(int n) {

if (n == 0) {

return 0;

} else if (n == 1) {

return 1;

} else {

return fibonacci(n - 1) + fibonacci(n - 2); // recursive call

Problem 4: Find the Greatest Common Divisor (GCD) Using Recursion

Write a recursive method to find the GCD of two numbers.

public class Main {

public static void main(String[] args) {

int num1 = 56;

int num2 = 98;


int result = gcd(num1, num2); // calling the recursive method

System.out.println("GCD of " + num1 + " and " + num2 + " is " + result);

// Recursive method to calculate the GCD

public static int gcd(int a, int b) {

if (b == 0) {

return a; // base case

} else {

return gcd(b, a % b); // recursive case

6. Important Points to Remember

 Method Overloading: Multiple methods can have the same name if they differ in the
number or type of parameters.
 Recursion: A method that calls itself. Make sure to define a base case to avoid infinite
recursion.
 Parameters: Methods can take input in the form of parameters, and the values of these
parameters are passed when the method is called.
 Return Statement: If a method returns a value, you must specify the return type in the
method signature.

Day 5–6 Goals

 Understand how to define and use methods in Java.


 Master method overloading for different parameter types.
 Learn recursion and be able to solve problems like factorial and Fibonacci series.
 Practice writing and calling methods for various scenarios.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy