JAVA LAB MANUAL - 3rd SEM
JAVA LAB MANUAL - 3rd SEM
JAVA LAB MANUAL - 3rd SEM
TECHNOLOGY-NORTH CAMPUS
Off International Airport Road, Kundana, Bengaluru -
562110
PREPARED BY:
Prof. P Gopala Krishna
Department Of CSE CITNC
Object Oriented Programming with Java (BCS306A)
LabExp(1): (LEx1_MatrixAddCmdLine.java)
/* 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;
rowFirMat=Integer.parseInt(args[0]);
colFirMat=Integer.parseInt(args[1]);
rowSecMat=Integer.parseInt(args[2]);
colSecMat=Integer.parseInt(args[3]);
Scanner sc = new Scanner(System.in);
b[i][j] = sc.nextInt();
}
}
System.out.println("");
else
{
System.out.println("Addition not possible");
System.out.println("Try Again");
}
}
}
OUTPUT :
Enter all the elements of first matrix:
2
2
2
2
First Matrix:
22
22
Second Matrix:
33
33
Matrix after addition:
55
55
LabExp(1): (LEx1_MatrixAdd.java)
/* Develop a JAVA program to add TWO matrices of suitable order N (The value of N should be read from
Scanner) */
import java.util.Scanner;
{
for (int j = 0; j < n; j++)
{
System.out.print(c[i][j]+" ");
}
System.out.println("");
}
}
else
{
System.out.println("Addition not possible");
System.out.println("Try Again");
}
}
}
OUTPUT:
First Matrix:
22
22
Second Matrix:
33
33
Matrix after addition:
55
55
LabExp(2): (LEx2_TestStack.java)
/* 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 {
private int stck[];
private int tos;
// allocate and initialize stack
Stack(int size) {
stck = new int[size];
tos = -1;
}
mystack2.push(i);
System.out.println("Stack in mystack2:");
for(int i=0; i<8; i++)
System.out.println(mystack2.pop());
}
}
OUTPUT :
Stack in mystack1:
4
3
2
1
0
Stack in mystack2:
7
6
5
4
3
2
1
0
LabExp(3): (LEx3_TestEmployee.java)
/* 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. */
class Employee {
// private instance variable,not accessible from outside this class
private int id;
private String
name;
private double salary;
// Constructors (overloaded)
/** Constructs an Employee instance with default value for Id, name and Salary. */
public Employee() { // 1st (default) constructor
id = 5001;
name = "Rama";
salary = 80000.00;
}
/** Constructs a Circle instance with the given radius and default color */
public Employee(int id,String name,double salary) {
// 2nd (parameterised)constructor
this.id = id;
this.name = name;
this.salary = salary;
}
public void setId(int id){
this.id=id;
}
public int getId(){
return id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setSalary(double salary){
this.salary=salary;
}
public MyPoint(){
x=4;
y=5;
}
public MyPoint(int x,int y){
this.x=x;
this.y=y;
}
public void setX(int x){
this.x=x;
}
public void setY(int y){
this.y=y;
}
class LEx4_TestMyPoint
{
public static void main(String args[]){
System.out.println("Control in the main() programm ...");
MyPoint p1=new MyPoint();
System.out.println(p1);
p1.setX(8);
// Test setter for x
p1.setY(6);
// Test setter for y
System.out.println(" ");
MyPoint p2 = new MyPoint(0, 4);
// Test another constructor
System.out.println(p2);
// Test toString()
// Testing the overloaded methods distance()
System.out.println(p1.distance(p2));
// Overloaded distance() with MyPoint parameter
System.out.println(p2.distance(p1));
// Overloaded distance() with MyPoint parameter
System.out.println(p1.distance(5, 6));
// Overloaded distance() with x, y parameters
System.out.println(p1.distance());
// Overloaded distance() with no parameters
}
}
OUTPUT :
Control in the main() programm ...
(4,5)
x is: 8
y is: 6
(8,6)
{
System.out.println("erasing Square");
}
}
class LEx5_TestInheritance{
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
LabExp(6): (LEx6_TestShape.java)
/*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. */
// Subclass Circle
class Circle extends Shape {
private double radius;
@Override
public double getArea() {
return Math.PI * radius * radius;
}
@Override
public double getPerimeter() {
return 2 * Math.PI * radius;
}
}
// Subclass Rectangle
class Rectangle extends Shape
{ private double length;
private double width;
@Override
@Override
public double getPerimeter() {
return 2 * (length + width);
}
}
// Subclass Triangle
class Triangle extends Shape {
private double side1;
private double side2;
private double side3;
@Override
public double getArea() {
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
public double getPerimeter() {
return side1 + side2 + side3;
}
}
public class LEx6_TestShape {
public static void main(String[] args) {
double r = 4.0;
Circle circle = new Circle(r);
double rs1 = 4.0, rs2 = 6.0;
double ts1 = 3.0, ts2 = 4.0, ts3 = 5.0;
Rectangle rectangle = new Rectangle(rs1, rs2);
Triangle triangle = new Triangle(ts1, ts2, ts3);
System.out.println("Radius of the Circle"+r);
LabExp(7): (LEx7_TestResize.java)
// Resizable
interface interface
Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
@Override
public void resizeHeight(int height) {
this.height = height;
System.out.println("Height Resized to: " + height);
}
return width;
}
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 :1
Exception caught: Cannot divide by zero!
Finally block executed
OUTPUT :2
Result of division: 0.5
Finally block executed
LabExp(10): (LEx10_TestPackage)
/* Develop a JAVA program to create a package named mypack and import & implement
it in a suitable class. */
}
To compile and run this program, you need to follow these steps:
project-directory/
├── mypack/
│ └── MyPackageClass.java
└── PackageDemo.java
ii) Compile the files:
javac mypack/MyPackageClass.java
javac PackageDemo.java
OUTPUT :
Hello from MyPackageClass in mypack package!
Result of adding numbers: 8
LabExp(11): (LEx11_TestRunnableThread.java)
/*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).*/
class MyRunnable implements Runnable {
private volatile boolean running = true;
@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.");
}
}
}
public void stopThread() {
running = false;
}
}
public class TestRunnableThread{
public static void main(String[] args) {
// Create five instances of MyRunnable
MyRunnable myRunnable1 = new MyRunnable();
MyRunnable myRunnable2 = new MyRunnable();
MyRunnable myRunnable3 = new MyRunnable();
MyRunnable myRunnable4 = new MyRunnable();
MyRunnable myRunnable5 = new MyRunnable();
// 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 {