0% found this document useful (0 votes)
61 views42 pages

Lab Manual CSE2006

Uploaded by

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

Lab Manual CSE2006

Uploaded by

raunakrajput2411
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

School of Computer Science and Engineering

(SCOPE)

Programming in Java
Lab Manual (CSE2006)

Student Name : Yash Priyam

Register Number : 24BCE10474.

Slot :A24+F22

1
TABLE OF CONTENTS

S.No Topic Page No.

1 Bonafide Certificate 3

2 Experiment Evaluation Summary 4

3 Write a program to implement constructor in java. 6

4 Write a program to implement Class concept in java. 8

5 Write a Program to implement method overloading by using 10


static method in java.

6 Write a Program to implement method overloading in single 12


class in java

7 Write a Program to implement simple inheritance in java. 14

8 Write a Program to implement method overriding in java. 17

9 Write a Program to call base class constructor using super 19


keyword in java.

10 Write a Program to call base class method using super 22


keyword in java

11 Write a Program to implement run time polymorphism by 25


applying dynamic dispatch method in method overriding.

12 Write a java Program to using Array. 27

13 Write a java Program to implement Abstract class. 29

14 Write a java Program to implement Interface. 31

15 Write a java Program to implement multithreading by 33


extending Thread class

16 Write a Java program to fetch data from database using 36


(JDBC-ODBC bridge).

17 Write a Java program to calculate factorial of any number 39


entered by user using looping statements

18 Write a java program using I/O streams 41

2
SCHOOL OF COMPUTING SCIENCE AND ENGINEERING

BONAFIDE CERTIFICATE

Bonafide record of work done by Yash Priyam

of B.Tech (CSE) in VIT Bhopal

during winter semester in academic year 2025-26

Faculty-in-charge Program Chair

Submitted to the practical Examination held at VIT Bhopal University, Kothrikalan on

____________

REGISTER NUMBER

2 4 B C E 1 0 4 7 4

Internal Examiner External Examiner

3
EXPERIMENT EVALUATION SUMMARY

Name: Yash Priyam Reg No: 24BCE10474

Marks Faculty
S. No Date Experiment
(50) Signature

1 19/09/2025 Write a program to implement constructor in java.

2 19/09/2025 Write a program to implement Class concept in


java.

3 19/09/2025 Write a Program to implement method overloading


by using static method in java.

4 19/09/2025 Write a Program to implement method overloading


in single class in java

5 20/09/2025 Write a Program to implement simple inheritance


in java.

6 20/09/2025 Write a Program to implement method overriding


in java.

7 20/09/2025 Write a Program to call base class constructor using


super keyword in java.

8 20/09/2025 Write a Program to call base class method using


super keyword in java

9 20/09/2025 Write a Program to implement run time


polymorphism by applying dynamic dispatch
method in method overriding.

10 21/09/2025 Write a java Program to using Array.

11 21/09/2025 Write a java Program to implement Abstract class.

12 21/09/2025 Write a java Program to implement Interface.

4
13 21/09/2025 Write a java Program to implement multithreading
by extending Thread class

14 22/09/2025 Write a Java program to fetch data from database


using (JDBC-ODBC bridge).

15 22/09/2025 Write a Java program to calculate factorial of any


number entered by user using looping statements

16 23/09/2025 Write a java program using I/O streams

5
EX. NO. 1 Program to implement constructor in java.

Aim:
To study and implement the concept of constructors in Java by creating objects and initializing
values automatically without explicitly calling methods.

Pseudocodes:
Start
Define class Student
Declare variables name and age
Define constructor with parameters name and age
Initialize variables using constructor
Define method display to print name and age
In main method:
Create object of Student class by passing values
Call display method
Stop

Coding:

