0% found this document useful (0 votes)
15 views5 pages

Java Programming Concepts Collection

The document contains a collection of 21 Java programs demonstrating various programming concepts such as encapsulation, abstraction, polymorphism, inheritance, exception handling, and basic algorithms. Each program is structured with a main class and showcases different functionalities like string manipulation, arithmetic operations, and system management. This collection serves as a practical guide for understanding Java programming fundamentals.

Uploaded by

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

Java Programming Concepts Collection

The document contains a collection of 21 Java programs demonstrating various programming concepts such as encapsulation, abstraction, polymorphism, inheritance, exception handling, and basic algorithms. Each program is structured with a main class and showcases different functionalities like string manipulation, arithmetic operations, and system management. This collection serves as a practical guide for understanding Java programming fundamentals.

Uploaded by

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

21 Java Programs Collection

1. Encapsulation and Abstraction


class Student {
private String name;
private int age;

public String getName() { return name; }


public void setName(String name) { [Link] = name; }
public int getAge() { return age; }
public void setAge(int age) { [Link] = age; }
}

abstract class Animal {


abstract void sound();
}

class Dog extends Animal {


void sound() { [Link]("Bark"); }
}

public class Main {


public static void main(String[] args) {
Student s = new Student();
[Link]("Bhupathi");
[Link](18);
[Link]("Name: " + [Link]() + ", Age: " + [Link]());
Animal a = new Dog();
[Link]();
}
}

2. Polymorphism and Inheritance


class Animal {
void sound() { [Link]("Animal sound"); }
}
class Dog extends Animal {
void sound() { [Link]("Dog barks"); }
}
class Cat extends Animal {
void sound() { [Link]("Cat meows"); }
}
public class Main {
public static void main(String[] args) {
Animal a1 = new Dog();
Animal a2 = new Cat();
[Link]();
[Link]();
}
}

3. Method and Constructor Overloading


class Example {
void display(int a) { [Link]("Integer: " + a); }
void display(String b) { [Link]("String: " + b); }
Example() { [Link]("Default constructor"); }
Example(int x) { [Link]("Parameterized constructor: " + x); }
public static void main(String[] args) {
Example e1 = new Example();
Example e2 = new Example(10);
[Link](5);
[Link]("Hello");
}
}
4. Interface Implementation
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() { [Link]("Dog barks"); }
}
public class Main {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
}
}

5. Exception Handling
public class Main {
public static void main(String[] args) {
try {
int a = 10, b = 0;
int c = a / b;
} catch (ArithmeticException e) {
[Link]("Error: Division by zero");
}
}
}

6. Handle Unchecked Exception


public class Main {
public static void main(String[] args) {
try {
String s = null;
[Link]([Link]());
} catch (NullPointerException e) {
[Link]("Caught NullPointerException");
}
}
}

7. Swap Two Numbers


public class Main {
public static void main(String[] args) {
int a = 10, b = 20;
int temp = a;
a = b;
b = temp;
[Link]("a = " + a + ", b = " + b);
}
}

8. Reverse a String
public class Main {
public static void main(String[] args) {
String str = "Bhupathi";
String rev = "";
for (int i = [Link]() - 1; i >= 0; i--) {
rev += [Link](i);
}
[Link]("Reversed: " + rev);
}
}

9. Palindrome String
public class Main {
public static void main(String[] args) {
String str = "madam";
String rev = new StringBuilder(str).reverse().toString();
[Link]([Link](rev) ? "Palindrome" : "Not Palindrome");
}
}

10. Prime Number Check


public class Main {
public static void main(String[] args) {
int n = 7, count = 0;
for (int i = 1; i <= n; i++) if (n % i == 0) count++;
[Link](count == 2 ? "Prime" : "Not Prime");
}
}

11. Leap Year Check


public class Main {
public static void main(String[] args) {
int year = 2024;
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
[Link]("Leap Year");
else
[Link]("Not Leap Year");
}
}

12. Basic Arithmetic Operations


import [Link];
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int a = [Link]();
int b = [Link]();
[Link]("Add: " + (a + b));
[Link]("Sub: " + (a - b));
[Link]("Mul: " + (a * b));
[Link]("Div: " + (a / b));
}
}

13. Factorial of Number


public class Main {
public static void main(String[] args) {
int n = 5, fact = 1;
for (int i = 1; i <= n; i++) fact *= i;
[Link]("Factorial: " + fact);
}
}

14. Fibonacci Sequence


public class Main {
public static void main(String[] args) {
int n = 10, a = 0, b = 1;
for (int i = 1; i <= n; i++) {
[Link](a + " ");
int sum = a + b;
a = b;
b = sum;
}
}
}

15. Employee Payroll System


class Employee {
String name;
double salary;
Employee(String n, double s) { name = n; salary = s; }
double calculatePay() { return salary; }
}
public class Main {
public static void main(String[] args) {
Employee e = new Employee("Bhupathi", 50000);
[Link]("Employee Pay: " + [Link]());
}
}

16. Compare Two Strings Without equals()


public class Main {
public static void main(String[] args) {
String a = "hello", b = "hello";
boolean same = [Link]() == [Link]();
for (int i = 0; i < [Link]() && same; i++)
if ([Link](i) != [Link](i)) same = false;
[Link](same ? "Equal" : "Not Equal");
}
}

17. Bank Account Management


class Bank {
private double balance;
public void deposit(double amt) { balance += amt; }
public void withdraw(double amt) { if (balance >= amt) balance -= amt; }
public double getBalance() { return balance; }
}
public class Main {
public static void main(String[] args) {
Bank b = new Bank();
[Link](1000);
[Link](300);
[Link]("Balance: " + [Link]());
}
}

18. Student Grades Analysis


public class Main {
public static void main(String[] args) {
int[] grades = {80, 90, 75, 60, 95};
int sum = 0, max = grades[0], min = grades[0];
for (int g : grades) {
sum += g;
if (g > max) max = g;
if (g < min) min = g;
}
[Link]("Average: " + sum / [Link]);
[Link]("Highest: " + max);
[Link]("Lowest: " + min);
}
}

19. Online Banking System


import [Link].*;
class Bank {
double balance = 1000;
void checkBalance() { [Link]("Balance: " + balance); }
void deposit(double amt) { balance += amt; }
void withdraw(double amt) { if (balance >= amt) balance -= amt; }
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
Bank b = new Bank();
[Link](500);
[Link](200);
[Link]();
}
}

20. Sum and Average of Array


public class Main {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int sum = 0;
for (int x : arr) sum += x;
double avg = (double) sum / [Link];
[Link]("Sum: " + sum);
[Link]("Average: " + avg);
}
}

21. Car Rental System


class Car {
String model;
double rate;
Car(String m, double r) { model = m; rate = r; }
}
public class Main {
public static void main(String[] args) {
Car c = new Car("Sedan", 1500);
int days = 5;
double total = [Link] * days;
[Link]("Total Rent: " + total);
}
}

You might also like