Object Oriented Programming with Java – BCS306A
PROGRAM-01: Develop a JAVA program to add two matrices of suitable order N(The value of N should be
read from command line argument)
package jit.cse.oops.lab;
import java.util.Scanner;
public class MatrixAdditionDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Check if the number of command line arguments is correct
if (args.length != 1) {
System.out.println("Make Sure to Pass Valid
Argument");
return;
// Parse the command line argument to get the order N
int N = Integer.parseInt(args[0]);
// Check if N is a positive integer
if (N <= 0) {
System.out.println("Please provide a valid positive integer
for the order N.");
return;
// Create matrices of order N
int[][] matrixOne = new int[N][N];
int[][] matrixTwo = new int[N][N];
int[][] resultMatrix = new int[N][N];
//Read Matrix1
System.out.println("Enter the MatrixOne Values");
for(int i = 0; i<N; i++) {
for(int j = 0; j<N; j++) {
matrixOne[i][j] = sc.nextInt();
Department of Computer Science & Engineering, JIT – Bangalore. Page 1
Object Oriented Programming with Java – BCS306A
//Read Matrix2
System.out.println("Enter the MatrixTwo Values");
for(int i = 0; i<N; i++) {
for(int j = 0; j<N; j++) {
matrixTwo[i][j] = sc.nextInt();
}
//Find Summation of matrices
for(int i = 0; i<N; i++) {
for(int j = 0; j<N; j++) {
resultMatrix[i][j] = matrixOne[i][j] +
matrixTwo[i][j];
}
//Print Summation of matrices
System.out.println("The Resultant Matrix after addition ");
for(int i = 0; i<N; i++) {
for(int j = 0; j<N; j++) {
System.out.print(resultMatrix[i][j] + " ");
System.out.println();
OUTPUT:
Enter the MatrixOne Values
1 2
3 4
Enter the MatrixTwo Values
5 6
7 8
The Resultant Matrix after addition
6 8
10 12
Department of Computer Science & Engineering, JIT – Bangalore. Page 2
Object Oriented Programming with Java – BCS306A
PROGRAM-02: Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop a
JAVA main method to illustrate Stack operations.
*********************************Stack.java**********************************
package jit.cse.oops.lab;
public class Stack {
private static final int MAX_SIZE = 10;
private int[] stackArray;
private int top;
public Stack() {
this.stackArray = new int[MAX_SIZE];
this.top = -1;
public void push(int value) {
if (top < MAX_SIZE - 1) {
stackArray[++top] = value;
System.out.println("Pushed: " + value);
} else {
System.out.println("Stack Overflow");
public void pop() {
if (top >= 0) {
int poppedValue = stackArray[top--];
System.out.println("Popped: " + poppedValue);
} else {
System.out.println("Stack Underflow");
Department of Computer Science & Engineering, JIT – Bangalore. Page 3
Object Oriented Programming with Java – BCS306A
public void display() {
if (top >= 0) {
System.out.print("Stack Contents: ");
for (int i = top; i >= 0; i--) {
System.out.print(stackArray[i] + " ");
}
System.out.println();
} else {
System.out.println("Stack is empty.");
****************************StackDemo.java***********************************
package jit.cse.oops.lab;
import java.util.Scanner;
public class StackDemo {
public static void main(String[] args) {
Stack stack = new Stack();
Scanner ip = new Scanner(System.in);
do {
System.out.println("Stack Menu:");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Display Stack Contents");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1: System.out.print("Enter the value to push:");
int valueToPush = scanner.nextInt();
stack.push(valueToPush);
Department of Computer Science & Engineering, JIT – Bangalore. Page 4
Object Oriented Programming with Java – BCS306A
break;
case 2: stack.pop();
break;
case 3: stack.display();
break;
case 0: System.exit(0);
default: System.out.println("Please enter valid
choice");
} while (choice != 0);
ip.close();
OUTPUT:
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 3
Stack is empty.
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 1
Enter the value to push:10
Pushed: 10
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 1
Enter the value to push:20
Pushed: 20
Stack Menu:
Department of Computer Science & Engineering, JIT – Bangalore. Page 5
Object Oriented Programming with Java – BCS306A
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 1
Enter the value to push:30
Pushed: 30
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 1
Enter the value to push:40
Pushed: 40
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 1
Enter the value to push:50
Pushed: 50
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 1
Enter the value to push:60
Stack Overflow
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 3
Stack Contents: 50 40 30 20 10
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 2
Department of Computer Science & Engineering, JIT – Bangalore. Page 6
Object Oriented Programming with Java – BCS306A
Popped: 50
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 2
Popped: 40
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 2
Popped: 30
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 2
Popped: 20
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 2
Popped: 10
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 2
Stack Underflow
Stack Menu:
1. Push
2. Pop
3. Display Stack Contents
0. Exit
Enter your choice: 0
Department of Computer Science & Engineering, JIT – Bangalore. Page 7
Object Oriented Programming with Java – BCS306A
PROGRAM-03: A class called Employee, which models an employee with an ID, name and salary. The
method raiseSalary (percent) increases the salary by the given percentage. Develop the Employee class and
suitable main method for demonstration.
******************************Employee.java**********************************
package jit.cse.oops.lab;
public class Employee {
private String id, name;
private float salary;
public Employee(String id, String name, float salary) {
this.id = id;
this.name = name;
this.salary = salary;
public void raiseSalary(float percent) {
this.salary = this.salary + (this.salary * (percent/100));
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", salary=" + salary +
"]";
****************************EmployeeDemo.java********************************
package jit.cse.oops.lab;
import java.util.Scanner;
public class EmployeeDemo {
public static void main(String[] args) {
Scanner ip = new Scanner(System.in);
Department of Computer Science & Engineering, JIT – Bangalore. Page 8
Object Oriented Programming with Java – BCS306A
System.out.println("Enter Employee Id, Name and Salary");
String id = ip.next();
String name = ip.next();
float salary = ip.nextFloat();
Employee obj = new Employee(id,name,salary);
System.out.println("Employee Details Before Salary Raise");
System.out.println(obj);
System.out.println("Enter the percent raise of salary");
float percent = ip.nextFloat();
obj.raiseSalary(percent);
System.out.println("Employee Details After Salary Raise");
System.out.println(obj);
OUTPUT:
Enter Employee Id, Name and Salary
CS003
Raghava
82000
Employee Details Before Salary Raise
Employee [id=CS003, name=Raghava, salary=82000.0]
Enter the percent raise of salary
5
Employee Details After Salary Raise
Employee [id=CS003, name=Raghava, salary=86100.0]
Department of Computer Science & Engineering, JIT – Bangalore. Page 9
Object Oriented Programming with Java – BCS306A
PROGRAM-04: A class called MyPoint, which models a 2D point with x and y coordinates, is designed as
follows:
• Two instance variables x (int) and y (int).
• A default (or "no-arg") constructor that construct a point at the default location of (0, 0).
• A overloaded constructor that constructs a point with the given x and y coordinates.
• A method setXY() to set both x and y.
• A method getXY() which returns the x and y in a 2-element int array.
• A toString() method that returns a string description of the instance in the format "(x, y)".
• A method called distance(int x, int y) that returns the distance from this point to another
point at the given (x, y) coordinates
• An overloaded distance(MyPoint another) that returns the distance from this point to the
given MyPoint instance (called another)
• Another overloaded distance() method that returns the distance from this point to the origin
(0,0) Develop the code for the class MyPoint. Also develop a JAVA program (called
TestMyPoint) to test all the methods defined in the class.
******************************MyPoint.java**********************************
package jit.cse.oops.lab;
public class MyPoint {
private int x;
private int y;
// Default constructor
public MyPoint() {
this.x = 0;
this.y = 0;
// Overloaded constructor
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
Department of Computer Science & Engineering, JIT – Bangalore. Page 10
Object Oriented Programming with Java – BCS306A
// Set both x and y
public void setXY(int x, int y) {
this.x = x;
this.y = y;
}
// Get x and y in a 2-element int array
public int[] getXY() {
return new int[]{x, y};
// Return a string description of the instance in the format "(x, y)"
public String toString() {
return "(" + x + ", " + y + ")";
// Calculate distance from this point to another point at (x, y) coordinates
public double distance(int x, int y) {
int xDiff = this.x - x;
int yDiff = this.y - y;
return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
// Calculate distance from this point to another MyPoint instance (another)
public double distance(MyPoint another) {
return distance(another.x, another.y);
// Calculate distance from this point to the origin (0,0)
public double distance() {
return distance(0, 0);
Department of Computer Science & Engineering, JIT – Bangalore. Page 11
Object Oriented Programming with Java – BCS306A
*****************************TestMyPoint.java********************************
package jit.cse.oops.lab;
public class TestMyPoint {
public static void main(String[] args) {
// Creating MyPoint objects using different constructors
MyPoint p1 = new MyPoint();
MyPoint p2 = new MyPoint();
MyPoint p3 = new MyPoint(3, 4);
// Testing setXY and getXY methods
p2.setXY(1, 2);
System.out.println("Point1 coordinates after setXY: " + p2.getXY()[0] +
", " + p2.getXY()[1]);
// Testing toString method
System.out.println("The value of x and y in P1 are " + p1);
System.out.println("The value of x and y in P3 are " + p3);
// Testing distance methods
System.out.println("Distance from P2 to P3: " +p2.distance(p3));
System.out.println("Distance from P2 to Origin: " + p2.distance());
OUTPUT:
Point1 coordinates after setXY: 1, 2
The value of x and y in P1 are (0, 0)
The value of x and y in P3 are (3, 4)
Distance from P2 to P3: 2.8284271247461903
Distance from P2 to Origin: 2.23606797749979
Department of Computer Science & Engineering, JIT – Bangalore. Page 12
Object Oriented Programming with Java – BCS306A
PROGRAM-05: Develop a JAVA program to create a class named Shape. Create three sub classes namely:
circle, triangle and square, each class has two member functions named draw () and erase (). Demonstrate
polymorphism concepts by developing suitable methods, defining member data and main program.
****************************Shape.java*************************************
package jit.cse.oops.lab;
class Shape {
protected String name;
public Shape(String name) {
this.name = name;
public void draw() {
System.out.println("Drawing a " + name);
public void erase() {
System.out.println("Erasing a " + name);
******************************Circle.java************************************
package jit.cse.oops.lab;
class Circle extends Shape {
private double radius;
public Circle(String name) {
super(name);
@Override
public void draw() {
System.out.println("Drawing a circle");
Department of Computer Science & Engineering, JIT – Bangalore. Page 13
Object Oriented Programming with Java – BCS306A
@Override
public void erase() {
System.out.println("Erasing a circle");
*****************************Triangle.java***********************************
package jit.cse.oops.lab;
class Triangle extends Shape {
public Triangle(String name) {
super(name);
@Override
public void draw() {
System.out.println("Drawing a triangle");
@Override
public void erase() {
System.out.println("Erasing a triangle" );
*******************************Square.java***********************************
package jit.cse.oops.lab;
class Square extends Shape {
public Square(String name) {
super(name);
@Override
public void draw() {
System.out.println("Drawing a square");
Department of Computer Science & Engineering, JIT – Bangalore. Page 14
Object Oriented Programming with Java – BCS306A
@Override
public void erase() {
System.out.println("Erasing a square");
****************************ShapeDemo.java***********************************
package jit.cse.oops.lab;
public class ShapeDemo {
public static void main(String[] args) {
Shape[] shapes = new Shape[3];
shapes[0] = new Circle("Circle");
shapes[1] = new Triangle("Triangle");
shapes[2] = new Square("Square");
for (Shape shape : shapes) {
shape.draw();
shape.erase();
System.out.println();
OUTPUT:
Drawing a circle
Erasing a circle
Drawing a triangle
Erasing a triangle
Drawing a square
Erasing a square
Department of Computer Science & Engineering, JIT – Bangalore. Page 15
Object Oriented Programming with Java – BCS306A
PROGRAM-06: Develop a JAVA program to create an abstract class Shape with abstract methods
calculateArea() and calculatePerimeter(). Create subclasses Circle and Triangle that extend the Shape class
and implement the respective methods to calculate the area and perimeter of each shape.
****************************Shape.java*************************************
package jit.cse.oops.lab;
public abstract class Shape {
abstract double calculateArea();
abstract double calculatePerimeter();
******************************Circle.java************************************
package jit.cse.oops.lab;
class Circle extends Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
@Override
double calculateArea() {
return Math.PI * radius * radius;
@Override
double calculatePerimeter() {
return 2 * Math.PI * radius;
*****************************Triangle.java***********************************
package jit.cse.oops.lab;
class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
Department of Computer Science & Engineering, JIT – Bangalore. Page 16
Object Oriented Programming with Java – BCS306A
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
double calculateArea() {
// Using Heron's formula to calculate the area of a triangle
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
@Override
double calculatePerimeter() {
return side1 + side2 + side3;
****************************ShapeDemo.java***********************************
package jit.cse.oops.lab;
public class ShapeDemo {
public static void main(String[] args) {
// Creating Circle and Triangle objects
Circle circle = new Circle(5.0);
Triangle triangle = new Triangle(3.0, 4.0, 5.0);
// Calculating and displaying area and perimeter
System.out.println("Circle Area: " + circle.calculateArea());
System.out.println("Circle Perimeter: " +
circle.calculatePerimeter());
System.out.println("\nTriangle Area: " +
triangle.calculateArea());
System.out.println("Triangle Perimeter: " +
triangle.calculatePerimeter());
Department of Computer Science & Engineering, JIT – Bangalore. Page 17
Object Oriented Programming with Java – BCS306A
OUTPUT:
Circle Area: 78.53981633974483
Circle Perimeter: 31.41592653589793
Triangle Area: 6.0
Triangle Perimeter: 12.0
Department of Computer Science & Engineering, JIT – Bangalore. Page 18
Object Oriented Programming with Java – BCS306A
PROGRAM-07: Develop a JAVA program to create an interface Resizable with methods resizeWidth(int
width) and resizeHeight(int height) that allow an object to be resized. Create a class Rectangle that
implements the Resizable interface and implements the resize methods
**************************Resizable.java*************************************
package jit.cse.oops.lab;
public interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
****************************Rectangle.java***********************************
package jit.cse.oops.lab;
class Rectangle implements Resizable {
private int width;
private int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
// Implementation of Resizable interface
@Override
public void resizeWidth(int width) {
this.width = width;
System.out.println("Resized width to: " + width);
@Override
public void resizeHeight(int height) {
this.height = height;
System.out.println("Resized height to: " + height);
Department of Computer Science & Engineering, JIT – Bangalore. Page 19
Object Oriented Programming with Java – BCS306A
public void displayInfo() {
System.out.println("Rectangle: Width = " + width + ", Height = "
+ height);
****************************ResizeDemo.java**********************************
package jit.cse.oops.lab;
public class ResizeDemo {
public static void main(String[] args) {
// Creating a Rectangle object
Rectangle rectangle = new Rectangle(10, 5);
// Displaying the original information
System.out.println("Original Rectangle Info:");
rectangle.displayInfo();
// Resizing the rectangle
rectangle.resizeWidth(15);
rectangle.resizeHeight(8);
// Displaying the updated information
System.out.println("\nUpdated Rectangle Info:");
rectangle.displayInfo();
}
OUTPUT:
Original Rectangle Info:
Rectangle: Width = 10, Height = 5
Resized width to: 15
Resized height to: 8
Updated Rectangle Info:
Rectangle: Width = 15, Height = 8
Department of Computer Science & Engineering, JIT – Bangalore. Page 20
Object Oriented Programming with Java – BCS306A
PROGRAM-08: Develop a JAVA program to create an outer class with a function display. Create another
class inside the outer class named inner with a function called display and call the two functions in the main
class.
*********************************Outer.java**********************************
package jit.cse.oops.lab;
public class Outer {
public void display() {
System.out.println("Inside Display of Outer");
}
public class Inner{
public void display() {
System.out.println("Inside Display of Inner");
}
}
}
**************************InnerOuterDemo.java********************************
package jit.cse.oops.lab;
public class InnerOuterDemo {
public static void main(String[] args) {
Outer out = new Outer();
out.display();
Outer.Inner in = out.new Inner();
in.display();
}
}
OUTPUT:
Inside Display of Outer
Inside Display of Inner
Department of Computer Science & Engineering, JIT – Bangalore. Page 21
Object Oriented Programming with Java – BCS306A
PROGRAM-09: Develop a JAVA program to raise a custom exception (user defined exception) for
DivisionByZero using try, catch, throw and finally.
**********************DivisionByZeroException.java***************************
package jit.cse.oops.lab;
//Custom exception class
class DivisionByZeroException extends Exception {
public DivisionByZeroException(String message) {
super(message);
*************************CustomExceptionDemo.java***************************
package jit.cse.oops.lab;
public class CustomExceptionDemo {
static double divide(int numerator, int denominator) throws
DivisionByZeroException {
if (denominator == 0) {
throw new DivisionByZeroException("Cannot divide by
zero!");
return (double) numerator / denominator;
public static void main(String[] args) {
int numerator = 10;
int denominator = 0;
try {
double result = divide(numerator, denominator);
System.out.println("Result of division: " + result);
} catch (DivisionByZeroException e) {
System.out.println("Exception caught: " +
e.getMessage());
} finally {
Department of Computer Science & Engineering, JIT – Bangalore. Page 22
Object Oriented Programming with Java – BCS306A
System.out.println("Finally block executed");
OUTPUT:
Exception caught: Cannot divide by zero!
Finally block executed
Department of Computer Science & Engineering, JIT – Bangalore. Page 23
Object Oriented Programming with Java – BCS306A
PROGRAM-10: Develop a JAVA program to create a package named mypack and import & implement it in
a suitable class.
***************************MyPackClass.java**********************************
package jit.cse.oops.lab.mypack;
public class MyPackClass {
public void display() {
System.out.println("Inside Display of MyPackClass");
public static void show() {
System.out.println("Inside Show of MyPackClass");
*****************************MyPackDemo.java********************************
package jit.cse.oops.lab;
import jit.cse.oops.lab.mypack.MyPackClass;
public class MyPackDemo {
public static void main(String[] args) {
MyPackClass ob = new MyPackClass();
ob.display();
MyPackClass.show();
OUTPUT:
Inside Display of MyPackClass
Inside Show of MyPackClass
Department of Computer Science & Engineering, JIT – Bangalore. Page 24
Object Oriented Programming with Java – BCS306A
PROGRAM-11: Write a program to illustrate creation of threads using runnable class. (start method start
each of the newly created thread. Inside the run method there is sleep() for suspend the thread for 500
milliseconds).
*************************ThreadRunnable.java*********************************
package jit.cse.oops.lab;
public class ThreadRunnable implements Runnable{
Thread t;
ThreadRunnable( ){
t = new Thread(this, "Demo Thread");
System.out.println("Child Thread: " + t);
t.start( );
public void run( ){
try{
for(int i=5; i>0; i--){
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}catch(InterruptedException e){
System.out.println("Child Interrupted");
System.out.println("Child Thread Exiting");
************************ThreadRunnableDemo.java******************************
package jit.cse.oops.lab;
public class ThreadRunnableDemo {
public static void main(String [] args){
new ThreadRunnable();
try{
for(int i=5; i>0; i--){
Department of Computer Science & Engineering, JIT – Bangalore. Page 25
Object Oriented Programming with Java – BCS306A
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println("Main Thread Interrupted");
}
System.out.println("Main Thread Exiting");
OUTPUT:
Child Thread: Thread[#20,Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Child Thread Exiting
Main Thread: 2
Main Thread: 1
Main Thread Exiting
Department of Computer Science & Engineering, JIT – Bangalore. Page 26
Object Oriented Programming with Java – BCS306A
PROGRAM-12: Develop a program to create a class MyThread in this class a constructor, call the base class
constructor, using super and start the thread. The run method of the class starts after this. It can be observed
that both main thread and created child thread are executed concurrently.
***************************MyThread.java*************************************
package jit.cse.oops.lab;
public class MyThread extends Thread{
MyThread(){
super("Demo Thread");
System.out.println("Child Thread: " + this);
start();
public void run(){
try{
for(int i=5; i>0; i--){
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}catch(InterruptedException e){
System.out.println("Child Interrupted");
System.out.println("Child Thread Exiting");
}
***************************MyThreadDemo.java*********************************
package jit.cse.oops.lab;
public class MyThreadDemo {
public static void main(String[] args) {
new MyThread();
try{
for(int i=5; i>0; i--){
Department of Computer Science & Engineering, JIT – Bangalore. Page 27
Object Oriented Programming with Java – BCS306A
System.out.println("Main Thread: " + i);
Thread.sleep(1000);
}catch(InterruptedException e){
System.out.println("Main Thread Interrupted");
}
System.out.println("Main Thread Exiting");
OUTPUT:
Child Thread: Thread[#20,Demo Thread,5,main]
Main Thread: 5
Child Thread: 5
Child Thread: 4
Main Thread: 4
Child Thread: 3
Child Thread: 2
Main Thread: 3
Child Thread: 1
Child Thread Exiting
Main Thread: 2
Main Thread: 1
Main Thread Exiting
Department of Computer Science & Engineering, JIT – Bangalore. Page 28
Object Oriented Programming with Java – BCS306A
Additional Programs
1. Program to delete vowels in a given string
import jit.cse.oops.lab;
public class RemoveAllVovels {
public static void main(String[] args) {
String string = "Welcome to Candid Java Programming";
System.out.println("Input String : "+string);
string = string.replaceAll("[AaEeIiOoUu]", "");
System.out.println(string);
}
}
2. Program to replace vowels with star
import jit.cse.oops.lab;
public class VowelswithStar {
public static void main(String[] args) {
String string = "Welcome to Candid Java Programming"; //Input
String
System.out.println("Input String : "+string); //Displaying Input
String string = string.replaceAll("[AaEeIiOoUu]", "*"); //Replace
vowels with star
System.out.println(string); //Display the word after replacement
}
}
3. Program to remove duplicate words in given string
import jit.cse.oops.lab;
public class RemoveDuplicate {
public static void main(String[] args) {
String input="Welcome to Java Session Java Session Session
Java";
String[] words=input.split(" ");
for(int i=0;i<words.length;i++) {
Department of Computer Science & Engineering, JIT – Bangalore. Page 29
Object Oriented Programming with Java – BCS306A
if(words[i]!=null) {
for(int j=i+1;j<words.length;j++) {
if(words[i].equals(words[j])) {
words[j]=null;
}
}
}
}
for(int k=0;k<words.length;k++) {
if(words[k]!=null) {
System.out.println(words[k]);
}
}
}
}
4. Program to reverse the string and check whether it is palindrome or not
import jit.cse.oops.lab;
public class PalindromeChecking {
public static void main(String[] args) {
String inpstr ="AMMA";
char[] inpArray = inpstr.toCharArray();
char[] revArray = new char[inpArray.length];
int j=0;
for (int i = inpArray.length - 1; i >= 0; i--) {
revArray[j]=inpArray[i]; j++;
}
String revstr=String.valueOf(revArray);
if(inpstr.equals(revstr)) {
System.out.println("The given string is a Palindrome");
} else {
System.out.println("The given string is not a Palindrome");
}
}
}
Department of Computer Science & Engineering, JIT – Bangalore. Page 30
Object Oriented Programming with Java – BCS306A
5. Find the index of the smallest number in an array
import jit.cse.oops.lab;
public class SmallestNumberIndex {
public static void main(String[] args) {
int a[] = new int[]{12,44,23,56,9,23,78,13};
int min = a[0]; int index=0;
for(int i = 0; i < a.length; i++) {
if(min > a[i]) {
min = a[i]; index=i;
}
}
System.out.println("Index position of Smallest value in a given
array is : "+index);
}
}
Department of Computer Science & Engineering, JIT – Bangalore. Page 31
Object Oriented Programming with Java – BCS306A
VIVA QUESTIONS
1. What are the top Java Features?
2. What is JVM?
3. What is JIT?
4. Difference between JVM, JRE, and JDK.
5. Explain public static void main(String args[]) in Java.
6. What are Packages in Java?
7. Why Packages are used?
8. What are the advantages of Packages in Java?
9. Can we declare Pointer in Java?
10. What is the Wrapper class in Java?
11. Differentiate between instance and local variables.
12. What is a Class Variable?
13. What is the default value stored in Local Variables?
14. Explain the difference between instance variable and a class variable.
15. How to copy an array in Java?
16. What is an object-oriented paradigm?
17. What are the main concepts of OOPs in Java?
18. What are Classes in Java?
19. What is this keyword in Java?
20. What are Brief Access Specifiers and Types of Access Specifiers?
21. What is an object?
22. What are the different ways to create objects in Java?
23. What is the constructor?
24. What are the different types of Polymorphism?
25. What are the different types of Inheritance?
26. What is the basic unit if Encapsulation?
27. What is an interface?
28. What is an abstract classes?
29. Mention the features of interface.
30. What are the differences between abstract classes and interface?
Department of Computer Science & Engineering, JIT – Bangalore. Page 32