class Student {

String name;

int age;

// Constructor to initialize name and age

Student(String n, int a) {

name = n;

age = a;

6
}

// Method to display student details

void display() {

System.out.println("Name: " + name);

System.out.println("Age: " + age);

public static void main(String[ args) {

// Creating object and passing values to constructor

Student s1 = new Student("John", 20);

// Displaying student details

s1.display();

Sample Input and Output:

7
EX. NO. 2 Program to implement Class concept in java.

Aim:
To write a Java program that demonstrates the concept of a class by defining attributes and
methods, and then creating objects to access them.

Pseudocodes:
Start
Define class Car
Declare variables brand and price
Define method setData to assign values to brand and price
Define method display to print brand and price
In main method:
Create object of Car class
Call setData method with values
Call display method
Stop

Coding:

class Car {

String brand;

int price;

// Method to set values

void setData(String b, int p) {

brand = b;

price = p;

8
}

// Method to display values

void display() {

System.out.println("Brand: " + brand);

System.out.println("Price: " + price);

public static void main(String[ args) {

// Creating object of Car

Car c1 = new Car();

// Setting values

c1.setData("Toyota", 2000000);

// Displaying values

c1.display();

Sample Input and Output:

9
EX. NO. 3 Program to implement method overloading by using static method in java.

Aim:
To write a Java program that demonstrates method overloading using static methods,
showing how multiple methods with the same name can accept different parameters.

Pseudocodes:

Start

Define class MathOperation

Declare static method add with two integer parameters

Declare static method add with three integer parameters

Declare static method add with two double parameters

In main method:

Call add method with two integers

Call add method with three integers

Call add method with two doubles

Display the results

Stop

Coding:

class MathOperation {

// Static method with two integers

static int add(int a, int b) {

return a + b;

10
// Static method with three integers

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

return a + b + c;

// Static method with two doubles

static double add(double a, double b) {

return a + b;

public static void main(String[ args) {

// Calling overloaded static methods

System.out.println("Sum of two integers: " + add(10, 20));

System.out.println("Sum of three integers: " + add(5, 15, 25));

System.out.println("Sum of two doubles: " + add(12.5, 7.5));

Sample Input and Output:

11
EX. NO. 4 Program to implement method overloading in single class in java.

Aim:

To write a Java program that demonstrates method overloading in a single class, where multiple
methods share the same name but differ in parameter lists.

Pseudocodes:

Start

Define class Calculator

Declare method multiply with two integer parameters

Declare method multiply with three integer parameters

Declare method multiply with two double parameters

In main method:

Create object of Calculator

Call multiply with two integers

Call multiply with three integers

Call multiply with two doubles

Display the results

Stop

Coding:

class Calculator {

// Method with two integers

int multiply(int a, int b) {

return a * b; }

12
// Method with three integers

int multiply(int a, int b, int c) {

return a * b * c;

// Method with two doubles

double multiply(double a, double b) {

return a * b;

public static void main(String[ args) {

// Creating object of Calculator

Calculator calc = new Calculator();

// Calling overloaded methods

System.out.println("Product of two integers: " + calc.multiply(5, 4));

System.out.println("Product of three integers: " + calc.multiply(2, 3, 4));

System.out.println("Product of two doubles: " + calc.multiply(2.5, 4.0));

Sample Input and Output:

13
EX. NO. 5 Program to implement simple inheritance in java.

Aim:

To write a Java program that demonstrates the concept of simple inheritance, where a child
class inherits properties and methods from a parent class.

Pseudocode:

Start

Define parent class Person

Declare variables name and age

Define method setData to assign values

Define method display to print values

Define child class Student that extends Person

Declare variable studentId

Define method setStudentData to assign values

Define method showStudent to print all values

In main method:

Create object of Student

Call setData and setStudentData

Call showStudent

Stop

Coding:

// Parent class

class Person {

String name;

int age;

14
// Method to set person data

void setData(String n, int a) {

name = n;

age = a;

// Method to display person data

void display() {

System.out.println("Name: " + name);

System.out.println("Age: " + age);

// Child class inheriting Person

class Student extends Person {

int studentId;

// Method to set student data

void setStudentData(String n, int a, int id) {

setData(n, a); // calling parent method

studentId = id;

// Method to display student data

void showStudent() {

15
display(); // calling parent method

System.out.println("Student ID: " + studentId);

public static void main(String[ args) {

// Creating object of Student

Student s1 = new Student();

// Setting values

s1.setStudentData("Alice", 21, 102);

// Displaying student details

s1.showStudent();

Sample Input and Output:

16
EX. NO. 6 Program to implement method overriding in java.

Aim:

To write a Java program that demonstrates method overriding, where a child class provides
a specific implementation of a method already defined in the parent class.

Pseudocode:

Start

Define parent class Animal

Declare method sound to print generic animal sound

Define child class Dog that extends Animal

Override method sound to print dog-specific sound

In main method:

Create object of Animal and call sound

Create object of Dog and call sound

Stop

Coding:

// Parent class

class Animal {

// Method to be overridden

void sound() {

System.out.println("Animal makes sound: Generic Sound");

17
// Child class

class Dog extends Animal {

// Overriding method

@Override

void sound() {

System.out.println("Dog makes sound: Bark");

public static void main(String[ args) {

// Creating parent class object

Animal a = new Animal();

a.sound();

// Creating child class object

Dog d = new Dog();

d.sound();

Sample Input and Output:

18
EX. NO. 7 Call base class constructor using super keyword in java.

Aim:

To write a Java program that demonstrates calling a base class constructor from a derived
class using the super keyword to initialize inherited properties.

Pseudocode:

Start

Define base class Person

Declare variables name and age

Define constructor to initialize name and age

Define method display to print name and age

Define derived class Student that extends Person

Declare variable studentId

Define constructor that uses super to call base class constructor and initialize studentId

Define method showStudent to display all details

In main method:

Create object of Student

Call showStudent

Stop

Coding:

// Base class

class Person {

String name;

int age;

19
// Constructor

Person(String n, int a) {

name = n;

age = a;

// Method to display person details

void display() {

System.out.println("Name: " + name);

System.out.println("Age: " + age);

// Derived class

class Student extends Person {

int studentId;

// Constructor

Student(String n, int a, int id) {

super(n, a); // Calling base class constructor

studentId = id;

// Method to display student details

void showStudent() {

display(); // Call base class method

20
System.out.println("Student ID: " + studentId);

public static void main(String[ args) {

// Creating object of Student

Student s1 = new Student("Alice", 21, 102);

// Displaying details

s1.showStudent();

Sample Input and Output:

21
EX. NO. 8 cCall base class method using super keyword in java.

Aim:

To write a Java program that demonstrates calling a base class method from a derived class
using the super keyword to access overridden methods.

Pseudocode:

Start

Define base class Person

Declare variables name and age

Define method display to print name and age

Define derived class Student that extends Person

Declare variable studentId

Override display method to include studentId and call super.display

In main method:

Create object of Student

Call display method

Stop

Coding:

// Base class

class Person {

String name;

int age;

// Method to display person details

void display() {

22
System.out.println("Name: " + name);

System.out.println("Age: " + age);

// Constructor to initialize values

Person(String n, int a) {

name = n;

age = a;

// Derived class

class Student extends Person {

int studentId;

// Constructor

Student(String n, int a, int id) {

super(n, a); // Initialize base class variables

studentId = id;

// Overriding display method

@Override

void display() {

super.display(); // Call base class display method

System.out.println("Student ID: " + studentId);

23
}

public static void main(String[ args) {

// Creating object of Student

Student s1 = new Student("Alice", 21, 102);

// Displaying details

s1.display();

Sample Input and Output:

24
EX. NO. 9 Run Time Polymorphism Using Dynamic Dispatch

Aim:

To write a Java program that demonstrates run-time polymorphism using dynamic method
dispatch, where a parent class reference calls an overridden method of a child class.

Pseudocodes:

Start

Define parent class Animal

Declare method sound to print generic animal sound

Define child class Cat that extends Animal

Override method sound to print cat-specific sound

In main method:

Create reference of Animal and assign it to new Cat object

Call sound method using parent reference

Stop

Coding:

// Parent class

class Animal {

// Method to be overridden

void sound() {

System.out.println("Animal makes sound: Generic Sound");

// Child class

class Cat extends Animal {

25
// Overriding method

@Override

void sound() {

System.out.println("Cat makes sound: Meow");

public static void main(String[ args) {

// Reference of parent class referring to child class object

Animal a = new Cat();

// Calling overridden method (dynamic dispatch)

a.sound();

Sample Input and Output:

26
EX. NO. 10 Java Program Using Array

Aim:

To write a Java program that demonstrates the use of arrays by storing multiple values in a
single variable and performing operations on them.

Pseudocodes:

Start

Define class ArrayExample

In main method:

Declare integer array of size 5

Initialize array with values

Loop through array to display all values

Stop

Coding:

class ArrayExample {

public static void main(String[ args) {

// Declare and initialize array

int[ numbers = {10, 20, 30, 40, 50};

// Display array elements

System.out.println("Array elements are:");

for(int i = 0; i < numbers.length; i++) {

System.out.println(numbers[i );

27
}

Sample Input and Output:

28
EX. NO. 11 Java Program to Implement Abstract Class

Aim:

To write a Java program that demonstrates the use of an abstract class by declaring abstract
methods and providing their implementation in the derived class.

Pseudocodes:

Start

Define abstract class Shape

Declare abstract method area

Define class Circle that extends Shape

Declare variable radius

Implement area method to calculate circle area

In main method:

Create object of Circle class

Call area method

Stop

Coding:

// Abstract class

abstract class Shape {

// Abstract method

abstract void area();

// Derived class

class Circle extends Shape {

double radius;

29
// Constructor to initialize radius

Circle(double r) {

radius = r;

// Implementing abstract method

@Override

void area() {

double result = 3.14 * radius * radius;

System.out.println("Area of circle: " + result);

public static void main(String[ args) {

// Creating object of Circle

Circle c = new Circle(5);

// Calling area method

c.area();

Sample Input and Output:

30
EX. NO. 12 Java Program to Implement Interface

Aim:

To write a Java program that demonstrates the use of an interface by declaring abstract
methods in the interface and implementing them in a class.

Pseudocodes:

Start

Define interface Vehicle

Declare method run

Define class Bike that implements Vehicle

Implement run method to display message

In main method:

Create object of Bike class

Call run method

Stop

Coding:

// Interface

interface Vehicle {

void run();

// Class implementing interface

class Bike implements Vehicle {

// Implementing run method

@Override

public void run() {

31
System.out.println("Bike is running");

public static void main(String[ args) {

// Creating object of Bike

Bike b = new Bike();

// Calling run method

b.run();

Sample Input and Output:

32
EX. NO. 13 Java Program to Implement Multithreading by Extending Thread Class

Aim:

To write a Java program that demonstrates multithreading by creating multiple threads


using the Thread class and executing them concurrently.

Pseudocodes:

Start

Define class MyThread that extends Thread

Override run method to display thread message

In main method:

Create multiple objects of MyThread

Start each thread using start method

Stop

Coding:

// Class extending Thread

class MyThread extends Thread {

String threadName;

// Constructor to set thread name

MyThread(String name) {

threadName = name;

// Overriding run method

@Override

33
public void run() {

for(int i = 1; i <= 5; i++) {

System.out.println(threadName + " is running: " + i);

public static void main(String[ args) {

// Creating thread objects

MyThread t1 = new MyThread("Thread 1");

MyThread t2 = new MyThread("Thread 2");

// Starting threads

t1.start();

t2.start();

Sample Input and Output:

34
35
EX. NO. 14 Java Program to Fetch Data from Database Using JDBC-ODBC Bridge

Aim:

To write a Java program that demonstrates fetching data from a database using the JDBC-
ODBC bridge by establishing a connection, executing a query, and displaying results.

Pseudocodes:

Start

Load JDBC-ODBC driver

Establish connection with database

Create Statement object

Execute SQL query to fetch data

Process ResultSet to display data

Close ResultSet, Statement, and Connection

Stop

Coding:

import java.sql.*;

class FetchDataJDBC {

public static void main(String[ args) {

Connection con = null;

Statement stmt = null;

ResultSet rs = null;

try {

// Load JDBC-ODBC driver

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

36
// Establish connection (DSN = MyDatabase)

con = DriverManager.getConnection("jdbc:odbc:MyDatabase");

// Create Statement

stmt = con.createStatement();

// Execute query

rs = stmt.executeQuery("SELECT * FROM Student");

// Display data

System.out.println("Roll No\tName\tAge");

while(rs.next()) {

System.out.println(rs.getInt("rollno") + "\t" +

rs.getString("name") + "\t" +

rs.getInt("age"));

} catch(Exception e) {

System.out.println("Error: " + e);

} finally {

try { if(rs != null) rs.close(); } catch(Exception e) {}

try { if(stmt != null) stmt.close(); } catch(Exception e) {}

try { if(con != null) con.close(); } catch(Exception e) {}

37
Sample Input and Output:

38
EX. NO. 15 Java Program to Calculate Factorial Using Loop

Aim:

To write a Java program that calculates the factorial of a number entered by the user using
looping statements such as for or while.

Pseudocodes:

Start

Import Scanner class

In main method:

Create Scanner object

Prompt user to enter a number

Read the number

Initialize factorial as 1

Loop from 1 to number:

Multiply factorial by loop counter

Display factorial

Stop

Coding:

import java.util.Scanner;

class Factorial {

public static void main(String[ args) {

Scanner sc = new Scanner(System.in);

// Prompt user to enter number

System.out.print("Enter a number: ");

39
int num = sc.nextInt();

int factorial = 1;

// Loop to calculate factorial

for(int i = 1; i <= num; i++) {

factorial *= i;

// Display factorial

System.out.println("Factorial of " + num + " is: " + factorial);

Sample Input and Output:

40
EX. NO. 10 Java Program Using I/O Streams

Aim:

To write a Java program that demonstrates the use of input and output streams to read data
from the user and write it to a file.

Pseudocodes:

Start

Import required classes from java.io package

In main method:

Create FileOutputStream object for output file

Create FileInputStream object for input file

Read data from input stream

Write data to output file

Close input and output streams

Stop

Coding:

import java.io. *;

class IOStreamsExample {

public static void main(String[ args) {

try {

// Create output stream to write to file

FileOutputStream fos = new FileOutputStream("output.txt");

String data = "Hello, this is a sample I/O stream example.";

41
// Convert string to bytes and write to file

fos.write(data.getBytes());

// Close output stream

fos.close();

// Create input stream to read from file

FileInputStream fis = new FileInputStream("output.txt");

int i;

System.out.println("Data read from file:");

while((i = fis.read()) != -1) {

System.out.print((char)i);

// Close input stream

fis.close();

} catch(Exception e) {

System.out.println("Error: " + e);

Sample Input and Output:

42

You might also like