ASHFAK JAVA
ASHFAK JAVA
ASHFAK JAVA
PRACTICAL COMPONENT OF
STUDENT NAME: KA
MOHAMMAD ASHFA
USN: CS-DIP-03
ACADEMIC YEAR: 2024-2025
CERTIFICATE
Sessional Marks
LAB EXPERIMENT 1:
Develop a JAVA program to add TWO matrices of suitable order N (The value
of N should be read from command line arguments).
package add;
import java.util.Scanner;
public class Add
{
public static void main(String[] args)
{
int m, n;
Scanner scan = new Scanner (System.in);
System.out.print(“Enter the number of rows in the matrix:”);
m = scan.nextInt();
System.out.print(“Enter the number of columns in the matrix:”);
n = scan.nextInt();
int a[][] = new int[m][n];
int b[][] = new int[m][n];
int c[][] = new int[m][n];
System.out.println(“Enter all the elements of first matrix:”);
for (int i = 0; i<m; i++)
{
for (int j = 0; j<n; j++)
{
a[i][j] = scan.nextInt();
}
}
System.out.println(“”);
System.out.println(“Enter all the elements of second matrix:”);
for (int i = 0; i<m; i++)
{
for (int j = 0; j<n; j++)
{
b[i][j] = scan.nextInt();
}
}
{
for (int j = 0; j<n; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
}
OUTPUT:
LAB EXPERIMENT 2:
package stackdemo;
import java.util.Scanner;
class Stack
{
int s[]=new int[10];
int top=-1;
int size=3;
void push(int i)
{
if(top==size-1)
System.out.println("Stack Overflow");
else
{
s[++top]=i;
}
}
void pop()
{
if(top==-1)
{
System.out.println("Stack Underflow");
}
else
{
System.out.println("Popped Element="+s[top]);
top--;
}
}
void display()
{
if(top==-1)
{
System.out.println("Stack is Empty\n");
}
else
{
System.out.println("Stack elements are:\n");
for(int i=top;i>=0;i--)
System.out.println(s[i]);
}
}
}
public class Stackdemo{
public static void main(String[] args){
Scanner scan=new Scanner(System.in);
Stack stk=new Stack();
for(;;)
{
System.out.println("\n----Stack Operstions ----- ");
System.out.println("1.Push");
System.out.println("2.Pop");
System.out.println("3.Display");
System.out.println("4.Exit");
System.out.println("Enter your choice:\n");
int choice=scan.nextInt();
switch(choice)
{
DEPARTMENT OF CSE, KVGCE SULLIA Page 4
OBJECT ORIENTED PROGRAMMING IN JAVA BCS306A
case 1:
System.out.println("Enter the element to push");
stk.push(scan.nextInt());
break;
case 2:stk.pop();
break;
case 3:stk.display();
break;
case 4:System.exit(0);
default:
System.out.println("Invalid choice\n");
break;
}
}
}
}
OUTPUT:
LAB EXPERIMENT 3:
A class called employee which models an employee with an ID, name and
salary, as shown in the following class diagram. The method raise
salary(percent) increases the salary by the given percentage. Develop the
employee class and suitable math method for demonstration.
package employeedemo1;
import java.util.*;
class Employee{
private int id;
private String name;
private double salary;
public Employee(int id,String name,double salary){
this.id=id;
this.name=name;
this.salary=salary;
}
public void raisesalary(double percent){
if(percent>0){
double raiseAmount=salary*(percent/100);
salary+=raiseAmount;
System.out.println(name+"'s salary raised by"+percent+"%New salary:"+salary);
}else{
System.out.println("Invalid percentage salary remains unchanged");
}
}
public String toString(){
return "Employee ID:"+id+"\n Name:"+name+"\n salary:"+salary;
}
}
public class EmployeeDemo1 {
public static void main(String[] args) {
OUTPUT:
LAB EXPERIMENT 4:
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 method called distance(int x, int y) that returns the distance from this point
to another point at the
● 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
package pointdemo;
class MyPoints{
int x;
int y;
public MyPoints()
this.x=0;
this.y=0;
this.x=m;
this.y=n;
this.x=m;
this.y=n;
int[] coordinates={this.x,this.y};
return coordinates;
int Xdiff=this.x-m;
int Ydiff=this.y-n;
return Math.sqrt(Xdiff*Xdiff+Ydiff*Ydiff);
return distance(another.x,another.y);
return distance(0,0);
System.out.println("point1:"+point1);
System.out.println("point2:"+point2);
point1.setXY(3,4);
int[] coordinates=point2.getXY();
System.out.println("coordinate of point2:("+coordinates[0]+","+coordinates[1]+")");
OUTPUT:
LAB EXPERIMENT 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. Shape.java
OUTPUT:
LAB EXPERIMENT 6:
{
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
public double calculateArea()
{
double s = (side1 + side2 + side3) / 2;
Object Oriented Programming with JAVA[BCS306A]
Santhosh T, Asst. Prof, Dept. of IS&E, BIET 18
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
public double calculatePerimeter()
{
return side1 + side2 + side3;
}
}
class Main {
public static void main(String[] args)
{
Circle circle = new Circle(5.0);
Triangle triangle = new Triangle(3.0, 4.0, 5.0);
System.out.println("Circle - Area: " + circle.calculateArea());
System.out.println("Circle - Perimeter: " +
circle.calculatePerimeter());
System.out.println("Triangle - Area: " +
triangle.calculateArea());
System.out.println("Triangle - Perimeter: " +
triangle.calculatePerimeter());
}
}
OUTPUT:
LAB EXPERIMENT 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.
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;
}
public void resizeWidth(int newWidth)
{
this.width = newWidth;
}
public void resizeHeight(int newHeight)
{
this.height = newHeight;
}
public void display()
{
System.out.println("Rectangle width: " + width + ", height: " + height);
}
DEPARTMENT OF CSE, KVGCE SULLIA Page 19
OBJECT ORIENTED PROGRAMMING IN JAVA BCS306A
LAB EXPERIMENT 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 Outerclass
{
void display()
{
System.out.println("Outer class display method.");
}
class Innerclass
{
void display()
{
System.out.println("Inner class display method.");
}
}
public static void main(String[] args)
{
Outerclass outer = new Outerclass();
outer.display();
Outerclass.Innerclass inner = outer.new Innerclass();
inner.display();
}
}
OUTPUT:
LAB EXPERIMENT 9:
Develop a JAVA program to raise a custom exception (user defined exception)
for DivisionByZero using try, catch, throw and finally.
catch (DivisionByZeroException e)
{
System.err.println("Error: " + e.getMessage());
}
finally
{
System.out.println("This block always executes,
regardless of exceptions.");
}
}
}
class DivisionByZeroException extends Exception
{
public DivisionByZeroException(String message)
{
super(message);
}
}
OUTPUT:
LAB EXPERIMENT 9:
Develop a JAVA program to raise a custom exception (user defined exception)
for DivisionByZero using try, catch, throw and finally.
{
public DivisionByZeroException(String message)
{
super(message);
}
}
OUTPUT:
Package: mypack
Class name: MyClass
package mypack;
public class MyClass
{
public void display()
{
System.out.println("This is a method from the mypack package!"); }
}
Example.java
import mypack.MyClass;
public class Example
{
public static void main(String[] args)
{
MyClass obj = new MyClass();
obj.display(); // Access the method from the imported package }
}
OUTPUT:
Mythread.java
public class Mythread implements Runnable
{
public void run()
{
for (int i = 1; i<= 5; i++)
{
System.out.println(Thread.currentThread().getName()+" i is " + i); try
{
//sleep current thread for 500 ms
Thread.sleep(500);
}
catch (InterruptedException e)
{
//print the exception message if occurred System.out.println(e.getMessage());
}
}
}
}
class ThreadExample
{
public static void main(String[] args)
{
Mythread myThread = new Mythread();
//thread 1
TestMyThread.java
public class TestMyThread
{
public static void main(String args[])
{
new MyThread();
try
{
for( int k = 5; k > 0; k--)
{
System.out.println ("Running main thread :" + k); Thread.sleep(1000);
}
}
catch (InterruptedException e)
{
}
System.out.println ("Exiting main thread . . ."); }
}
class MyThread extends Thread
{
MyThread()
{
super("Using Thread class");
System.out.println("Child thread:" + this);
start();
}
public void run()
{
try
{
for ( int i =5; i > 0; i--)
{
System.out.println("Child thread" + i);
Thread.sleep (500);
}
}
catch (InterruptedException e)
{
}
System.out.println("exiting child thread …"); }
}
OUTPUT: