// Operators Examples
int a = 15, b = 4;
System.out.println(a + b); // 19
System.out.println(a - b); // 11
System.out.println(a * b); // 60
System.out.println(a / b); // 3
System.out.println(a % b); // 3
int x = 10, y = 20;
System.out.println(x > y); // false
System.out.println(x < y); // true
System.out.println(x == y); // false
System.out.println(x != y); // true
int age = 25;
boolean isStudent = true;
System.out.println(age > 18 && isStudent); // true
System.out.println(age > 30 || isStudent); // true
System.out.println(!(age > 18)); // false
int c = 5;
c += 3;
System.out.println(c); // 8
c *= 2;
System.out.println(c); // 16
int n = 5;
System.out.println(n++); // 5
System.out.println(++n); // 7
System.out.println(n--); // 7
System.out.println(--n); // 5
// Control Statements Examples
int num = 7;
if(num % 2 == 0){
System.out.println("Even");
} else {
System.out.println("Odd");
}
int score = 85;
if(score >= 90){
System.out.println("A");
} else if(score >= 80){
System.out.println("B");
} else {
System.out.println("C");
}
int day = 2;
switch(day){
case 1: System.out.println("Saturday"); break;
case 2: System.out.println("Sunday"); break;
case 3: System.out.println("Monday"); break;
default: System.out.println("Invalid Day");
}
// Loops Examples
for(int i=1; i<=5; i++){
System.out.println("Number: " + i);
}
int i = 1;
while(i<=5){
System.out.println("Count: " + i);
i++;
}
int j = 1;
do{
System.out.println("Value: " + j);
j++;
} while(j<=5);
// Arrays Examples
int[] numbers = {10, 20, 30, 40};
System.out.println(numbers[0]);
for(int k=0; k<numbers.length; k++){
System.out.println(numbers[k]);
}
String[] names = {"Ahmed", "Sara", "Mona"};
for(String name : names){
System.out.println(name);
}
// Methods Examples
public static void sayHello(){
System.out.println("Hello, World!");
}
public static int multiply(int a, int b){
return a * b;
}
public static void main(String[] args){
int result = multiply(5, 3);
System.out.println("Result: " + result);
}
public static int max(int a, int b, int c){
int max = a;
if(b > max) max = b;
if(c > max) max = c;
return max;
}
public static void main(String[] args){
System.out.println(max(10, 50, 30));
}
// Homework Ideas
// 1. Print numbers from 1 to 100
for(int i=1; i<=100; i++){
System.out.println(i);
}
// 2. Sum of even numbers from 1 to 50
int sum = 0;
for(int i=1; i<=50; i++){
if(i % 2 == 0){
sum += i;
}
}
System.out.println("Sum of evens: " + sum);
// 3. Ask user for name and age (needs Scanner)
import java.util.Scanner;
Scanner input = new Scanner(System.in);
System.out.print("Enter your name: ");
String yourName = input.nextLine();
System.out.print("Enter your age: ");
int yourAge = input.nextInt();
System.out.println("Your name is " + yourName + " and your age is " + yourAge);
// 4. Method to get square of number
public static int square(int n){
return n * n;
}
// 5. Array of grades and print highest grade
int[] grades = {50, 70, 85, 90, 65};
int highest = grades[0];
for(int g=1; g<grades.length; g++){
if(grades[g] > highest){
highest = grades[g];
}
}
System.out.println("Highest grade: " + highest);
// 6. Loop to reverse a string
String text = "Hello";
String reversed = "";
for(int i=text.length()-1; i>=0; i--){
reversed += text.charAt(i);
}
System.out.println("Reversed: " + reversed);
// 7. Method to count number of letters in string
public static int countLetters(String s){
return s.length();
}