JAVA Lab Record (1)
JAVA Lab Record (1)
(Q1) Develop a JAVA program to add TWO matrices of suitable order N (The value of N should be
read from command line arguments).
PROGRAM -
// Initialize matrices
int[][] matrix1 = new int[N][N];
int[][] matrix2 = new int[N][N];
int[][] result = new int[N][N];
1 / 25
}
}
OUTPUT -
2 / 25
LAB PROGRAM 2
(Q2) Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop
a JAVA main method to illustrate Stack Operations.
PROGRAM -
// Constructor
public Stack()
{
stackArray = new int[MAX_SIZE];
top = -1;
}
3 / 25
{
System.out.println("Elements in the Stack:");
4 / 25
OUTPUT -
5 / 25
LAB PROGRAM 3
(Q3) 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.
PROGRAM -
// Constructor
public Employee(int id, String name, double salary)
{
this.id = id;
this.name = name;
this.salary = salary;
}
// Getter methods
public int getId()
{
return id;
}
6 / 25
}
OUTPUT -
7 / 25
LAB PROGRAM 4
(Q4) Program: A class called MyPoint which models a 2D point with X and Y coordinates, is designed
as follows:
Develop the code for the class MyPoint. Also develop a JAVA program (called TestMyPoint)
to test all the methods defined in the class.
PROGRAM -
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;
}
8 / 25
public int[] getXY()
{
// Display points
System.out.println("Point 1: " + point1.toString());
System.out.println("Point 2: " + point2.toString());
9 / 25
// Get coordinates of point1
OUTPUT -
10 / 25
LAB PROGRAM 5
(Q5) 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 members
data and main program.
PROGRAM -
class Shape
{
// Member functions
public void draw()
{
System.out.println("Drawing a shape");
}
11 / 25
System.out.println("Erasing a triangle");
}
}
class Square extends Shape
{
// Override draw method for Square
@Override
public void draw()
{
System.out.println("Drawing a square");
}
// Demonstrate polymorphism
for (Shape shape : shapes)
{
shape.draw();
shape.erase();
System.out.println(); // Add a newline for clarity
}
}
}
OUTPUT -
12 / 25
LAB PROGRAM 6
(Q6) Develop a JAVA program to create an abstract class Shape with abstract methods
calculateArea() and calculatePerimeters(). Create subclasses Circle and Triangle that extend
the Shape class and implement the respective methods to calculate the area and perimeters
of each shape.
PROGRAM -
// Constructor
public Circle(double radius)
{
this.radius = radius;
}
@Override
public double calculateArea()
{
return Math.PI * radius * radius;
}
@Override
public double calculatePerimeter()
{
return 2 * Math.PI * radius;
}
}
// Constructor
public Triangle(double side1, double side2, double side3)
{
this.side1 = side1;
this.side2 = side2;
13 / 25
this.side3 = side3;
}
// Implement abstract methods
@Override
public double calculateArea()
{
// Heron's formula for the area of a triangle
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
public double calculatePerimeter()
{
return side1 + side2 + side3;
}
}
14 / 25
OUTPUT -
15 / 25
LAB PROGRAM 7
(Q7) 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
implement the Resizable inteeface and implements the resize methods.
PROGRAM -
// Constructor
public Rectangle(int width, int height)
{
this.width = width;
this.height = height;
}
@Override
public void resizeWidth(int width)
{
this.width = width;
System.out.println("Width resized to: " + width);
}
@Override
public void resizeHeight(int height)
{
this.height = height;
System.out.println("Height resized to: " + height);
}
16 / 25
}
OUTPUT -
17 / 25
LAB PROGRAM 8
(Q8) 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.
PROGRAM -
class Outer
{
// Outer class display method
public void display()
{
System.out.println("Outer class display method");
}
// Inner class
class Inner
{
// Inner class display method
public void display()
{
System.out.println("Inner class display method");
}
}
}
// Create an instance of the inner class using the outer class instance
Outer.Inner inner = outer.new Inner();
OUTPUT -
18 / 25
LAB PROGRAM 9
(Q9) Develop a JAVA program to raise a custom exception (user defined exception) for
DivisionByZero using try, catch, throw and finally.
PROGRAM -
19 / 25
}
OUTPUT -
20 / 25
LAB PROGRAM 10
(Q10) Develop a JAVA program to create a package named mypack and import & implement it in a
suitable class.
PROGRAM -
OUTPUT -
21 / 25
LAB PROGRAM 11
(Q11) 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).
PROGRAM -
@Override
public void run()
{
try
{
for (int i = 1; i <= 5; i++)
{
System.out.println(threadName + ": Count - " + i);
22 / 25
thread1.start();
thread2.start();
// Main thread
for (int i = 1; i <= 5; i++)
{
System.out.println("Main Thread: Count - " + i);
try
{
// Sleep for 500 milliseconds
Thread.sleep(500);
}
catch (InterruptedException e)
{
System.out.println("Main thread interrupted.");
}
}
System.out.println("Main thread exiting.");
}
}
OUTPUT -
23 / 25
LAB PROGRAM 12
(Q12) 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.
PROGRAM -
@Override
public void run()
{
try
{
for (int i = 1; i <= 5; i++)
{
System.out.println(Thread.currentThread().getName() + ": Count - " +
i);
// Sleep for 500 milliseconds
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println(Thread.currentThread().getName() + " thread
interrupted.");
}
System.out.println(Thread.currentThread().getName() + " thread exiting.");
}
}
// Main thread
try
{
for (int i = 1; i <= 5; i++)
24 / 25
{
System.out.println(Thread.currentThread().getName() + ": Count - " +
i);
// Sleep for 500 milliseconds
Thread.sleep(500);
}
}
catch (InterruptedException e)
{
System.out.println(Thread.currentThread().getName() + " thread
interrupted.");
}
System.out.println(Thread.currentThread().getName() + " thread exiting.");
}
}
OUTPUT -
25 / 25