Java BCS306A LabManual
Java BCS306A LabManual
Develop a JAVA program to add TWO matrices of suitable order N (The value of N should be read
from command line arguments).
import java.util.Scanner;
class Matrix {
int n = sc.nextInt();
int i, j;
A[i][j] = sc.nextInt();
B[i][j] = sc.nextInt();
System.out.println();
---------OUTPUT---------
Elements of matrix C:
24
68
Program 02: Stack Operations
Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop a JAVA main
method to illustrate Stack operations.
class Stack {
void push(int x) {
if (top == capacity - 1) {
System.out.println("Stack is overflow");
System.exit(1);
a[++top] = x;
int pop() {
if (top == -1) {
System.out.println("Stack is empty");
System.exit(1);
return a[top--];
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
s.push(60);
------------OUTPUT-----------
Inserted element=10
Inserted element=20
Inserted element=30
Inserted element=40
Inserted element=50
Inserted element=60
Popped Element: 60
import java.util.Scanner;
class Employee {
int id;
String name;
float salary;
e.id = 1;
e.name = "MNS";
e.salary = 90000;
---------OUTPUT----------
class MyPoint {
private int x, y;
MyPoint() {
this.x = 0;
this.y = 0;
MyPoint(int x, int y) {
this.x = x;
this.y = y;
this.x = x;
this.y = y;
int[] getxy() {
return coordinates;
}
double distance(int x, int y) {
double distance() {
class TestMyPoint {
Point1.setxy(3, 4);
----------OUTPUT---------
Point 1: (3,4)
Point 2: 1, 2
class Shape {
void erase() {
System.out.println("Erasing shape");
void draw() {
System.out.println("Drawing circle");
void erase() {
System.out.println("Erasing circle");
void draw() {
System.out.println("Drawing triangle");
void erase() {
System.out.println("Erasing triangle");
void draw() {
System.out.println("Drawing square");
void erase() {
System.out.println("Erasing square");
}
c.draw();
c.erase();
t.draw();
t.erase();
s.draw();
s.erase();
----------OUTPUT---------
Drawing circle
Erasing circle
Drawing triangle
Erasing triangle
Drawing square
Erasing square
Program 06: Abstract Class
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
double radius = 5;
double calculateArea() {
double calculatePerimeter() {
double a = 3, b = 4, c = 5;
double calculateArea() {
double s = (a + b + c) / 2;
double calculatePerimeter() {
return a + b + c;
class Program {
----------OUTPUT---------
interface Resizable {
this.width = width;
this.height = height;
@Override
this.width = width;
@Override
this.height = height;
return width;
return height;
rectangle.displayInfo();
rectangle.resizeWidth(15);
rectangle.resizeHeight(8);
rectangle.displayInfo();
--------OUTPUT---------
class Outer {
void display() {
System.out.println("Outer class display method");
}
class Inner {
void display() {
System.out.println("Inner class display method");
}
}
}
public class OuterInnerDemo {
public static void main(String[] args) {
Outer outer = new Outer();
outer.display();
Outer.Inner inner = outer.new Inner();
inner.display();
}
}
-------OUTPUT---------
super(message);
if (denominator == 0) {
int denominator = 0;
try {
} catch (DivisionByZeroException e) {
-------OUTPUT--------
package mypack;
return a + b;
Now, let’s create the main program in a different file outside the mypack folder:
myPackageObject.displayMessage();
To compile and run this program, you need to follow these steps:
project-directory/
├── mypack/
│ └── MyPackageClass.java
└── PackageDemo.java
javac mypack/MyPackageClass.java
javac PackageDemo.java
--------OUTPUT---------
@Override
@SuppressWarnings("deprecation")
while (running) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
running = false;
thread1.start();
thread2.start();
thread3.start();
thread4.start();
thread5.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
myRunnable1.stopThread();
myRunnable2.stopThread();
myRunnable3.stopThread();
myRunnable4.stopThread();
myRunnable5.stopThread();
-------OUTPUT--------
Thread ID: 24 is running.
Thread ID: 21 is running.
Thread ID: 20 is running.
Thread ID: 23 is running.
Thread ID: 22 is running.
Program 12: Thread Class
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.
super(name);
start();
@Override
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
public class ThreadConcurrentExample {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
--------OUTPUT--------