Java_Presentation_Updated
Java_Presentation_Updated
Presentation
JAVA KICKOFF
WORKSHOP
Introduction, Variables, Operations, Loops, and
OOP Concepts
Introduction to Java
It is used for:
• Mobile applications (specially Android apps)
• Desktop applications
• Web applications
• Web servers and application servers
• Games
• Database connection
• And much, much more!
Java compiltion
C:\Users\Your Name>javac Main.java
C:\Users\Your Name>java Main
public Main {
public static void main(String[] args) {
int age = 20; // Integer
double price = 99.99; // Floating-point
char grade = 'A'; // Character
boolean isPassed = true; // Boolean
String name = "Java"; // String
Example :
public Main {
public static void main(String[] args) {
int a = 10, b = 5;
System.out.println("Addition: " + (a + b)); // 15
System.out.println("Subtraction: " + (a - b)); // 5
System.out.println("Multiplication: " + (a * b)); // 50
System.out.println("Division: " + (a / b)); // 2
System.out.println("Modulus: " + (a % b)); // 0
}
}
Assignment Operator:
Example:
Example:
Math.max(x,y)
The Math.max(x,y) method can be used to find the highest value of x and y:
Example:t
Math.max(5, 10);
Math.min(x,y)
The Math.min(x,y) method can be used to find the lowest value of x and y:
Example
Math.min(5, 10);
Math.sqrt(x)
The Math.sqrt(x) method returns the square root of x:
Example
Math.sqrt(64);a
Math.abs(x)
The Math.abs(x) method returns the absolute (positive) value of x:
Example
Math.abs(-4.7);
Random Numbers
Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive):
Example
Math.random();
To get more control over the random number, for example, if you only want a random number between 0
and 100, you can use the following formula:
Example
int randomNum = (int)(Math.random() * 101); // 0 to 100
For Loop
When you know exactly how many times you want to loop through a block of code, use
the for loop instead of a while loop:
Syntax:
for (statement 1; statement 2; statement 3) { // code block to be executed }
Example
for (int i = 0; i <= 10; i = i + 2) { System.out.println(i); }
Break:
The break statement can also be used to jump out of a loop.
This example stops the loop when i is equal to 4:
System.out.println("\n\nUsing break:");
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
System.out.print(i );
}
}
}
Object-Oriented
Programming (OOP)
Encapsulation: Wrapping data and methods
Inheritance: Acquiring properties of parent class
Polymorphism: One method, multiple behaviors
Attributes: Variables inside a class
Encapsulation Example
class Person {
private String name;
public void setName(String n) { name = n; }
public String getName() { return name; }
}
Inheritance Example