Oop With Java Bcs306a Lab Manual - 2023
Oop With Java Bcs306a Lab Manual - 2023
Prepared By,
Prof. Siddharth Bhatkande
Assistant Professor
fillMatrix(matrix1, 1);
fillMatrix(matrix2, 2);
System.out.println("\nMatrix 2:");
printMatrix(matrix2);
return resultMatrix;
}
In this example, the matrices are filled with sequential values for simplicity, but
you can modify the fillMatrix method to fill the matrices with any values you
prefer.
Output
$ java MatrixAddition 3
Matrix 1:
1 2 3
4 5 6
7 8 9
Matrix 2:
2 3 4
5 6 7
8 9 10
import java.util.Scanner;
public class Stack {
private static final int MAX_SIZE = 10;
private int[] stackArray;
private int top;
public Stack() {
stackArray = new int[MAX_SIZE];
top = -1;
}
System.out.println();
} else {
System.out.println("Stack is empty.");
}
}
int choice;
do {
System.out.println("\nStack Menu:");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Display Stack Contents");
switch (choice) {
case 1:
System.out.print("Enter the value to push: ");
int valueToPush = scanner.nextInt();
stack.push(valueToPush);
break;
case 2:
stack.pop();
break;
case 3:
stack.peek();
break;
case 4:
stack.display();
break;
case 5:
System.out.println("Is the stack empty? " + stack.isEmpty());
break;
case 6:
scanner.close();
}
}
Output
$ java Stack
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display Stack Contents
5. Check if the stack is empty
6. Check if the stack is full
0. Exit
Enter your choice: 4
JCE, CSE Belagavi Page 10
Object Oriented Programming with JAVA BCS306A
Stack is empty.
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display Stack Contents
5. Check if the stack is empty
6. Check if the stack is full
0. Exit
Enter your choice: 5
Is the stack empty? true
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display Stack Contents
5. Check if the stack is empty
6. Check if the stack is full
0. Exit
Enter your choice: 6
Is the stack full? false
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display Stack Contents
5. Check if the stack is empty
6. Check if the stack is full
0. Exit
Enter your choice: 1
Enter the value to push: 10
Pushed: 10
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display Stack Contents
5. Check if the stack is empty
6. Check if the stack is full
0. Exit
Enter your choice: 1
Enter the value to push: 20
Pushed: 20
Stack Menu:
1. Push
2. Pop
3. Peek
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display Stack Contents
5. Check if the stack is empty
6. Check if the stack is full
0. Exit
Enter your choice: 3
Peeked: 20
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display Stack Contents
5. Check if the stack is empty
6. Check if the stack is full
0. Exit
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display Stack Contents
5. Check if the stack is empty
6. Check if the stack is full
0. Exit
Enter your choice: 4
Stack Contents: 10 20 30
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display Stack Contents
5. Check if the stack is empty
6. Check if the stack is full
0. Exit
Enter your choice: 2
Popped: 30
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display Stack Contents
5. Check if the stack is empty
6. Check if the stack is full
0. Exit
Enter your choice: 3
Peeked: 20
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display Stack Contents
5. Check if the stack is empty
6. Check if the stack is full
0. Exit
Enter your choice: 4
Stack Contents: 10 20
Stack Menu:
1. Push
2. Pop
3. Peek
4. Display Stack Contents
5. Check if the stack is empty
6. Check if the stack is full
0. Exit
Enter your choice: 0
Exiting the program. Goodbye!
the Employee class by creating an instance, displaying its details, raising the
salary, and then displaying the updated details.
Output
$ java Employee
Initial Employee Details:
Employee ID: 1, Name: John Doe, Salary: $50000.0
John Doe's salary raised by 10.0%. New salary: $55000.0
MyPoint.java
public class MyPoint {
private int x;
private int y;
// Default constructor
public MyPoint() {
this.x = 0;
JCE, CSE Belagavi Page 21
Object Oriented Programming with JAVA BCS306A
this.y = 0;
}
// Overloaded constructor
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
TestMyPoint.java
point1.setXY(1, 2);
System.out.println("Point1 coordinates after setXY: " + point1.getXY()[0]
+ ", " + point1.getXY()[1]);
This TestMyPoint program creates two MyPoint objects, sets and retrieves
coordinates, and tests the various distance calculation methods. Feel free to
modify and expand this code as needed.
Output
$ java TestMyPoint
Point1 coordinates after setXY: 1, 2
Point2 coordinates: (3, 4)
Distance from Point1 to Point2: 2.8284271247461903
Distance from Point2 to Origin: 5.0
}
}
}
@Override
public void draw() {
System.out.println("Drawing a circle with radius " + radius);
}
@Override
public void erase() {
System.out.println("Erasing a circle with radius " + radius);
}
}
@Override
public void draw() {
System.out.println("Drawing a triangle with base " + base + " and height "
+ height);
}
@Override
public void erase() {
System.out.println("Erasing a triangle with base " + base + " and height " +
height);
}
}
@Override
public void draw() {
System.out.println("Drawing a square with side length " + side);
}
@Override
public void erase() {
System.out.println("Erasing a square with side length " + side);
}
}
In this program, the Shape class is the superclass, and Circle , Triangle ,
and Square are its subclasses. The draw() and erase() methods are overridden
in each subclass. The main method creates an array of Shape objects and
initializes it with instances of the different subclasses. When iterating through
the array and calling the draw() and erase() methods, polymorphism allows the
appropriate overridden methods in each subclass to be executed.
Output
$ java ShapeDemo
Drawing a circle with radius 5.0
abstr
act class Shape {
abstract double calculateArea();
abstract double calculatePerimeter();
}
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
@Override
double calculatePerimeter() {
@Override
double calculateArea() {
// Using Heron's formula to calculate the area of a triangle
double s = (side1 + side2 + side3) / 2;
return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
@Override
double calculatePerimeter() {
return side1 + side2 + side3;
}
}
Output
$ java ShapeDemo
Circle Area: 78.53981633974483
Circle Perimeter: 31.41592653589793
this.width = width;
System.out.println("Resized width to: " + width);
}
@Override
public void resizeHeight(int height) {
this.height = height;
System.out.println("Resized height to: " + height);
}
interface and provides the specific implementation for resizing the width and
height. The main method in the ResizeDemo class creates a Rectangle object,
displays its original information, resizes it, and then displays the updated
information.
Output
$ java ResizeDemo
Original Rectangle Info:
Rectangle: Width = 10, Height = 5
Resized width to: 15
Resized height to: 8
class Outer {
void display() {
System.out.println("Outer class display method");
}
class Inner {
void display() {
System.out.println("Inner class display method");
}
}
}
In this program, the Outer class has a method named display , and it also contains
an inner class named Inner with its own display method. In the main method of
the OuterInnerDemo class, an instance of the outer class (Outer) is created, and
its display method is called. Then, an instance of the inner class (Inner) is
created using the outer class instance, and its display method is called. This
demonstrates the concept of nesting classes in Java.
Output
$ java OuterInnerDemo
Outer class display method
Inner class display method
try {
JCE, CSE Belagavi Page 44
Object Oriented Programming with JAVA BCS306A
} catch (DivisionByZeroException e) {
System.out.println("Exception caught: " + e.getMessage());
} finally {
System.out.println("Finally block executed");
}
}
}
In this program:
Output
$ java CustomExceptionDemo
Exception caught: Cannot divide by zero!
Finally block executed
Program 10 : Packages
Package mypack /
/ Inside a folder named 'mypack'
package mypack;
To compile and run this program, you need to follow these steps:
project-directory/
├── mypack/
│ └── MyPackageClass.java
└── PackageDemo.java
javac mypack/MyPackageClass.java
javac PackageDemo.java
Output
JCE, CSE Belagavi Page 49
Object Oriented Programming with JAVA BCS306A
$ java PackageDemo
Hello from MyPackageClass in mypack package!
Result of adding numbers: 8
@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 class RunnableThreadExample {
e.printStackTrace();
}
Output
$ java RunnableThreadExample
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.
// 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.");
}
}
}
}
In this program:
When you run this program, you’ll observe that both the main thread and the child
thread are executed concurrently, and their outputs may be interleaved.
Output
$ java ThreadConcurrentExample
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