VISION INSTITUTE OF TECHNOLOGY, Subject:- Object Oriented Programming with
Java (BCS403)
ALIGARH
Practical file of Object-Oriented Programming with Java
1. Hello World Program
public class HelloWorld
{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
Output:
Hello, World!
2. Sum of Two Numbers
public class Sum
{
public static void main(String[] args)
{
int num1 = 5, num2 = 10, sum;
sum = num1 + num2;
System.out.println("Sum: " + sum);
}
}
Output:
Sum: 15
3. Factorial of a Number
public class Factorial
{
public static void main(String[] args)
{
int num = 5;
long factorial = 1;
for (int i = 1; i <= num; ++i)
Page1 Faculty: SHAHRUKH KAMAL
Shahrukhkamal7@gmail.com
VISION INSTITUTE OF TECHNOLOGY, Subject:- Object Oriented Programming with
Java (BCS403)
ALIGARH
Practical file of Object-Oriented Programming with Java
{
factorial *= i;
}
System.out.println("Factorial of " + num + "
= " + factorial);
}
}
Output:
Factorial of 5 = 120
4. Check Even or Odd
public class EvenOdd
{
public static void main(String[] args)
{
int num = 29;
if(num % 2 == 0)
System.out.println(num + " is even.");
else
System.out.println(num + " is odd.");
}
}
Output:
29 is odd.
5. Print Fibonacci Series
public class Fibonacci
{
public static void main(String[] args)
{
int n = 10, firstTerm = 0, secondTerm = 1;
Page2 Faculty: SHAHRUKH KAMAL
Shahrukhkamal7@gmail.com
VISION INSTITUTE OF TECHNOLOGY, Subject:- Object Oriented Programming with
Java (BCS403)
ALIGARH
Practical file of Object-Oriented Programming with Java
System.out.println("Fibonacci Series till " +
n + " terms:");
for (int i = 1; i <= n; ++i)
{
System.out.print(firstTerm + ", ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}
Output:
Fibonacci Series till 10 terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
6. Check Prime Number
public class PrimeCheck
{
public static void main(String[] args)
{
int num = 29;
boolean flag = false;
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is a prime
number.");
else
System.out.println(num + " is not a prime
number.");
Page3 Faculty: SHAHRUKH KAMAL
Shahrukhkamal7@gmail.com
VISION INSTITUTE OF TECHNOLOGY, Subject:- Object Oriented Programming with
Java (BCS403)
ALIGARH
Practical file of Object-Oriented Programming with Java
}
}
Output:
29 is a prime number.
7. Find Largest of Three Numbers
public class Largest
{
public static void main(String[] args)
{
int num1 = 10, num2 = 20, num3 = 7;
if(num1 >= num2 && num1 >= num3)
System.out.println(num1 + " is the
largest number.");
else if(num2 >= num1 && num2 >= num3)
System.out.println(num2 + " is the
largest number.");
else
System.out.println(num3 + " is the
largest number.");
}
}
Output:
20 is the largest number.
8. Reverse a String
public class ReverseString
{
public static void main(String[] args)
{
String str = "Hello";
String reversed = "";
for (int i = str.length() - 1; i >= 0; i--)
Page4 Faculty: SHAHRUKH KAMAL
Shahrukhkamal7@gmail.com
VISION INSTITUTE OF TECHNOLOGY, Subject:- Object Oriented Programming with
Java (BCS403)
ALIGARH
Practical file of Object-Oriented Programming with Java
{
reversed += str.charAt(i);
}
System.out.println("Reversed string: " +
reversed);
}
}
Output:
Reversed string: olleH
9. Palindromic String Check
public class Palindrome
{
public static void main(String[] args)
{
String str = "radar", reversedStr = "";
int strLength = str.length();
for (int i = (strLength - 1); i >= 0; --i)
{
reversedStr += str.charAt(i);
}
if
(str.toLowerCase().equals(reversedStr.toLowerCase()))
{
System.out.println(str + " is a
Palindrome String.");
}
else
{
System.out.println(str + " is not a
Palindrome String.");
}
}
}
Output:
Page5 Faculty: SHAHRUKH KAMAL
Shahrukhkamal7@gmail.com
VISION INSTITUTE OF TECHNOLOGY, Subject:- Object Oriented Programming with
Java (BCS403)
ALIGARH
Practical file of Object-Oriented Programming with Java
radar is a Palindrome String.
10.Simple Calculator
import java.util.Scanner;
public class Calculator
{
public static void main(String[] args)
{
Scanner reader = new Scanner(System.in);
System.out.print("Enter two numbers: ");
double first = reader.nextDouble();
double second = reader.nextDouble();
System.out.print("Enter an operator (+, -, *,
/): ");
char operator = reader.next().charAt(0);
double result;
switch(operator) {
case '+':
result = first + second;
break;
case '-':
result = first - second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
default:
System.out.printf("Error! operator is
not correct");
return;
}
Page6 Faculty: SHAHRUKH KAMAL
Shahrukhkamal7@gmail.com
VISION INSTITUTE OF TECHNOLOGY, Subject:- Object Oriented Programming with
Java (BCS403)
ALIGARH
Practical file of Object-Oriented Programming with Java
System.out.printf("The result is: %.2f\n",
result);
}
}
Output (example):
Enter two numbers: 12 5
Enter an operator (+, -, *, /): /
The result is: 2.40
Advanced Programs
11.Bubble Sort
public class BubbleSort
{
static void bubbleSort(int[] arr)
{
int n = arr.length;
for (int i = 0; i < n-1; i++)
{
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
public static void main(String[] args)
{
int[] arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
System.out.println("Sorted array:");
for (int value : arr) {
System.out.print(value + " ");
Page7 Faculty: SHAHRUKH KAMAL
Shahrukhkamal7@gmail.com
VISION INSTITUTE OF TECHNOLOGY, Subject:- Object Oriented Programming with
Java (BCS403)
ALIGARH
Practical file of Object-Oriented Programming with Java
}
}
}
Output:
Sorted array:
11 12 22 25 34 64 90
12.Binary Search
public class BinarySearch {
int binarySearch(int[] arr, int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x)
return m;
if (arr[m] < x)
l = m + 1;
else
r = m - 1;
}
return -1;
}
public static void main(String[] args)
{
BinarySearch ob = new BinarySearch();
int[] arr = {2, 3, 4, 10, 40};
int n = arr.length;
int x = 10;
int result = ob.binarySearch(arr, x);
if (result == -1)
System.out.println("Element not
present");
else
System.out.println("Element found at
index " + result);
Page8 Faculty: SHAHRUKH KAMAL
Shahrukhkamal7@gmail.com
VISION INSTITUTE OF TECHNOLOGY, Subject:- Object Oriented Programming with
Java (BCS403)
ALIGARH
Practical file of Object-Oriented Programming with Java
}
}
Output:
Element found at index 3
13.Linked List Implementation
class LinkedList
{
Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
public void printList()
{
Node n = head;
while (n != null)
{
System.out.print(n.data + " ");
n = n.next;
}
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
list.head = new Node(1);
Node second = new Node(2);
Node third = new Node(3);
Page9 Faculty: SHAHRUKH KAMAL
Shahrukhkamal7@gmail.com
VISION INSTITUTE OF TECHNOLOGY, Subject:- Object Oriented Programming with
Java (BCS403)
ALIGARH
Practical file of Object-Oriented Programming with Java
list.head.next = second;
second.next = third;
list.printList();
}
}
Output:
1 2 3
14.HashMap Example
java
Copy code
import java.util.HashMap;
public class HashMapExample
{
public static void main(String[] args)
{
HashMap<String, Integer> map = new
HashMap<>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
for (String key : map.keySet())
{
System.out.println("Key: " + key + ",
Value: " + map.get(key));
}
}
}
Output:
yaml
Copy code
Page10 Faculty: SHAHRUKH KAMAL
Shahrukhkamal7@gmail.com
VISION INSTITUTE OF TECHNOLOGY, Subject:- Object Oriented Programming with
Java (BCS403)
ALIGARH
Practical file of Object-Oriented Programming with Java
Key: One, Value: 1
Key: Two, Value: 2
Key: Three, Value:3
Page11 Faculty: SHAHRUKH KAMAL
Shahrukhkamal7@gmail.com