Java Lab
Java Lab
BCS306A
1. 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 AddMatrix{
public static void main(String[] args) {
int row,col,i,j;
Scanner s=new Scanner(System.in);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
res[i][j]=mat1[i][j]+mat2[i][j];
}
}
System.out.println(“Sum of matrices:”);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
System.out.print(res[i][j]+“\t”);
}
System.out.println();
}
}
}
Output:
Enter the number of rows:
2
Enter the number of columns:
2
Enter the elements of Matrix1:
1234
Enter the elements of Matrix2:
5678
Sum of matrices:
6 8
10 12
return poppedValue;
}
// Method to peek the top element of the stack without removing it
public int peek() {
if (isEmpty()) {
System.out.println("Stack is empty. No element to peek.");
return -1;
}
return stackArray[top];
}
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
stack.push(50);
Output:
Stack (top to bottom): 50 40 30 20 10
Dr. Azizkhan F Pathan, Dept. of IS&E, JIT, Davangere3
Object Oriented Programming with JAVA
BCS306A
Popped element: 50
Top element: 40
Stack (top to bottom): 40 30 20 10
3. A class called Employee, which models an employee with an ID, name and salary,
is designed as shown in the following class diagram. The method raiseSalary
(percent) increases the salary by the given percentage. Develop the Employee class
and suitable main method for demonstration.
public class Employee {
private int id;
private String name;
private double salary;
Output:
//Default Constructor
public MyPoint()
{
this.x = 0;
this.y = 0;
}
//Overloaded Constructor
public MyPoint(int x, int y)
{
this.x = x;
this.y = y;
}
class testmypoint{
public static void main(String[] args){
MyPoint point1=new MyPoint(3,4);
MyPoint point2=new MyPoint(6,8);
System.out.println("Point1:"+point1);
System.out.println(("Point2:"+point2);
Output:
Point 1: (3,4)
Point 1: (6,8)
Distance between Point 1and Point 2: 5.0
Distance from Point 1to the origin: 5.0
Point 1 coordinates after setXY: (1,2)
5.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.
public class Shape{
void draw()
{
System.out.println("Drawing Shape");
void erase()
{
System.out.println("erasing Shape");
}
void erase()
{
System.out.println("erasing Circle");
}
}
void erase()
{
System.out.println("erasing Triangle");
}
}
Dr. Azizkhan F Pathan, Dept. of IS&E, JIT, Davangere7
Object Oriented Programming with JAVA
BCS306A
void erase()
{
System.out.println("erasing Square");
}
}
class method{
public static void main(String args[]){
Shape c=new Circle();
Shape t=new Triangle();
Shape s=new 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
// Constructor
public Circle(double radius) {
this.radius = radius;
}
// Constructor
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
System.out.println("Triangle:");
System.out.println("Area: " + triangle.calculateArea());
System.out.println("Perimeter: " + triangle.calculatePerimeter());
}
}
Output:
Circle:
Area: 1.99
Perimeter: 31.42
Triangle:
Area: 6.0
Perimeter: 12.0
// Resizable interface
interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
Output:
Original Rectangle Info:
Rectangle: Width = 10, Height = 5
Resized width to: 15
Resized height to: 8
8. 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.
class Outer {
void display() {
System.out.println("Outer class display method");
}
class Inner {
void display() {
System.out.println("Inner class display method");
}
}
}
try {
double result = divide(numerator, denominator);
System.out.println("Result of division: " + result);
} catch (DivisionByZeroException e) {
System.out.println("Exception caught: " + e.getMessage());
} finally {
System.out.println("Finally block executed");
}
}
}
Output:
Exception caught: Cannot divide by zero!
Finally block executed
10. Develop a JAVA program to create a package named mypack and import &
implement it in a suitable class.
Package mypack
// Inside a folder named 'mypack'
package mypack;
Now, let’s create the main program in a different file outside the mypack folder:
PackageDemo class using mypack Package
// Main program outside the mypack folder
import mypack.MyPackageClass;
//import mypack.*;
}
}
To compile and run this program, you need to follow these steps:
Organize your directory structure as follows:
Output:
Hello from MyPackageClass in mypack package!
Result of adding numbers: 8
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).
@Override
@SuppressWarnings("deprecation")
public void run() {
while (running) {
try {
// Suppress deprecation warning for Thread.sleep()
Thread.sleep(500);
System.out.println("Thread ID: " + Thread.currentThread().getId()
+"
is running.");
} catch (InterruptedException e) {
System.out.println("Thread interrupted.");
}
}
}
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.
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.
// The run method that will be executed when the thread starts
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " Count: " + i);
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " Thread
interrupted.");
}
}
}
}
// Main thread
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + " Thread Count: " + i);
try {
Thread.sleep(500); // Sleep for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " Thread
interrupted.");
}
}
}
}
Output:
main Thread Count: 1
Child Thread Count: 1
main Thread Count: 2
Child Thread Count: 2
main Thread Count: 3
Child Thread Count: 3
main Thread Count: 4
Child Thread Count: 4
main Thread Count: 5
Child Thread Count: 5