OBJECT ORIENTED PROGRAMMING WITH JAVA LAB
(BCS452)
Submitted By : Deepti Kulshrestha
University Roll No : 2202300100058
Branch : CSE
Section A
Submitted To : Vivek Krishna Misra
AP,CSE,DGI, GREATER NOIDA
Dronacharya Group Of Institutions, Greater Noida
AMANRAJ(2202300100019)
INDEX
[Link] DESCRIPTION DATE GRADE SIGNATURE
1. WAP in java to print hello world 10/06/24
2. WAP in java to find sumo of two no. Using 10/06/24
Command line arguments
3. WAP in java to implement Constructor 10/06/24
Overloading
4. WAP in java to implement Method 10/06/24
Overloading
5. WAP in java to implement Method 10/06/24
Overriding
6. WAP in java to implement Static 12/06/24
method, static class and static Variables
7. WAP in java to implement static and 12/06/24
instance block
8. WAP in Java to implement Single 12/06/24
Inheritance
9. WAP in Java to implement Multilevel 12/06/24
Inheritance
10. WAP in Java to implement Hierarchical 12/06/24
Inheritance
11. WAP in java to show the use of Abstract 12/06/24
Class
12. WAP in java to show the use of interface 12/06/24
13. WAP in java to show the use of try catch 12/06/24
and finally
14. WAP in java to show the use of try with 12/06/24
multiple catch
15. WAP in java to show the use of nested 12/06/24
try and catch
16. WAP in java to implement custom 12/06/24
exception (AgeException) using throw
and throws
17. WAP in java Create multithreading using 12/06/24
Thread Class and show the use
of important Methods of Thread class.
18. WAP in java to create thread using 12/06/24
Runnable interface and show the use
of printStacktrace(), toString(), and
getMessage() methods
19. Write a program in java to create a 12/06/24
String ans show the use of equals()
and ==
AMANRAJ(2202300100019)
[Link] DESCRIPTION DATE GRADE SIGNATURE
20. Write a program in java to remove all 12/06/24
extra spaces from a paragraph
21. Write a program in java to show the use 12/06/24
of default and static methods
22. Write a program in java to show the use 12/06/24
of Lambda of single argument,
no argument and two arguments
23. Write a program in java to show the use 12/06/24
of method reference
24. Write a program in java to create a 12/06/24
Stream of Integers and find the sum
of all Even Integers
25. Write a program in java to create a 12/06/24
LinkedList and copy all the value to
ArrayList
26. Write a program in java to input a 12/06/24
paragraph and find the occurrence of
each unique words
27. Write a program in java to create a 12/06/24
Employee class and sort the object of
this class using Comparable
28. Write a program in java to create a 12/06/24
Employee class and sort the object of
this class using Comparator of many
field
29. Write a program in java to implement 12/06/24
Set interface
30. Write a program in java to implement 12/06/24
HashMap and TreeMap
Program 1
//WAP in java to print Hello World
Code
public class hello {
public static void main(String[] args) {
[Link]("Hello World");
}
}
Output
AMANRAJ(2202300100019)
Program 2
//WAP to find the sum of two numbers using
Command Line Arguments
Code
public class sum {
public static void main(String[] args) {
if ([Link] != 2) {
[Link]("Please enter two numbers as arguments.");
return;
}
try {
AMANRAJ(2202300100019)
int num1 = [Link](args[0]);
int num2 = [Link](args[1]);
int sum = num1 + num2;
[Link]("The sum of " + num1 + " and " + num2 + " is: " + sum);
} catch (NumberFormatException e) {
[Link]("Invalid arguments. Please enter two numbers.");
}
}
Output
Program 3
//WAP in java to implement Constructor Overloading
public class Box {
double width, height, depth;
int boxNo;
Box(double w, double h, double d, int num)
{
width = w;
height = h;
depth = d;
boxNo = num;
[Link]("this is a 4 parameter constructor");
}
Box()
{
AMANRAJ(2202300100019)
width = height = depth = 0;
[Link]("This is a non parameterized Constructor");
}
Box(int num)
{
this();
boxNo = num;
[Link]("This is a one parameter constructor");
}
public static void main(String[] args)
{
Box box1 = new Box(1);
Box box2 = new Box(2,4,5,6);
Box box3 = new Box();
}
}
Output
AMANRAJ(2202300100019)
Program 4
//WAP in java to implement Method Overloading
Code
public class mayt2 {
static void foo(){
[Link]("GOOD MORNING BRO");
}
static void foo(int a){
[Link]("GOOD MORNING " + a + " bro");
}
static void foo(int a, int b){
[Link]("GOOD MORNING " + a + " bro");
[Link]("GOOD MORNING " + b + " bro");
}
AMANRAJ(2202300100019)
public static void main(String[] args) {
foo();
foo(1000);
foo(100,29);
}
}
Output
Program 5
//WAP in java to implement Method Overriding
Code
class Vehicle{
void run(){[Link]("Vehicle is running");}
}
class Bike2 extends Vehicle{
void run(){[Link]("Bike is running safely");}
public static void main(String args[]){
Bike2 obj = new Bike2();//creating object
[Link]();//calling method
}
}
AMANRAJ(2202300100019)
Output
Program 6
//WAP in java to implement Static method,Static class
and Static Variables
Code
public class StaticExample {
public static int objectCount = 0;
public static void incrementObjectCount() {
objectCount++;
}
public static class Utilities {
public static int getObjectCount() {
return objectCount;
AMANRAJ(2202300100019)
}
}
public StaticExample() {
incrementObjectCount();
}
public static void main(String[] args) {
StaticExample obj1 = new StaticExample();
StaticExample obj2 = new StaticExample();
[Link]("Total objects created: " + [Link]);
[Link]("Object count using Utilities: " +
[Link]());
}
}
Output
Program 7
//WAP in java to implement static and instance block
Code
public class StaticExample {
static {
[Link]("This is a static block.");
}
private String message;
{
[Link]("This is an instance block. It runs whenever a new object is created.");
message = "Default message";
}
public StaticExample() {
AMANRAJ(2202300100019)
[Link]("This is the constructor.");
message = "Object created with message: " + message;
}
public String getMessage() {
return message;
}
public static void main(String[] args) {
StaticExample obj1 = new StaticExample();
[Link]([Link]());
StaticExample obj2 = new StaticExample();
[Link]([Link]());
}
}
Output
Program 8
//WAP in Java to implement Single Inheritance
Code
class Animal {
public void speak(){
[Link]("We are the group of animals");
}
}
class Dog extends Animal{
public void bark(){
[Link]("bhow bhow");
}
}
AMANRAJ(2202300100019)
public class inheritance {
public static void main(String[] args) {
Dog d = new Dog();
[Link]();
[Link]();
}
}
Output
Program 9
//WAP in Java to implement Multilevel Inheritance
Code
class Animal {
public void speak(){
[Link]("We are the group of animals");
}
}
class Dog extends Animal{
public void bark(){
[Link]("bhow bhow");
}
}
AMANRAJ(2202300100019)
class Cat extends Dog{
public void meow(){
[Link]("meow meow");
}
}
public class inheritance {
public static void main(String[] args) {
Cat c = new Cat();
[Link]();
[Link]();
[Link]();
}
}
Output
Program 10
//WAP in Java to implement Hierarchical Inheritance
Code
class Animal {
public void speak(){
[Link]("We are the group of animals");
}
}
class Dog extends Animal{
public void bark(){
[Link]("bhow bhow");
}
}
class Cat extends Animal{
AMANRAJ(2202300100019)
public void meow(){
[Link]("meow meow");
}
}
public class inheritance {
public static void main(String[] args) {
Cat c = new Cat();
[Link]();
[Link]();
Dog d = new Dog();
[Link]();
[Link]();
}
}
Output
Program 11
//WAP in java to show the use of Abstract Class
Code
abstract class Parent{
public Parent(){
[Link]("I am Base 2 constructor");
}
public void sayHello(){
[Link]("Hello");
}
abstract public void greet();
}
class Child extends Parent{
AMANRAJ(2202300100019)
public void greet(){
[Link]("Good Morning");
}
}
class GrandChild extends Parent{
public void greet(){
[Link]("Namaste");
}
}
public class Abstract {
public static void main(String[] args) {
Child c = new Child();
[Link]();
GrandChild gc = new GrandChild();
[Link]();
}
}
Output
Program 12
//WAP in java to show the use of Interface
Code
interface Bicycle{
void applyBrake(int decrement);
void speedUp(int increment);
}
class AvonCycle implements Bicycle{
void blowHorn(){
[Link]("Pee Pee Poo POO");
}
public void applyBrake(int decrement){
[Link]("applying brake");
AMANRAJ(2202300100019)
}
public void speedUp(int increment){
[Link]("Speeding up vehicle");
}
}
public class Interface {
public static void main(String[] args) {
AvonCycle av = new AvonCycle();
[Link](4);
[Link](4);
}
}
Output
Program 13
//WAP in java to show the use of try catch and finally
Code
public class exceptions {
public static void main(String[] args) {
int a = 6000;
int b = 0;
try {
int c = a/b;
[Link]("the result is " + c);
}
catch(Exception e){
AMANRAJ(2202300100019)
[Link]("Failed to divide. Reason: ");
[Link](e);
}
finally {
[Link]("End of program");
}
}
}
Output
Program 14
//WAP in java to show the use of try with multiple
catch
Code
public class TryCatchMultiple {
public static void main(String[] args) {
int num = 10;
String name = null;
try {
int result = num / 0;
[Link]("Name: " + [Link]());
} catch (ArithmeticException e) {
[Link]("Error: Division by zero!");
} catch (NullPointerException e) {
[Link]("Error: Attempting to use null object!");
AMANRAJ(2202300100019)
} finally {
[Link]("This block always executes.");
}
}
}
Output
Program 15
//WAP in java to show the use of nested try and catch
Code
public class NestedTryCatchEasy {
public static void main(String[] args) {
int number = 10;
try {
readBook("Fairytales");
try {
String[] stories = new String[3];
stories[4] = "The Ugly Duckling";
} catch (ArrayIndexOutOfBoundsException e) {
[Link]("Inner catch: Oops! That shelf is empty."); // Friendly message for
inner exception
}
AMANRAJ(2202300100019)
} catch (Exception e) {
[Link]("Outer catch: Uh oh, there might be a problem with the book."); //
Friendly message for outer exception
} finally {
[Link]("This always happens, even if there are problems.");
}
}
public static void readBook(String title) throws Exception {
throw new Exception("There might be missing pages in this book!");
}
}
Output
Program 16
//WAP in java to implement custom exception
(AgeException) using throw
and throws
Code
public class AgeException extends Exception {
public AgeException(String message) {
super(message);
}
}
class ValidateAge {
public static void main(String[] args) {
int age = 15;
AMANRAJ(2202300100019)
try {
ValidateAge(age);
[Link]("Age is valid.");
} catch (AgeException e) {
[Link]("Error: " + [Link]());
}
}
public static void validateAge(int age) throws AgeException {
if (age < 18) {
throw new AgeException("You must be at least 18 years old.");
}
}
}
Output
Program 17
//WAP in java Create multithreading using Thread
Class and show the use
of important Methods of Thread class.
(getName(),setName(),
getPriority(), setPriority(),getID(), join())
Code
public class ThreadMethodsExample extends Thread {
private String name;
public ThreadMethodsExample(String name) {
[Link] = name;
}
@Override
AMANRAJ(2202300100019)
public void run() {
[Link]("Thread running: " + name);
try {
[Link](1000);
} catch (InterruptedException e) {
[Link]("Thread " + name + " interrupted!");
}
}
public static void main(String[] args) {
Thread thread1 = new ThreadMethodsExample("Thread-1");
Thread thread2 = new ThreadMethodsExample("Thread-2");
Thread thread3 = new Thread(new ThreadMethodsExample("Thread-3"), "Custom-Thread-
Name");
[Link]("Thread 1 name: " + [Link]());
[Link]("Thread 1 name (after): " + [Link]());
[Link]("Thread 2 priority: " + [Link]());
[Link](Thread.NORM_PRIORITY + 1);
[Link]("Thread 2 priority (after): " + [Link]());
[Link]("Thread 3 ID: " + [Link]());
[Link]();
[Link]();
[Link]();
try {
[Link]();
[Link]();
[Link]();
} catch (InterruptedException e) {
[Link]("Main thread interrupted!");
}
[Link]("All threads finished.");
}
}
Output
AMANRAJ(2202300100019)
Program 18
//WAP in java to create thread using Runnable
interface and show the use
of printStacktrace(), toString(), and getMessage()
methods
Code
class MyThreadRunnable implements Runnable{
public void run(){
[Link]("i am not a thread");
[Link]("i am not a thread");
}
}
class MyThreadRunnable2 implements Runnable{
AMANRAJ(2202300100019)
public void run(){
[Link]("I am not a thread 2");
[Link]("I am not a thread 2");
}
}
public class threading_runnable {
public static void main(String[] args) {
MyThreadRunnable r1 = new MyThreadRunnable();
Thread r11 = new Thread(r1);
MyThreadRunnable2 r2 = new MyThreadRunnable2();
Thread r22 = new Thread(r2);
[Link]();
[Link]();
[Link]([Link]());
[Link]([Link]());
}
}
Output
AMANRAJ(2202300100019)
Program 19
//Write a program in java to create a String and show
the use of equals()
and ==
Code
public class Stringmeth {
public static void main(String[] args) {
String a = "Deepti";
String b = "deepti";
[Link]([Link](b));
[Link](a==b);
}
}
Output
AMANRAJ(2202300100019)
Program 20
//Write a program in java to remove all extra spaces
from a paragraph
Code
public class Stringmeth {
public static void main(String[] args) {
String nontrim = " Deepti is a student of Cse Department";
[Link](nontrim);
[Link]([Link]("\\s", ""));
}
}
Output
AMANRAJ(2202300100019)
Program 21
//Write a program in java to show the use of default
and static methods
Code
public interface Calculator {
static double add(double a, double b) {
return a + b;
}
default double subtract(double a, double b) {
return a - b;
}
AMANRAJ(2202300100019)
}
class MyMath implements Calculator {
public double getArea() {
throw new UnsupportedOperationException("Not implemented");
}
}
class Main {
public static void main(String[] args) {
[Link]("Sum: " + [Link](5, 3));
[Link]("Difference: " + new MyMath().subtract(10, 2));
}
}
Output
AMANRAJ(2202300100019)
Program 22
//Write a program in java to show the use of Lambda
of single argument,no argument and two arguments
Code Output
// No argument
interface Test1 {
void print();
}
class G {
static void fun(Test1 t) { [Link](); }
public static void main(String[] args)
{
fun(() -> [Link]("Hello"));
}
}
//Single argument
interface Test2 {
void print(Integer p);
}
class Run {
static void fun(Test2 t, Integer p) {
[Link](p);
}
public static void main(String[] args) {
fun(p -> [Link](p), 10);
}
}
//Two Arguments
interface Test3 {
void print(Integer p1, Integer p2);
}
class Hey {
static void fun(Test3 t, Integer p1, Integer p2)
{
[Link](p1, p2);
}
public static void main(String[] args)
{
AMANRAJ(2202300100019)
fun((p1, p2)
-> [Link](p1 + " " + p2),
10, 20);
}
}
AMANRAJ(2202300100019)
Program 23
//Write a program in java to show the use of method
reference
Code
public class StaticVariableExample {
public static final String GREETING = "Hello";
public static String getGreeting() {
return GREETING;
}
public static void main(String[] args) {
Greeter greeter = StaticVariableExample::getGreeting;
String message = [Link]();
[Link](message);
}
}
@FunctionalInterface
interface Greeter {
String greet();
}
Output
AMANRAJ(2202300100019)
Program 24
//Write a program in java to create a Stream of
Integers and find the sum of all Even Integers
Code
import [Link];
public class EvenIntegerSum {
public static void main(String[] args) {
int limit = 10;
int sumOfEvens = [Link](1, limit).filter(n -> n % 2 == 0).sum();
[Link]("Sum of even numbers from 1 to " + limit + ": " + sumOfEvens);
}
}
Output
AMANRAJ(2202300100019)
Program 25
//Write a program in java to create a LinkedList and
copy all the value to ArrayList
Code
import [Link];
import [Link];
import [Link];
public class LinkedListToArrayList {
public static void main(String[] args) {
LinkedList<String> names = new LinkedList<>();
[Link]("Alice");
[Link]("Bob");
[Link]("Charlie");
ArrayList<String> copiedNames = new ArrayList<>();
[Link](copiedNames, names);
[Link]("Original LinkedList: " + names);
[Link]("Copied ArrayList: " + copiedNames);
}
}
Output
AMANRAJ(2202300100019)
Program 26
//Write a program in java to input a paragraph and
find the occurrence of each unique words
Code
import [Link];
import [Link];
import [Link];
public class UniqueWordCount {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Enter a paragraph: ");
String paragraph = [Link]().toLowerCase();
HashMap<String, Integer> wordCounts = new HashMap<>();
Pattern pattern = [Link]("\\W+");
for (String word : [Link](paragraph)) {
if ([Link]()) {
continue;
}
[Link](word, [Link](word, 0) + 1);
}
[Link]("Unique Words and Counts:");
for ([Link]<String, Integer> entry : [Link]()) {
[Link]([Link]() + ": " + [Link]());
}
[Link]();
}
AMANRAJ(2202300100019)
Program 27
//Write a program in java to create a Employee class
and sort the object of this class using Comparable
Code
import [Link];
import [Link];
public class EmployeeSort {
public static void main(String[] args) {
Employee emp1 = new Employee(101, "Alice", "Manager", 80000);
Employee emp2 = new Employee(202, "Bob", "Engineer", 65000);
Employee emp3 = new Employee(303, "Charlie", "Developer", 72000);
ArrayList<Employee> employees = new ArrayList<>();
[Link](emp1);
[Link](emp2);
[Link](emp3);
[Link]("Unsorted Employees:");
for (Employee emp : employees) {
[Link](emp);
}
[Link](employees);
[Link]("\nSorted Employees by ID:");
for (Employee emp : employees) {
[Link](emp);
}
}
}
class Employee implements Comparable<Employee> {
private int id;
private String name;
private String designation;
private double salary;
public Employee(int id, String name, String designation, double salary) {
[Link] = id;
[Link] = name;
AMANRAJ(2202300100019)
[Link] = designation;
[Link] = salary;
}
@Override
public int compareTo(Employee other) {
return [Link] - [Link]; // Sort by ID in ascending order
}
}
Output
AMANRAJ(2202300100019)
//Program 28
Write a program in java to create a Employee
class and sort the object of this class using
Comparator of many field
Code
import [Link];
import [Link];
import [Link];
public class EmployeeSortMultiField {
public static void main(String[] args) {
Employee emp1 = new Employee(101, "Alice", "Manager", 80000, "IT");
Employee emp2 = new Employee(202, "Bob", "Engineer", 65000, "Engineering");
Employee emp3 = new Employee(303, "Charlie", "Developer", 72000, "IT");
Employee emp4 = new Employee(201, "David", "Engineer", 68000, "Engineering");
ArrayList<Employee> employees = new ArrayList<>();
[Link](emp1);
[Link](emp2);
[Link](emp3);
[Link](emp4);
[Link]("Unsorted Employees:");
for (Employee emp : employees) {
[Link](emp);
}
Comparator<Employee> multiFieldComparator =
[Link](Employee::getName)
.thenComparing(Employee::getDepartment);
[Link](employees, multiFieldComparator);
[Link]("\nSorted Employees by Name and Department:");
for (Employee emp : employees) {
[Link](emp);
}
}
}
class Employee {
private int id;
private String name;
private String designation;
private double salary;
private String department;
AMANRAJ(2202300100019)
public Employee(int id, String name, String designation, double salary, String department) {
[Link] = id;
[Link] = name;
[Link] = designation;
[Link] = salary;
[Link] = department;
}
public String getName() {
return name;
}
public String getDepartment() {
return department;
}
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Designation: " + designation + ", Salary: $" +
salary + ", Department: " + department;
}
}
Output
AMANRAJ(2202300100019)
//Program 29
//Write a program in java to implement Set
interface
Code
import [Link];
import [Link];
public class SimpleSetExample {
public static void main(String[] args) {
Set<String> fruits = new HashSet<>();
[Link]("Apple");
[Link]("Banana");
[Link]("Orange");
[Link]("Apple");
[Link]("Fruits Set:");
for (String fruit : fruits) {
[Link](fruit);
}
if ([Link]("Mango")) {
[Link]("Mango is present in the set.");
} else {
[Link]("Mango is not present in the set.");
}
}
}
Output
AMANRAJ(2202300100019)
//Program 30
//Write a program in java to implement Set
interface
Code
import [Link];
import [Link];
import [Link];
import [Link];
public class HashMapTreeMapExample {
public static void main(String[] args) {
HashMap<String, Integer> studentIds = new HashMap<>();
[Link]("Alice", 12345);
[Link]("Bob", 54321);
[Link]("Charlie", 67890);
[Link]("HashMap (Student IDs):");
for ([Link]<String, Integer> entry : [Link]()) {
[Link]([Link]() + ": " + [Link]());
}
SortedMap<String, String> countriesCapitals = new TreeMap<>();
[Link]("India", "New Delhi");
[Link]("France", "Paris");
[Link]("USA", "Washington D.C.");
[Link]("\nTreeMap (Countries and Capitals):");
for ([Link]<String, String> entry : [Link]()) {
[Link]([Link]() + ": " + [Link]());
}
}
}
Output
AMANRAJ(2202300100019)