0% found this document useful (0 votes)
20 views

Java Lab Manual

The document discusses four Java programs: 1) Adding two matrices of a given order N, 2) Developing a stack class to hold integers with methods like push, pop, peek etc, 3) Developing an Employee class with methods to set and get employee details and raise salary by a percentage, 4) Developing a Point class with methods to set and get coordinates and calculate distance between points.

Uploaded by

Vivek S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Java Lab Manual

The document discusses four Java programs: 1) Adding two matrices of a given order N, 2) Developing a stack class to hold integers with methods like push, pop, peek etc, 3) Developing an Employee class with methods to set and get employee details and raise salary by a percentage, 4) Developing a Point class with methods to set and get coordinates and calculate distance between points.

Uploaded by

Vivek S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

OOP WITH JAVA LABOTRATORY(BCS306A)

Program:1.
Develop a JAVA program to add TWO matrices of suitable order N (The value of N
should be read from command line arguments).

Aim: Demonstrating creation of java classes, objects, declaration and initialization of


variables.

import java.util.Scanner;

public class Matrix {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.println("Enter the value of N: ");
int N = scanner.nextInt();

int[][] firstMatrix = new int[N][N];


int[][] secondMatrix = new int[N][N];
int[][] resultMatrix = new int[N][N];

System.out.println("Enter the elements of first matrix: ");


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
firstMatrix[i][j] = scanner.nextInt();
}
}

System.out.println("Enter the elements of second matrix: ");


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
secondMatrix[i][j] = scanner.nextInt();
}
}

for (int i = 0; i < N; i++) {


for (int j = 0; j < N; j++) {
resultMatrix[i][j] = firstMatrix[i][j] + secondMatrix[i][j];
}
}

System.out.println("The resultant matrix is: ");


for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
System.out.print(resultMatrix[i][j] + " ");
}
System.out.println();

Dept. of ISE, JSSATEB Page:5


OOP WITH JAVA LABOTRATORY(BCS306A)
}
}
}

OUTPUT
1. Enter the value of N:
2
Enter the elements of first matrix:
34
45
Enter the elements of second matrix:
56
78
The resultant matrix is:
11 10
11 13
2. Enter the value of N:
3
Enter the elements of first matrix:
345
567
10 22 45
Enter the elements of second matrix:
67 8 9
346
3 67 8
The resultant matrix is:
70 12 14
8 10 13
12 89 53

Dept. of ISE, JSSATEB Page:5


OOP WITH JAVA LABOTRATORY(BCS306A)
Program:2.
Develop a stack class to hold a maximum of 10 integers with suitable methods. Develop a
JAVA main method to illustrate Stack operations.

Aim: Demonstrating creation of java classes, objects, constructors, declaration and


initialization of variables.
public class Stack_Operations {
private int[] stack;
private int top;
private int maxSize;
public Stack_Operations(int maxSize) {
this.maxSize = maxSize;
stack = new int[maxSize];
top = -1;
}

public void push(int item) {


if (!isFull()) {
stack[++top] = item;
} else {
System.out.println("Stack is full");
}
}

public int pop(int i) {


if (!isEmpty()) {
return stack[top--];
} else {
System.out.println("Stack is empty");
return -1;
}
}

public int peek() {


if (!isEmpty()) {
return stack[top];
} else {
System.out.println("Stack is empty");
return -1;
}

Dept.of ISE, JSSATEB Page:9


OOP WITH JAVA LABOTRATORY(BCS306A)
}

public boolean isEmpty() {


return top == -1;
}
public boolean isFull() {
return top == maxSize - 1;
}

public int size() {


return top + 1;
}

public void printStack() {


for (int i = 0; i <= top; i++) {
System.out.print(stack[i] + " ");
}
System.out.println();
}

public static void main(String[] args) {


// Create a stack with a maximum size of 10 integers
Stack_Operations stack = new Stack_Operations(10);
// Push some integers onto the stack
System.out.println("Pushing integers onto the stack:");
for (int i = 0; i < 10; i++) {
stack.push(i);
}
// Print the stack
System.out.println("Stack after pushing:");
stack.printStack();
// Pop some integers off the stack
System.out.println("Popping integers off the stack:");
for (int i = 0; i < 5; i++) {
stack.pop(i);
System.out.println("Stack after pop:");
stack.printStack();

Dept. of ISE, JSSATEB Page:10


OOP WITH JAVA LABOTRATORY(BCS306A)
}
}
}
}

