0% found this document useful (0 votes)
6 views33 pages

Object Oriented Programming Using Java Practical

The document is a practical file for the Object Oriented Programming using Java course at Faridabad College of Engineering & Management for the session 2025-2026. It includes a certificate of completion, acknowledgment, and a detailed list of programming experiments with source code and outputs. The experiments cover various Java programming concepts such as basic input/output, arithmetic operations, string manipulation, and class design.

Uploaded by

Ankit Verma
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)
6 views33 pages

Object Oriented Programming Using Java Practical

The document is a practical file for the Object Oriented Programming using Java course at Faridabad College of Engineering & Management for the session 2025-2026. It includes a certificate of completion, acknowledgment, and a detailed list of programming experiments with source code and outputs. The experiments cover various Java programming concepts such as basic input/output, arithmetic operations, string manipulation, and class design.

Uploaded by

Ankit Verma
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

FARIDABAD COLLEGE OF ENGINEERING & MANAGEMENT

(AICTE Approved & Affiliated to HSBTE)

DEPARTMENT OF COMPUTER ENGINEERING

OBJECT ORIENTED PROGRAMMING USING JAVA


PRACTICAL FILE
SUBJECT CODE:220844
SESSION: 2025 – 2026

SUBMITTED BY: SUBMITTED TO:


Name :
Roll No : (Subject Teacher)
Branch : Diploma in Computer Engineering
Semester :
CERTIFICATE
FARIDABAD COLLEGE OF ENGINEERING & MANAGEMENT

This is to certify that the Object Oriented Programming Using Java practical work
submitted by Mr./Ms. __________________________ Roll No. ____________of
Diploma in Computer Engineering, Semester _______, for the session 2025–2026
has been completed successfully under my supervision.

He/She has satisfactorily completed the required practicals as per guidelines of


Haryana State Board of Technical Education (HSBTE), Panchkula.

Date: Signature of Teacher


Place: Faridabad ______________________
(Name of Subject Teacher)
Computer Engineering Department
ACKNOWLEDGEMENT
I would like to express my sincere gratitude to the faculty of Faridabad College of
Engineering & Management for their constant support and encouragement.
I am especially thankful to our respected subject teacher
________________________ for providing valuable guidance, motivation and
continuous assistance throughout the preparation of this Object Oriented
Programming Using Java Practical File.
I am also thankful to my friends and classmates who helped directly or indirectly
in the completion of this work.

Date:
Place: Faridabad

____________________
(Student Name)
Sl. Page
CONTENT
No. No.
Write a Java program to print 'Hello' on screen and then print
01 1
your name on a separate line.
02 Write a Java program to print the sum of two numbers. 2

Write a Java program that takes a number as input and prints


03 3
its multiplication table upto 10.

04 Write a Java program to print the area and perimeter of a 4


rectangle.
05 Write a Java program to swap two variables. 5

Write a Java program to convert a decimal number to binary


06 6
number.
07 Write a Java program to compare two numbers. 7

Write a Java program and compute the sum of the digits of


08 8
an integer.
Write a Java program to count the letters, spaces, numbers
09 9-10
and other characters of an input string.
10 Write a Java program to reverse a string. 11

Write a Java program to accept a number and check the


11 number is even or not. Prints 1 if the number is even or 0 if 12
the number is odd.
Write a Java program that accepts two integer values from
the user and return the larger values. However if the two
12 13
values are the same, return 0 and return the smaller value if
the two values have the same remainder when divided by 6.

Write a Java program to get the larger value between first


13 14
and last element of an array (length 3) of integers.
Design a class to represent a bank account. Include the
following members:
Data members:
 Name of the depositor
 Account Number
 Type of account
14  Balance amount in the account 15-19
Methods:
 To assign initial values
 To deposit an amount
 To withdraw an amount
 To display the name and balance

Given are two one-dimensional arrays, A and B which are


sorted in ascending order. Write a program to merge them
15 20-21
into a single sorted array C that contains every item from
arrays A and B, in ascending order.
16 Write a java program implementing multiple inheritance. 22
17 Write a java program implementing package. 23

Write a java program to read a file line by line and print the
18 24
line on the output screen.
Write a java program to read content from one file and write
19 25
it into another file.

Define an exception called “No Match Exception” that is


20 thrown when a string is not equal to “India”. Write a 26
program that uses this exception.
Develop a java project for percentage calculator/temperature
21 27-28
conversion tool.
Experiment 1:-
Write a Java program to print 'Hello' on screen and then print your name on a
separate line.

Source Code:
public class Exercise1
{
public static void main(String[] args)
{
[Link]("Hello\nSaumya Ranjan Sahu!");
}
}

OUTPUT:-
Hello
Saumya Ranjan Sahu

1
Experiment 2:-
Write a Java program to print the sum of two numbers.

Source Code:
[Link];
public class Main {
public static void main(String[] args)
{
Scanner input = new Scanner ([Link]);
[Link]("Input the first number: ");
int num1 = [Link]();
[Link]("Input the second number:
"); int num2 = [Link]();
int sum = num1 + num2;
[Link]();
[Link]("Sum: "+sum);
}
}

OUTPUT:-
Input the first number: 256
Input the second number: 326
Sum: 582

2
Experiment 3:-
Write a Java program that takes a number as input and prints its multiplication
table upto 10.

Source Code:
[Link];
public class Exercise7 {
public static void main(String[] args)
{ Scanner in = new
Scanner([Link]);
[Link]("Input a number: ");
int num1 = [Link]();
for (inti=0; i< 10; i++){
[Link](num1 + " x " + (i+1) + " = "
+
(num1 * (i+1)));
}
}
}

OUTPUT:-
Input a number: 8
8x1=8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80

3
Experiment 4:-
Write a Java program to print the area and perimeter of a rectangle.

Source Code:
[Link];
public class Area_Perimeter
{
public static void main(String[] args)
{
int l, b, perimeter, area;
Scanner s = new Scanner([Link]);
[Link]("Enter length of rectangle:");
l = [Link]();
[Link]("Enter breadth of rectangle:");
b = [Link]();
perimeter = 2 * (l + b);
[Link]("Perimeter of rectangle:"+perimeter);
area = l * b;
[Link]("Area of rectangle:"+area);
}
}

OUTPUT:-
Enter length of rectangle:4
Enter breadth of rectangle:5
Perimeter of rectangle:18
Area of rectangle:20

4
Experiment 5:-
Write a Java program to swap two variables.

Source Code:
[Link];
public class Main {
public static void main(String[] args)
{ int x, y, z;
Scanner in = new Scanner([Link]);
[Link]("Input the first number: ");
x = [Link]();
[Link]("Input the second number: ");
y = [Link]();
z = x;
x =
y; y
= z;
[Link]("Swapped values are: " + x + " and " + y);
}
}

OUTPUT:-
Input the first number:
36
Input the second number:
44
Swapped values are:44 and 36

5
Experiment 6:-
Write a Java program to convert a decimal number to binary number.

Source Code:
[Link];
public class Exercise19 {
public static void main(String args[]) {

intdec_num, quot, i = 1, j;
intbin_num[] = new int[100];
Scanner scan = new Scanner([Link]);
[Link]("Input a Decimal Number: ");
dec_num = [Link]();
quot = dec_num;
while (quot != 0) {
bin_num[i++] = quot % 2;
quot = quot / 2;
}
[Link]("Binary number is: ");
for (j = i - 1; j > 0; j--) {
[Link](bin_num[j]);
}
[Link]("\n");
}
}

OUTPUT:-
Input a Decimal Number: 5
Binary number is: 101

6
Experiment 7:-
Write a Java program to compare two numbers.

Source Code:
[Link];

public class Exercise32 {


public static void main(String args[]) {
Scanner input = new Scanner([Link]);
int number1;
int number2;
[Link]("Input first integer: ");
number1 = [Link]();
[Link]("Input second integer: ");
number2 = [Link]();
if (number1 == number2)
[Link]("%d == %d\n", number1, number2);
if (number1 != number2)
[Link]("%d != %d\n", number1, number2);
if (number1 < number2)
[Link]("%d < %d\n", number1, number2);
if (number1 > number2)
[Link]("%d > %d\n", number1, number2);
if (number1 <= number2)
[Link]("%d <= %d\n", number1, number2);
if (number1 >= number2)
[Link]("%d >= %d\n", number1, number2);
}
}

OUTPUT:-
Input first integer: 25
Input second integer: 39
25 != 39
25 < 39
25 <= 39

7
Experiment 8:-
Write a Java program and compute the sum of the digits of an integer.

Source Code:
[Link];
public class Exercise6 {
public static void main(String[] args)
{
Scanner in = new Scanner([Link]);
[Link]("Input an integer: ");
int digits = [Link]();
[Link]("The sum is " + sumDigits(digits));
}
public static intsumDigits(long n) {
int result = 0;
while(n > 0) {
result += n % 10;
n /= 10;
}
return result;
}
}

OUTPUT:-
Input an integer: 25
The sum is 7

8
Experiment 9:-
Write a Java program to count the letters, spaces, numbers and other characters of
an input string.

Source Code:
[Link];
public class Exercise38 {
public static void main(String[] args) {
String test = "Aakiu, I swdskieo 236587. GH kiu: sieo?? 25.33";
count(test);
}
public static void count(String x)
{ char[] ch = [Link]();
int letter = 0;
int space = 0;
intnum = 0;
int other = 0;
for (inti = 0; i<[Link](); i++) {
if ([Link](ch[i])) {
letter++;
}
else
if ([Link](ch[i])) {
num++;
}
else
if ([Link](ch[i])) {
space++;
}
else
{
other++;
}
}
[Link]("The string is : Aa kiu, I swdskieo 236587. GH kiu: sieo?? 25.33");
[Link]("letter: " + letter);
[Link]("space: " + space);
[Link]("number: " + num);
[Link]("other: " + other);

9
}
}

OUTPUT:-
The string is : Aa kiu, I swdskieo 236587. GH kiu: sieo?? 25.33
letter: 23
space: 9
number: 10
other: 6

10
Experiment 10:-
Write a Java program to reverse a string.

Source Code:
[Link];
public class Exercise37 {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
[Link]("Input a string: ");
char[] letters = [Link]().toCharArray();
[Link]("Reverse string: ");
for (inti = [Link] - 1; i>= 0; i--) {
[Link](letters[i]);
}
[Link]("\n");
}
}

