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

Understanding Java Methods Explained

A method in programming is a block of code that performs a specific task, enhancing code reusability and readability. The document outlines various types of methods in Java, including predefined and user-defined methods, and provides examples of methods with different parameter and return value configurations. It emphasizes the benefits of using methods, such as avoiding duplicate code and making code modular.

Uploaded by

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

Understanding Java Methods Explained

A method in programming is a block of code that performs a specific task, enhancing code reusability and readability. The document outlines various types of methods in Java, including predefined and user-defined methods, and provides examples of methods with different parameter and return value configurations. It emphasizes the benefits of using methods, such as avoiding duplicate code and making code modular.

Uploaded by

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

What is a Method?

🔹 A method is 🔹 It is used to
a block of code improve code
that performs reusability and
a specific task. readability.
Method Syntax

returnType methodName(parameters) {
// body of method
return value;
}

int add(int a, int b) {


return a + b;
}
Types of
Methods Type of Method Description
in Java Predefined Already available in
Methods Java
User-defined Created by the
Methods programmer
Type Parameter Return Value

With
parameter & ✅ ✅
User-
with return Defined
With
parameter & ✅ ❌ Method
without return
Types
Without
parameter & ❌ ✅
with return

Without
parameter & ❌ ❌
without return
import [Link].*;
public class Main
{
//with parameter with return type
public static int add(int x,int y)
{
return x+y;
}
public static void main(String []args)
{
Scanner s=new Scanner([Link]);
int a=[Link]();
int b=[Link]();
[Link](add(a,b));
}

}
import [Link].*;
public class Main
{
//with parameter without return type
public static void add(int x,int y)
{
[Link](x+y);
}
public static void main(String []args)
{
Scanner s=new Scanner([Link]);
int a=[Link]();
int b=[Link]();
add(a,b);
}

}
import [Link].*;
public class Main
{
//without parameter with return type
public static int add()
{
Scanner s=new Scanner([Link]);
int a=[Link]();
int b=[Link]();
return a+b;
}
public static void main(String []args)
{
[Link](add());
}

}
import [Link].*;
public class Main
{
//without parameter without return type
public static void add()
{
Scanner s=new Scanner([Link]);
int a=[Link]();
int b=[Link]();
[Link](a+b);
}
public static void main(String []args)
{
add();
}

}
•Why Use Methods?
•🔹 Avoid writing duplicate code
🔹 Easy to debug and modify
🔹 Makes code modular and readable
🔹 Reusability: Use it many times with different values
problem
solving
Solve With
Parameter &
With Return ..?
import [Link];

public class Main {


public static boolean isSpyNumber(int num) {
int sum = 0, product = 1;
while (num > 0) {
int digit = num % 10;
sum += digit;
product *= digit;
num /= 10;
}
return sum == product;
}
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int number = [Link]();

if (isSpyNumber(number))
[Link]("Spy Number");
else
[Link]("Not a Spy Number");
}
}
Solve With
Parameter &
Without Return
public static void main(String[] args) {
import [Link]; Scanner sc = new Scanner([Link])
[Link]("Enter a number: ");
public class Main { int number = [Link]();
public static void checkAutomorphic(int num) { checkAutomorphic(number);
int square = num * num; }
int temp = num; }

while (temp > 0) {


if (temp % 10 != square % 10) {
[Link]("Not an Automorphic
Number");
return;
}
temp /= 10;
square /= 10;
}
[Link]("Automorphic Number");
}
Solve Without
Parameter &
With Return
public class Main {
public static boolean isMagic() {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int num = [Link]();
while (num > 9) {
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
num = sum;
}
return num == 1;
}
public static void main(String[] args) {
if (isMagic())
[Link]("Magic Number");
else
[Link]("Not a Magic Number");
}
}
public class Main {
public static boolean isPalindrome(int num) {
int original = num, reversed = 0;
while (num > 0) {
reversed = reversed * 10 + num % 10;
num /= 10;
}
return original == reversed;
}

public static void main(String[] args) {


[Link](isPalindrome(121)); // true
[Link](isPalindrome(123)); // false
}
}

You might also like