Data Analytics
# ๐ Java Programming Language โ Part 2/10: Operators & Control Flow #Java #Programming #OOP #ControlFlow #Coding Welcome to Part 2 of our Java series! Today we'll explore operators and control flow structures. --- ## ๐น Java Operators Overview Java providesโฆ
# ๐ Java Programming Language โ Part 3/10: Methods & Functions
#Java #Programming #Methods #Functions #OOP
Welcome to Part 3 of our Java series! Today we'll dive into methods - the building blocks of Java programs.
---
## ๐น What are Methods in Java?
Methods are blocks of code that:
โ๏ธ Perform specific tasks
โ๏ธ Can be reused multiple times
โ๏ธ Help organize code logically
โ๏ธ Can return a value or perform actions without returning
---
## ๐น Method Components
### 1. Simple Method Example
### 2. Method Parameters
### 3. Return Values
---
## ๐น Method Overloading
Multiple methods with same name but different parameters.
---
## ๐น Recursion
A method that calls itself.
### 1. Factorial Example
### 2. Fibonacci Sequence
---
## ๐น Variable Scope
Variables have different scope depending on where they're declared.
---
## ๐น Practical Example: Temperature Converter
---
## ๐น Best Practices for Methods
1. Single Responsibility Principle - Each method should do one thing
2. Descriptive Names - Use verbs (calculateTotal, validateInput)
3. Limit Parameters - Ideally 3-4 parameters max
4. Proper Indentation - Keep code readable
5. Documentation - Use JavaDoc comments
#Java #Programming #Methods #Functions #OOP
Welcome to Part 3 of our Java series! Today we'll dive into methods - the building blocks of Java programs.
---
## ๐น What are Methods in Java?
Methods are blocks of code that:
โ๏ธ Perform specific tasks
โ๏ธ Can be reused multiple times
โ๏ธ Help organize code logically
โ๏ธ Can return a value or perform actions without returning
// Method structure
[access-modifier] [static] return-type methodName(parameters) {
// method body
return value; // if not void
}
---
## ๐น Method Components
### 1. Simple Method Example
public class Calculator {
// Method without return (void)
public static void greet() {
System.out.println("Welcome to Calculator!");
}
// Method with return
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
greet(); // Calling void method
int sum = add(5, 3); // Calling return method
System.out.println("Sum: " + sum);
}
}### 2. Method Parameters
public static void printUserInfo(String name, int age) {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}### 3. Return Values
public static boolean isAdult(int age) {
return age >= 18;
}---
## ๐น Method Overloading
Multiple methods with same name but different parameters.
public class MathOperations {
// Version 1: Add two integers
public static int add(int a, int b) {
return a + b;
}
// Version 2: Add three integers
public static int add(int a, int b, int c) {
return a + b + c;
}
// Version 3: Add two doubles
public static double add(double a, double b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(2, 3)); // 5
System.out.println(add(2, 3, 4)); // 9
System.out.println(add(2.5, 3.7)); // 6.2
}
}---
## ๐น Recursion
A method that calls itself.
### 1. Factorial Example
public static int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}### 2. Fibonacci Sequence
public static int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n-1) + fibonacci(n-2);
}---
## ๐น Variable Scope
Variables have different scope depending on where they're declared.
public class ScopeExample {
static int classVar = 10; // Class-level variable
public static void methodExample() {
int methodVar = 20; // Method-level variable
System.out.println(classVar); // Accessible
System.out.println(methodVar); // Accessible
}
public static void main(String[] args) {
int mainVar = 30; // Block-level variable
System.out.println(classVar); // Accessible
// System.out.println(methodVar); // ERROR - not accessible
System.out.println(mainVar); // Accessible
}
}---
## ๐น Practical Example: Temperature Converter
public class TemperatureConverter {
public static double celsiusToFahrenheit(double celsius) {
return (celsius * 9/5) + 32;
}
public static double fahrenheitToCelsius(double fahrenheit) {
return (fahrenheit - 32) * 5/9;
}
public static void main(String[] args) {
System.out.println("20ยฐC to Fahrenheit: " + celsiusToFahrenheit(20));
System.out.println("68ยฐF to Celsius: " + fahrenheitToCelsius(68));
}
}---
## ๐น Best Practices for Methods
1. Single Responsibility Principle - Each method should do one thing
2. Descriptive Names - Use verbs (calculateTotal, validateInput)
3. Limit Parameters - Ideally 3-4 parameters max
4. Proper Indentation - Keep code readable
5. Documentation - Use JavaDoc comments
โค1