Comilla University
Department of Computer Science and Engineering
OOP LAB Report
Course Code: CSE-2104
Course Title: Object Oriented Programming Language LAB
Submitted To:
Mahmuda Khatun
Assistant Professor
Department of CSE
Comilla University
Submitted By:
Md. Rafiqul Islam
ID: 12208012
Session: 2021-2022
Date of Submission: 12.11.2024
0
LIST OF THE EXPERIMENTS
S.NO NAME OF THE EXPERIMENT PAGE NO.
01 Classes & Objects 01
02 Constructors 02
03 Methods Overloading 03
04 Methods Overriding 03
05 Inheritance 04
06 Interface 05
07 Abstract Class 06
08 Multithreading 06
09 Package 07
10 Inheritance implementation using AWT 08
11 Association implementation using AWT 09
12 Inheritance implementation using Swing 10
EXPERIMENT No: 01
EXPERIMENT NAME: Write a Program in Java to implement the Classes and Objects.
EXPLANATION:
A class is a blueprint for creating objects; it defines attributes and methods that the object will
have. An object is an instance of a class that contains actual values for these attributes and can
perform actions defined by its class methods.
ALGORITHM:
STEP 1: Start the Program
STEP 2: Create Class
STEP 3: Declare the Input and Output Variables
STEP 3: Create object and access the method
STEP 4: Implement it with return type and without parameter list
STEP 5: Implement it with return type and with parameter list
STEP 6: Implement the constructor by creating classes and objects
SOURCE CODE:
class Animal {
String name;
int age;
1
void display() {
System.out.println("Name: " + name + ", Age: " + age);
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Animal();
dog.name = "Buddy";
dog.age = 5;
dog.display();
}
}
OUTPUT:
Name: Buddy, Age: 5
EXPERIMENT No: 02
EXPERIMENT NAME: Write a program in java with Constructors.
EXPLANATION:
A constructor is a special method used to initialize new objects. It has the same name as the
class and is automatically called when an object is created. Constructors can be overloaded to
allow different ways of initializing an object.
ALGORITHM:
STEP 1: Start the Program
STEP 2: Declare and initialize the input variables
STEP 3: Create the Constructors
STEP 4: Create various methods for Subclass
STEP 5: In derived class extend the previous class
STEP 6: In main class specify the values and create the object
STEP 7: Stop
SOURCE CODE:
class Car {
String model;
int year;
Car(String model, int year) {
this.model = model;
this.year = year;
}
void display() {
System.out.println("Model: " + model);
System.out.println("Year: " + year);
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car("Toyota", 2020);
car.display();
}
2
}
OUTPUT:
Model: Toyota
Year: 2020
EXPERIMENT No: 03
EXPERIMENT NAME: Write a program in java to implement method overloading.
EXPLANATION:
Overloading occurs when multiple methods with the same name but different parameter lists
are defined within the same class. It allows a class to perform similar operations in slightly
different ways depending on the parameter list.
ALGORITHM:
STEP 1: Start the Program
STEP 2: Initialize the File Pointer
STEP 3: Create the class Sum
STEP 4: Overload Sum with two parameters
STEP 5: Create int sum
STEP 6: Create another overloaded sum with two double parameters
STEP 7: In main function create the object and call the methods
STEP 8: Print the data in the file
SOURCE CODE:
public class Sum {
public int sum(int x, int y){
return (x + y);}
public int sum(int x, int y, int z){
return (x + y + z);}
public double sum(double x, double y){
return (x + y);}
public static void main(String args[]){
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));}}
OUTPUT:
30
60
31.0
EXPERIMENT No: 04
EXPERIMENT NAME: Write a program in java to implement method overriding.
EXPLANATION:
Overriding occurs when a subclass provides a specific implementation of a method that is
already defined in its superclass. This is used for polymorphism, allowing subclasses to define
behaviour specific to their types.
3
ALGORITHM:
STEP 1: Start the Program
STEP 2: Create a Base (Parent) Class
STEP 3: Create a Derived (Child) Class
STEP 4: Use @Override Annotation (Optional but Recommended)
STEP 5: Create a Main Class with main Method
STEP 6: Call Methods
STEP 7: Print the Data in the File
SOURCE CODE:
class MathOperations {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}}
public class Main {
public static void main(String[] args) {
MathOperations operations = new MathOperations();
System.out.println("Sum (2 parameters): " + operations.add(5, 3));
System.out.println("Sum (3 parameters): " + operations.add(5, 3, 2));
}}
OUTPUT:
Sum (2 parameters): 8
Sum (3 parameters): 10
EXPERIMENT No: 05
EXPERIMENT NAME: Write a program in Java to implement Inheritance.
EXPLANATION:
Inheritance is a mechanism where one class (subclass) inherits the fields and methods of
another class (superclass). This promotes code reuse and establishes a parent-child relationship
between classes, enabling polymorphism.
ALGORITHM:
STEP 1: Start the Program
STEP 2: Declare and initialize the input variables
STEP 3: Create the class Bicycle
STEP 4: Create the constructor for Bicycle class.
STEP 5: Create various methods for Subclass
STEP 6: In derived class extend the previous class
STEP 7: In main class specify the values and create the object
STEP 8: Print the Data in the File
SOURCE CODE:
class Vehicle {
String brand = "Ford";
4
}
class Car extends Vehicle {
String model = "Mustang";
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
System.out.println("Brand: " + car.brand);
System.out.println("Model: " + car.model);
}
}
Output:
Brand: Ford
Model: Mustang
EXPERIMENT No: 06
EXPERIMENT NAME: Write a program in Java to implement Interface.
EXPLANATION:
An interface is a reference type, similar to a class, that can contain only abstract methods (until
Java 8, which also allows default methods). A class can implement multiple interfaces,
allowing Java to support a form of multiple inheritance.
ALGORITHM:
STEP 1: Start the Program
STEP 2: Import the GUI packages
STEP 3: Create new frame and set sizes
STEP 4: In showeventdemo add button and listeners
STEP 5: In buttonclicklistener check whether the button is clicked
STEP 6: Print the Data in the File
SOURCE CODE:
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Dog barks");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
}
}
Output:
Dog barks
5
EXPERIMENT No: 07
EXPERIMENT NAME: Write a program in Java to implement Abstract Class.
EXPLANATION:
An abstract class cannot be instantiated on its own and may contain abstract methods without
implementation, which subclasses are expected to override. Abstract classes can also have
concrete methods and fields.
ALGORITHM:
STEP 1: Start the Program
STEP 2: Define an Abstract Class
STEP 3: Define Abstract Method(s)
STEP 4: Create Concrete Subclasses
STEP 5: Create Main Class and Test Implementation
STEP 6: Print the Data in the File
SOURCE CODE:
abstract class Shape {
abstract void draw();
}
class Circle extends Shape {
void draw() {
System.out.println("Drawing a Circle");
}
}
public class Main {
public static void main(String[] args) {
Shape shape = new Circle();
shape.draw();
}
}
OUTPUT:
Drawing a Circle
EXPERIMENT No: 08
EXPERIMENT NAME: Write a Program in Java to implement Multithreading.
EXPLANATION:
Multithreading enables a program to perform multiple tasks concurrently by creating multiple
threads of execution. Java provides a robust multithreading model with classes like Thread and
Runnable to achieve parallel processing and enhance performance.
ALGORITHM:
STEP 1: Start the Program
STEP 2: Declare and Initialize the Variables
STEP 3: Create the class Multi-Threading Demo
STEP 4: Declare the method run
STEP 5: Create the class Multithreads
6
STEP 6: Specify number of Threads
STEP 7: In main method create the object and start
STEP 8: Stop
SOURCE CODE:
class MultithreadingDemo extends Thread {
public void run(){
try {
System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");}
catch (Exception e) {
System.out.println("Exception is caught");}}}
public class Multithread {
public static void main(String[] args){
int n = 8;
for (int i = 0; i < n; i++) {
MultithreadingDemo object
= new MultithreadingDemo();
object.start();}}}
OUTPUT:
Thread 15 is running
Thread 14 is running
Thread 16 is running
Thread 12 is running
Thread 11 is running
Thread 13 is running
Thread 18 is running
Thread 17 is running
EXPERIMENT No: 09
EXPERIMENT NAME: Write a program in Java to implement Packages.
EXPLANATION:
A package is a namespace for organizing classes and interfaces in a logical manner. It helps
prevent name conflicts, control access, and improve maintainability. Packages are essentially
directories containing Java classes.
ALGORITHM:
STEP 1. START
STEP 2. Import package
STEP 3. Create Class A
STEP 4. Create Class B
STEP 5. Print the Data in the File
SOURCE CODE:
package pack;
public class A {
7
public void msg() {
System.out.println("Hello");}}
package mypack;
class B {
public static void main(String args[]) {
pack.A obj = new pack.A();
obj.msg();}}
Output:
Hello
EXPERIMENT No: 10
EXPERIMENT NAME: Write a Program in Java to implement inheritance using awt.
EXPLANATION:
AWT (Abstract Window Toolkit) is Java’s original platform-independent windowing,
graphics, and user-interface widget toolkit. Inheritance in AWT might involve extending AWT
classes like Frame, Button, etc., to customize their behaviour and design GUI applications.
ALGORITHM:
STEP 1: Start the Program
STEP 2: Import Required AWT Packages
STEP 3: Create the Parent Class
STEP 4: Create the Child Class that Inherits Parent Class
STEP 5: Set Up Event Handling (Optional)
STEP 6: Create Main Method to Instantiate the Child Class
STEP 7: Run and Display the Output
SOURCE CODE:
import java.awt.Button;
import java.awt.Frame;
public class AWTExample1 extends Frame {
AWTExample1() {
Button b = new Button("Click Me!!");
b.setBounds(30,100,80,30);
add(b);
setSize(300,300);
setTitle("This is our basic AWT example");
setLayout(null);
setVisible(true); }
public static void main(String args[]) {
AWTExample1 f = new AWTExample1(); } }
OUTPUT:
8
EXPERIMENT No: 11
EXPERIMENT NAME: Write a Program in Java to implement association using awt.
EXPLANATION:
Association represents a relationship between two classes where they can communicate with
each other. In AWT, association can be implemented by creating a class that contains or refers
to instances of other classes (e.g., event handling in a Frame that has Button components
associated with it).
ALGORITHM:
STEP 1: Start the Program
STEP 2: Import Required AWT Packages
STEP 3: Define the Associated Classes
STEP 4: Create the Main Application Class with AWT Components
STEP 5: Set Up Event Handling (Optional)
STEP 6: Run and Display the Output
SOURCE CODE:
import java.awt.Button;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextField;
class AWTExample2 {
AWTExample2() {
Frame f = new Frame();
Label l = new Label("Employee id:");
Button b = new Button("Submit");
TextField t = new TextField();
l.setBounds(20, 80, 80, 30);
t.setBounds(20, 100, 80, 30);
b.setBounds(100, 100, 80, 30);
f.add(b);
f.add(l);
f.add(t);
f.setSize(400,300);
f.setTitle("Employee info");
f.setLayout(null);
f.setVisible(true); }
public static void main(String args[]) {
AWTExample2 awt_obj = new AWTExample2(); } }
OUTPUT:
9
EXPERIMENT No: 12
EXPERIMENT NAME: Write a Program in Java to implement inheritance using swing.
EXPLANATION:
Swing is a part of Java Foundation Classes (JFC) and is used for creating advanced GUIs.
Inheritance in Swing often involves extending classes like JFrame, JButton, etc., to create a
customized and more interactive GUI than possible with AWT.
ALGORITHM:
STEP 1: Start the Program
STEP 2: Create the Base Class (Super Class)
STEP 3: Create the Derived Class (Subclass)
STEP 4: Set Up GUI Components in Both Classes
STEP 5: Define the main Method to Launch the GUI
STEP 6: Run and Display the Output
SOURCE CODE:
import javax.swing.*;
public class SwingInheritance extends JFrame {
JFrame f;
SwingInheritance() {
JButton b = new JButton("click");
b.setBounds(130, 100, 100, 40);
add(b);
setSize(400, 500);
setLayout(null);
setVisible(true);}
public static void main(String[] args) {
new SwingInheritance();}}
OUTPUT:
10