OUTPUT:

0123456789
Popping integers off the stack:
Stack after pop:
012345678
Stack after pop:
01234567
Stack after pop:
0123456
Stack after pop:
012345
Stack after pop:
012340123456789
Popping integers off the stack:
Stack after pop:
012345678
Stack after pop:
01234567
Stack after pop:
0123456
Stack after pop:
012345
Stack after pop:
01234

Dept. of ISE, JSSATEB Page:10


OOP WITH JAVA LABOTRATORY(BCS306A)
Program 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.

Aim: Demonstrating creation of java classes, object, declaration and initializationof v


variables.

public class Employee_Sal_Inc {


private int id;
private String name;
private double salary;

public Employee_Sal_Inc(int id, String name, int salary) {


this.id = id;
this.name = name;
this.salary = salary;
}

public int getId() {


return id;
}

public String getName() {


return name;
}

public double getSalary() {


return salary;
}

public void raiseSalary(double percent) {


double increase = salary * percent / 100;
salary += increase;
}

@Override
public String toString() {
return "Employee{" +
"id=" + id +

Dept. of ISE, JSSATEB Page:12


OOP WITH JAVA LABOTRATORY(BCS306A)
", name='" + name + '\'' +
", salary=" + salary +
'}';
}

public static void main(String[] args) {


Employee_Sal_Inc employee = new Employee_Sal_Inc(1, "John Doe", 50000);
System.out.println("Employee before salary increase:\n " + employee);
System.out.println("Enter how much percent increment you want to give to employee:");
Scanner sc=new Scanner(System.in);
double i=sc.nextDouble();

employee.raiseSalary(i);
System.out.println("Employee after salary increase: \n" + employee);
}
}

OUTPUT :

Employee before salary increase:


Employee{id=1, name='John Doe', salary=50000.0}
Enter how much percent increment you want to give to employee:
15
Employee after salary increase:
Employee{id=1, name='John Doe', salary=57500.0

Dept. of ISE, JSSATEB Page:12


OOP WITH JAVA LABOTRATORY(BCS306A)

Program 4:
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.
Aim: Demonstrating creation of java classes, object, declaration and initializationof
variables.

public class My_Point1 {


public class MyPoint {

}
private int x;
private int y;
public My_Point1(int i, int j) {
this.x = x;
this.y = y;
}

public My_Point1() {
this(0, 0);
}

public void setXY(int x, int y) {

Dept. of ISE, JSSATEB Page:12


OOP WITH JAVA LABOTRATORY(BCS306A)
this.x = x;
this.y = y;

}
public int[] getXY() {
int[] xy = new int[2];
xy[0] = x;
xy[1] = y;
return xy;
}
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
public double distance(int x, int y) {
return Math.sqrt(Math.pow(this.x - x, 2) + Math.pow(this.y - y, 2));
}
public double distance(My_Point1 another) {
return Math.sqrt(Math.pow(this.x - another, 2) + Math.pow(this.y - another, 2));
}
public double distance() {
return Math.sqrt(Math.pow(this.x , 2) + Math.pow(this.y , 2));
}

public static void main(String[] args) {


My_Point1 p1 = new My_Point1();
System.out.println(p1);
My_Point1 p2 = new My_Point1(3, 4);
System.out.println(p2);
p1.setXY(5, 6);
System.out.println(p1);
int[] xy = p1.getXY();
System.out.println(xy[0]);
System.out.println(xy[1]);
System.out.println(p1.distance(2, 3));
System.out.println(p1.distance(p2));
System.out.println(p1.distance());
}
}

Dept. of ISE, JSSATEB Page:12


OOP WITH JAVA LABOTRATORY(BCS306A)
Program: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.
Aim: Demonstrate the core object-oriented concept of inheritance and polymorphism
public abstract class Shape {
private String name;
public Shape(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract double calculateArea();
public abstract double calculatePerimeter();

public static void main(String[] args) {


Shape circle = new Circle("Circle", 5.0);
System.out.println("Area of circle: " + circle.calculateArea());
System.out.println("Perimeter of circle: " + circle.calculatePerimeter());
Shape triangle = new Triangle("Triangle", 3.0, 4.0, 5.0);
System.out.println("Area of triangle: " + triangle.calculateArea());
System.out.println("Perimeter of Triangle"+ triangle.calculatePerimeter());
}

public class Circle extends Shape{


private double radius;
public Circle(String name, double radius) {
super(name);
this.radius = radius;
}
@Override
public double calculateArea() {
return Math.PI * radius * radius;
}
@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;

Dept. of ISE, JSSATEB Page:12


OOP WITH JAVA LABOTRATORY(BCS306A)
}
}

public class Triangle extends Shape {


private double sideA;
private double sideB;
private double sideC;
public Triangle(String name, double sideA, double sideB, double sideC) {
super(name);
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
public double calculateArea() {
double semiperimeter = (sideA + sideB + sideC) / 2;
return Math.sqrt(semiperimeter * (semiperimeter - sideA) * (semiperimeter - sideB) *
(semiperimeter - sideC));
}
public double calculatePerimeter() {
return sideA + sideB + sideC;
}
}
}

OUTPUT :

Area of circle: 78.53981633974483


Perimeter of circle: 31.41592653589793
Area of triangle: 6.0
Perimeter of Triangle12.0

Dept. of ISE, JSSATEB Page:12


OOP WITH JAVA LABOTRATORY(BCS306A)

Program:6
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.
Aim: Demonstrate constructor and method overloading in java programming.

public abstract class Shape {


private String name;
public Shape(String name) {
this.name = name;
}
public String getName() {
return name;
}
public abstract double calculateArea();
public abstract double calculatePerimeter();

public static void main(String[] args) {


double i,j,k,l;
System.out.println("Enter Radius of circle:");
Scanner sc=new Scanner(System.in);
i=sc.nextDouble();
Shape circle = new Circle("Circle",+ i);
System.out.println("Area of circle: " + circle.calculateArea());
System.out.println("Perimeter of circle: " + circle.calculatePerimeter());
System.out.println("Enter sides of Triangle j, k, l:");
j=sc.nextDouble();
k=sc.nextDouble();
l=sc.nextDouble();
Shape triangle = new Triangle("Triangle", +j , + k, + l);
System.out.println("Area of triangle: " + triangle.calculateArea());
System.out.println("Perimeter of Triangle:"+ triangle.calculatePerimeter());
}

public class Circle extends Shape{


private double i;//radius is declared as variable i
public Circle(String name, double i) {
super(name);
this.i = i;
}

Dept. of ISE, JSSATEB Page:12


OOP WITH JAVA LABOTRATORY(BCS306A)
@Override
public double calculateArea() {
return Math.PI * i * i;
}
@Override
public double calculatePerimeter() {
return 2 * Math.PI * i;
}
}

public class Triangle extends Shape {


private double sideA;
private double sideB;
private double sideC;
public Triangle(String name, double sideA, double sideB, double sideC) {
super(name);
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}

public double calculateArea() {


double semiperimeter = (sideA + sideB + sideC) / 2;
return Math.sqrt(semiperimeter * (semiperimeter - sideA) * (semiperimeter - sideB) *
(semiperimeter - sideC));
}

public double calculatePerimeter() {


return sideA + sideB + sideC;
}
}

}
OUTPUT:
1. Enter Radius of circle:

6
Area of circle: 113.09733552923255
Perimeter of circle: 37.69911184307752
Enter sides of Triangle j, k, l:
2
3
2
Area of triangle: 1.984313483298443
Perimeter of Triangle:7.0

Dept. of ISE, JSSATEB Page:12


OOP WITH JAVA LABOTRATORY(BCS306A)

2. Enter Radius of circle:


4
Area of circle: 50.26548245743669
Perimeter of circle: 25.132741228718345
Enter sides of Triangle j, k, l:
5
4
2
Area of triangle: 3.799671038392666
PerimeterofTriangle:11.0

Dept. of ISE, JSSATEB Page:20


OOP WITH JAVA LABOTRATORY(BCS306A)

Program:7
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.

Aim: Demonstrate the use of packages in java programming.

interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);

class Rectangle implements Resizable {


private int width;
private int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public void resizeWidth(int width) {
this.width = width;
}
@Override
public void resizeHeight(int height) {
this.height = height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
}
public static void main(String[] args) {
Rectangle rectangle = new Rectangle(10, 20);
System.out.println("Original width: " + rectangle.getWidth());
System.out.println("Original height: " + rectangle.getHeight());
rectangle.resizeWidth(30);
rectangle.resizeHeight(40);
System.out.println("New width: " + rectangle.getWidth());
System.out.println("New height: " + rectangle.getHeight());
}
}

Dept. of ISE, JSSATEB Page:20


OOP WITH JAVA LABOTRATORY(BCS306A)

OUTPUT:
Original width: 10
Original height: 20
New width: 30
New height: 40

Dept. of ISE, JSSATEB Page:20


OOP WITH JAVA LABOTRATORY(BCS306A)

Program: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.

Aim: Demonstrate the inheritance along with interface in java programming.

public class Outer_Inner {

public void display() {


System.out.println("Outer class display method");

class Inner {
public void display() {
System.out.println("Inner class display method");
}
}
}

public static void main(String[] args) {


Outer_Inner outer = new Outer_Inner();
outer.display();
Outer_Inner.Inner inner = outer.new Inner();
inner.display();
}
}

OUTPUT

Outer class display method


Inner class display method

Dept.of ISE, JSSATEB Page:23


OOP WITH JAVA LABOTRATORY(BCS306A)

Program:9
Develop a JAVA program to raise a custom exception (user defined exception) for Division
By Zero using try, catch, throw and finally

Aim: Demonstrate creation of user defined exception

import java.util.Scanner;

public class Custom_Exception {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter two numbers: ");
int a = scanner.nextInt();
int b = scanner.nextInt();
try {
if (b == 0) {
throw new ArithmeticException("Division by zero is not allowed!");
}

float result = a / b;
System.out.println("Result: " + result);
}
catch (ArithmeticException e)
{
System.out.println(e.getMessage());
} finally {
scanner.close();

}
}
OUTPUT:
Enter two numbers:
10
2
Result: 5.0
Enter two numbers:
10
0
Division by zero is not allowed!

Dept.of ISE, JSSATEB Page:23


OOP WITH JAVA LABOTRATORY(BCS306A)

Program:10.
Develop a JAVA program to create a package named mypack and import & implement it
in a suitable class.

Aim: Demonstrate the IO Package using java programming.

public class MyClass implements Mypackage_interface {


public void display() {
System.out.println("Hello from MyClass!");
}

public interface Mypackage_interface {


void display();
}

public static void main(String[] args) {


MyClass obj = new MyClass();
obj.display();
}

OUTPUT:

Hello from MyClass!

Dept. of ISE, JSSATEB Page:25


OOP WITH JAVA LABOTRATORY(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).

Aim: Exception handling in java, introduction to throwable class, throw, finally.

public class Thread_Example implements Runnable {

private String name;


public Thread_Example(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println("Thread started: " + name);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread ended: " + name);
}

public static void main(String[] args) {


Thread_Example runnableExample1 = new Thread_Example("Thread-1");
Thread_Example runnableExample2 = new Thread_Example("Thread-2");
Thread_Example runnableExample3 = new Thread_Example("Thread-3");
Thread thread1 = new Thread(runnableExample1);
Thread thread2 = new Thread(runnableExample2);
Thread thread3 = new Thread(runnableExample3);
thread1.start();
thread2.start();
thread3.start();
}
}
OUTPUT 1:
Thread started: Thread-1
Thread started: Thread-2
Thread started: Thread-3
Thread ended: Thread-1
Thread ended: Thread-2
Thread ended: Thread-3

Dept.of ISE, JSSATEB Page:26


OOP WITH JAVA LABOTRATORY(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.
Aim: Demonstrate the file operations in java programming.
public class MyThread extends Thread {
public MyThread() {
super();
System.out.println("Child Thread");
}
@Override
public void run() {
try {
for (int i = 0; i < 5; i++) {
System.out.println("Child Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
try {
for (int i = 0; i < 5; i++) {
System.out.println("Main Thread: " + i);
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
OUTPUT1:
Child Thread
Main Thread: 0
Child Thread: 0
Child Thread: 1
Main Thread: 1
Child Thread: 2
Main Thread: 2
Child Thread: 3
Main Thread: 3
Child Thread: 4 Main Thread: 4

Dept. of ISE, JSSATEB Page:30

You might also like