BCS306A-OOPs With Java Lab (1)
BCS306A-OOPs With Java Lab (1)
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;
if (args.length != 1) {
return;
int N = Integer.parseInt(args[0]);
if (N <= 0) {
return;
//Read Matrix1
matrixOne[i][j] = sc.nextInt();
//Read Matrix2
matrixTwo[i][j] = sc.nextInt();
}
resultMatrix[i][j] = matrixOne[i][j] +
matrixTwo[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
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 Stack() {
this.top = -1;
stackArray[++top] = value;
} else {
System.out.println("Stack Overflow");
} else {
System.out.println("Stack Underflow");
if (top >= 0) {
System.out.println();
} else {
System.out.println("Stack is empty.");
****************************StackDemo.java***********************************
package jit.cse.oops.lab;
import java.util.Scanner;
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");
switch (choice) {
stack.push(valueToPush);
break;
case 2: stack.pop();
break;
case 3: stack.display();
break;
case 0: System.exit(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:
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
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
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;
this.id = id;
this.name = name;
this.salary = salary;
@Override
****************************EmployeeDemo.java********************************
package jit.cse.oops.lab;
import java.util.Scanner;
String id = ip.next();
System.out.println(obj);
obj.raiseSalary(percent);
System.out.println(obj);
OUTPUT:
PROGRAM-04: A class called MyPoint, which models a 2D point with x and y coordinates, is designed as
follows:
• 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 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;
private int x;
private int y;
// Default constructor
public MyPoint() {
this.x = 0;
this.y = 0;
// Overloaded constructor
this.x = x;
this.y = y;
this.x = x;
this.y = y;
}
// Get x and y in a 2-element int array
*****************************TestMyPoint.java********************************
package jit.cse.oops.lab;
p2.setXY(1, 2);
OUTPUT:
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 {
this.name = name;
******************************Circle.java************************************
package jit.cse.oops.lab;
@Override
System.out.println("Drawing a circle");
@Override
System.out.println("Erasing a circle");
*****************************Triangle.java***********************************
package jit.cse.oops.lab;
super(name);
@Override
System.out.println("Drawing a triangle");
@Override
System.out.println("Erasing a triangle" );
*******************************Square.java***********************************
package jit.cse.oops.lab;
super(name);
@Override
System.out.println("Drawing a square");
@Override
System.out.println("Erasing a square");
****************************ShapeDemo.java***********************************
package jit.cse.oops.lab;
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
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;
******************************Circle.java************************************
package jit.cse.oops.lab;
this.radius = radius;
@Override
double calculateArea() {
@Override
double calculatePerimeter() {
*****************************Triangle.java***********************************
package jit.cse.oops.lab;
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
double calculateArea() {
@Override
double calculatePerimeter() {
****************************ShapeDemo.java***********************************
package jit.cse.oops.lab;
OUTPUT:
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;
}
****************************Rectangle.java***********************************
package jit.cse.oops.lab;
this.width = width;
this.height = height;
@Override
this.width = width;
@Override
this.height = height;
****************************ResizeDemo.java**********************************
package jit.cse.oops.lab;
rectangle.displayInfo();
rectangle.resizeWidth(15);
rectangle.resizeHeight(8);
rectangle.displayInfo();
}
OUTPUT:
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");
}
**************************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:
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;
super(message);
*************************CustomExceptionDemo.java***************************
package jit.cse.oops.lab;
if (denominator == 0) {
int denominator = 0;
try {
} catch (DivisionByZeroException e) {
} finally {
OUTPUT:
Exception caught: Cannot divide by zero!
Finally block executed
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;
*****************************MyPackDemo.java********************************
package jit.cse.oops.lab;
import jit.cse.oops.lab.mypack.MyPackClass;
ob.display();
MyPackClass.show();
OUTPUT:
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;
Thread t;
ThreadRunnable( ){
t.start( );
try{
Thread.sleep(500);
}catch(InterruptedException e){
System.out.println("Child Interrupted");
************************ThreadRunnableDemo.java******************************
package jit.cse.oops.lab;
new ThreadRunnable();
try{
Thread.sleep(1000);
}catch(InterruptedException e){
OUTPUT:
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;
MyThread(){
super("Demo Thread");
start();
try{
Thread.sleep(500);
}catch(InterruptedException e){
System.out.println("Child Interrupted");
***************************MyThreadDemo.java*********************************
package jit.cse.oops.lab;
new MyThread();
try{
Thread.sleep(1000);
}catch(InterruptedException e){
OUTPUT:
Additional Programs
1. Program to delete vowels in a given string
import jit.cse.oops.lab;
import jit.cse.oops.lab;
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]);
}
}
}
}
import jit.cse.oops.lab;
import jit.cse.oops.lab;
VIVA QUESTIONS