OUTPUT:-
Input a string: The quick brown fox
Reverse string: xofnworbkciuqehT

11
Experiment 11:-
Write a Java program to accept a number and check the number is even or not.
Prints 1 if the number is even or 0 if the number is odd.

Source Code:
[Link].*;

public class Exercise49 {


public static void main(String[] args)
{ Scanner in = new Scanner([Link]);
[Link]("Input a number: ");
int n = [Link]();

if (n % 2 == 0) {
[Link](1); // If the number is even, print 1
} else {
[Link](0); // If the number is odd, print 0
}
}
}

OUTPUT:-
Input a number: 20
1

12
Experiment 12:-
Write a Java program that accepts two integer values from the user and return the
larger values. However if the two values are the same, return 0 and return the
smaller value if the two values have the same remainder when divided by 6.

Source Code:
[Link].*;
public class Exercise63 {
public static void main(String[] args)
{
Scanner in = new Scanner([Link]);
[Link]("Input the first number : ");
int a = [Link]();
[Link]("Input the second number:
"); int b = [Link]();
[Link]("Result: "+result(a, b));
}
public static int result(int x, int y)
{
if(x == y)
return 0;
if(x % 6 == y % 6)
return (x < y) ? x : y;
return (x > y) ? x : y;
}
}

OUTPUT:-
Input the first number: 12
Input the second number: 13
Result: 13

13
Experiment13:-
Write a Java program to get the larger value between first and last element of an
array (length 3) of integers.

Source Code:
[Link];
public class Exercise80 {
public static void main(String[] args)
{
int[] array_nums = {20, 30, 40};
[Link]("Original Array: "+[Link](array_nums));
intmax_val = array_nums[0];
if(array_nums[2] >= max_val)
max_val = array_nums[2];
[Link]("Larger value between first and last element: "+max_val);
}
}

OUTPUT:-
Original Array: [20, 30, 40]
Larger value between first and last element: 40

14
Experiment 14:-
Design a class to represent a bank account. Include the following members:
Data members:
• Name of the depositor
• Account Number
• Type of account
• Balance amount in the account
Methods:
• To assign initial values
• To deposit an amount
• To withdraw an amount
• To display the name and balance

Source Code:
package practice;
import [Link].*;
[Link].*;
[Link];
[Link];

class Bank
{
public String nameOfDepositor;
publicintaccNumber;
public String accType;
public double balanceAmount;
public void assignValues(String nameOfDepositor, String accType, double balanceAmount)
{
[Link]=nameOfDepositor;
[Link]=accType;
[Link]=balanceAmount;

Random random = new Random();


[Link]=[Link](1000000);
[Link]("Your new account number is: "+accNumber);
}
public void depositAmount(double amount)
{

15
if(accNumber==0)
[Link]("!You don't have bank account to deposit\nNote:please assign values to create
an account");
else
{
balanceAmount+=amount;
[Link]("Amount deposited successfully...");
}
}
public void withdrawAmount(double amount)
{
if(accNumber==0)
[Link]("!You don't have bank account to credit\nNote:please assign values to create
an account");
else if(balanceAmount>amount)
{
balanceAmount-=amount;
[Link]("Amount credited
successfully...");
}
else
[Link]("! Insufficient balance");
}
public void displayDetails()
{
if(accNumber==0)
[Link]("!You don't have bank account\nNote:please assign values to create an
account");
else
{
[Link]("Name of the Depositor: "+nameOfDepositor);
[Link]("Balance amount in the account: "+balanceAmount);
}
}
public void getInput()
{
[Link]("How can i help you?");
[Link]("1. Open account");
[Link]("2. Deposit amount");
[Link]("3. Withdraw amount");
[Link]("4. Account details");

16
[Link]("5. Exit");
[Link]("Please choose from above (Eg.2): ");
}
}
class Main
{
public static void main(String[] s) throws IOException
{
[Link]("::::::::::WELCOME TO ICICI BANK::::::::::");
Bank newAccount=new Bank();

try (Scanner scan = new Scanner([Link])) {


intcontinueState=0;

while(continueState==0)
{
[Link]();
intcurrentProcess=[Link]();

if(currentProcess==1)
{
[Link]("Enter your name: ");
String nameOfDepositor=[Link]();
[Link]("Enter your account type: ");
String accType=[Link]();
[Link]("Enter your opening balance: ");
double balanceAmount=[Link]();
[Link](nameOfDepositor, accType, balanceAmount);
}
else if(currentProcess==2)
{
[Link]("Enter amount to deposit: ");
[Link]([Link]());
}
else if(currentProcess==3)
{
[Link]("Enter amount to withdraw: ");
[Link]([Link]());
}
else if(currentProcess==4)

17
{
[Link]();
}
else if(currentProcess==5)
{
continueState=1;
[Link]("THANK YOU");
}

[Link] ("press 0 to continue... ");


continueState=[Link]();
}
}
}
}

OUTPUT:-
::::::::::WELCOME TO ICICI BANK::::::::::
How can i help you?
1. Open account
2. Deposit amount
3. Withdraw amount
4. Account details
5. Exit
Please choose from above (Eg.2): 1
Enter your name: SaumyaRanjanSahu
Enter your account type: saving
Enter your opening balance: 5000
Your new account number is: 402229
press 0 to continue... 0
How can i help you?
1. Open account
2. Deposit amount
3. Withdraw amount
4. Account details
5. Exit
Please choose from above (Eg.2): 2
Enter amount to deposit: 4000
Amount deposited successfully...
press 0 to continue... 0

18
How can i help you?
1. Open account
2. Deposit amount
3. Withdraw amount
4. Account details
5. Exit
Please choose from above (Eg.2): 3
Enter amount to withdraw: 3000
Amount credited successfully...
press 0 to continue... 0
How can i help you?
1. Open account
2. Deposit amount
3. Withdraw amount
4. Account details
5. Exit
Please choose from above (Eg.2): 4
Name of the Depositor:
SaumyaRanjanSahu Balance amount in the
account: 6000.0 press 0 to continue... 0
How can i help you?
1. Open account
2. Deposit amount
3. Withdraw amount
4. Account details
5. Exit
Please choose from above (Eg.2): 5
THANK YOU
press 0 to continue...

19
Experiment 15:-
Given are two one-dimensional arrays, A and B which are sorted in ascending
order. Write a program to merge them into a single sorted array C that contains
every item from arrays A and B, in ascending order.

Source Code:
[Link];
public class MergeArrayProgram
{
private static int[] mergeArray(int[] arrayA, int[] arrayB)
{
int[] mergedArray = new int[[Link] + [Link]];

inti=0, j=0, k=0;


while (i<[Link]&& j <[Link])
{
if (arrayA[i] <arrayB[j])
{
mergedArray[k] = arrayA[i];
i++;
k++;
}
else
{
mergedArray[k] = arrayB[j];
j++;
k++;
}
}
while (i<[Link])
{
mergedArray[k] = arrayA[i];
i++;
k++;
}
while (j <[Link])
{
mergedArray[k] = arrayB[j];
j++;

20
k++;
}
returnmergedArray;
}

public static void main(String[] args)


{
int[] arrayA = new int[] {-7, 12, 17, 29, 41, 56, 79};

int[] arrayB = new int[] {-9, -3, 0, 5, 19};

int[] mergedArray = mergeArray(arrayA, arrayB);

[Link]("Array A : "+[Link](arrayA));

[Link]("Array B : "+[Link](arrayB));

[Link]("Merged Array : "+[Link](mergedArray));


}
}

OUTPUT:-
Array A : [-7, 12, 17, 29, 41, 56, 79]
Array B : [-9, -3, 0, 5, 19]
Merged Array : [-9, -7, -3, 0, 5, 12, 17, 19, 29, 41, 56, 79]

21
Experiment 16:-
Write a java program implementing multiple inheritance.

Source Code:
interfaceMy_restaurents {
void eat();
}
interfaceMy_journey {
void travel();
}
class Holiday implements My_restaurents, My_journey
{ public void eat() {
[Link]("I am trying this food");
}
public void travel() {
[Link]("I am trying this route");
}
}
public class My_trip {
public static void main(String args[])
{ Holiday my_schedule = new Holiday();
my_schedule.eat();
my_schedule.travel();
}
}

OUTPUT:-
I am trying this food
I am trying this route

22
Experiment 17:-
Write a java program implementing package.

Source Code:
package data;
public class Demo
{

public void show()


{
[Link]("Hi Everyone");
}
public void view()
{

[Link]("Hello");
}
}
import data.*;
classncj {
public static void main(String arg[])
{
Demo d = new Demo();
[Link]();
[Link]();
}
}

OUTPUT:-
Hi Everyone
Hello

23
Experiment 18:-
Write a java program to read a file line by line and print the line on the output
screen.

Source Code:
[Link];
[Link];
class Main {
public static void main(String[] args)
{ try {
File file = new File("[Link]");
Scanner sc = new Scanner(file);
[Link]("Reading File Using Scanner:");
while([Link]()) {
[Link]([Link]());
}
[Link]();
} catch (Exception e) {
[Link]();
}
}
}

OUTPUT:-
Reading File Using Scanner:
First Line
Second Line
Third Line
Fourth Line
Fifth Line

24
Experiment 19:-
Write a java program to read content from one file and write it into another file.

Source Code:
[Link];
[Link];
[Link];
[Link];

public class CopyFile {

public static void main(String[] args)


{ try {
File sourceFile = new File("[Link]");
File destinationFile = new File("[Link]");

FileInputStreaminputStream = new FileInputStream(sourceFile);


FileOutputStreamoutputStream = new FileOutputStream(destinationFile);
byte[] buffer = new byte[8192];
intbytesRead;
while ((bytesRead = [Link](buffer)) != -1)
{ [Link](buffer, 0, bytesRead);
}
[Link]();
[Link]();
[Link]("The file has been copied successfully.");
}
catch (IOException e)
{
[Link]("An error occurred while copying the file.");
[Link]();
}
}
}

OUTPUT:-
The file has been copied successfully.

25
Experiment 20:-
Define an exception called “No Match Exception” that is thrown when a string is
not equal to “India”. Write a program that uses this exception.

Source Code:
[Link];
public class NoMatchException extends Exception {

publicNoMatchException(String message) {
super(message);
}
}
public class Main {

public static void main(String[] args)


{ Scanner scanner = new
Scanner([Link]);

[Link]("Enter a string: ");


String input = [Link]();

try {
if ([Link]("India")) {
[Link]("The string is equal to
'India'.");
} else {
throw new NoMatchException("The string is not equal to 'India'.");
}
} catch (NoMatchException e) {
[Link]([Link]());
}
}
}

OUTPUT:-
Enter a string:
foobar
The string is not equal to 'India'.

26
Experiment 21:
Develop a java project for percentage calculator/temperature conversion tool.

Source Code:
import [Link];

public class PercentageCalculator


{ public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);

[Link]("Enter the total value: ");


double total = [Link]();

[Link]("Enter the percentage: ");


double percentage = [Link]();

double result = calculatePercentage(total, percentage);


[Link]("Result: " + result + "%");

[Link]();
}

private static double calculatePercentage(double total, double percentage)


{ return (percentage / 100) * total;
}
}

OUTPUT:

Enter the total value: 200


Enter the percentage: 25
Result: 50.0%

27
Source Code:

import [Link];

public class TemperatureConverter


{ public static void main(String[] args)
{
Scanner scanner = new Scanner([Link]);

[Link]("Enter temperature in Celsius: ");


double celsius = [Link]();

double fahrenheit = convertCelsiusToFahrenheit(celsius);


[Link]("Temperature in Fahrenheit: " + fahrenheit + "°F");

[Link]();
}

private static double convertCelsiusToFahrenheit(double celsius) {


return (celsius * 9/5) + 32;
}
}

OUTPUT:
Enter temperature in Celsius: 30
Temperature in Fahrenheit: 86.0°F

28

You might also like