Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Java Course Basic Knowlwdge

Yyy

Uploaded by

syadavs1972000
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java Course Basic Knowlwdge

Yyy

Uploaded by

syadavs1972000
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

// Java Course

// Lesson 1: Introduction to Java


public class Lesson1 {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

// Lesson 2: Data Types and Variables


public class Lesson2 {
public static void main(String[] args) {
int age = 30;
double salary = 50000.00;
char gender = 'M';
boolean isStudent = false;
String name = "John Doe";
}
}

// Lesson 3: Operators
public class Lesson3 {
public static void main(String[] args) {
int x = 10;
int y = 5;

// Arithmetic Operators
System.out.println(x + y); // 15
System.out.println(x - y); // 5
System.out.println(x * y); // 50
System.out.println(x / y); // 2
System.out.println(x % y); // 0

// Assignment Operators
int a = 10;
a += 5; // a = a + 5;
a -= 3; // a = a - 3;
a *= 2; // a = a * 2;
a /= 4; // a = a / 4;
a %= 3; // a = a % 3;

// Comparison Operators
System.out.println(x == y); // false
System.out.println(x != y); // true
System.out.println(x > y); // true
System.out.println(x < y); // false
System.out.println(x >= y); // true
System.out.println(x <= y); // false
}
}

// Lesson 4: Control Flow


public class Lesson4 {
public static void main(String[] args) {
int age = 20;

// if-else statement
if (age >= 18) {
System.out.println("You are an adult.");
} else {
System.out.println("You are a minor.");
}

// switch statement
int day = 2;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}

// for loop
for (int i = 0; i < 5; i++) {
System.out.println(i);
}

// while loop
int j = 0;
while (j < 5) {
System.out.println(j);
j++;
}
}
}

// Lesson 5: Arrays
public class Lesson5 {
public static void main(String[] args) {
// One-dimensional array
int[] numbers = {1, 2, 3, 4, 5};
System.out.println(numbers[2]); // 3

// Two-dimensional array
int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
System.out.println(matrix[1][2]); // 6
}
}

// Lesson 6: Methods
public class Lesson6 {
public static void main(String[] args) {
int result = add(3, 4);
System.out.println(result); // 7

int result2 = multiply(2, 5);


System.out.println(result2); // 10
}

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


return a + b;
}

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


return a * b;
}
}

// Lesson 7: Object-Oriented Programming (OOP)


public class Lesson7 {
public static void main(String[] args) {
// Creating an object of the Person class
Person person = new Person();
person.name = "John";
person.age = 30;
person.greet();

// Creating an object of the Car class


Car car = new Car();
car.brand = "Toyota";
car.model = "Camry";
car.start();
car.stop();
}
}

class Person {
String name;
int age;

void greet() {
System.out.println("Hello, my name is " + name + " and I am " + age + "
years old.");
}
}

class Car {
String brand;
String model;

void start() {
System.out.println("The " + brand + " " + model + " is starting.");
}

void stop() {
System.out.println("The " + brand + " " + model + " is stopping.");
}
}

// Lesson 8: Inheritance
public class Lesson8 {
public static void main(String[] args) {
// Creating an object of the Student class
Student student = new Student();
student.name = "Alice";
student.age = 20;
student.studentId = "12345";
student.greet();
student.study();
}
}

class Student extends Person {


String studentId;

void study() {
System.out.println("The student is studying.");
}
}

// Lesson 9: Polymorphism
public class Lesson9 {
public static void main(String[] args) {
Animal animal1 = new Animal();
Animal animal2 = new Dog();
Animal animal3 = new Cat();

animal1.makeSound();
animal2.makeSound();
animal3.makeSound();
}
}

class Animal {
void makeSound() {
System.out.println("The animal makes a sound.");
}
}

class Dog extends Animal {


@Override
void makeSound() {
System.out.println("The dog barks.");
}
}

class Cat extends Animal {


@Override
void makeSound() {
System.out.println("The cat meows.");
}
}

// Lesson 10: Exception Handling


public class Lesson10 {
public static void main(String[] args) {
try {
int result = divide(10, 0);
System.out.println(result);
} catch (ArithmeticException e) {
System.out.println("Error: Cannot divide by zero.");
}
}

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


return a / b;
}
}

You might also like