Lab Oops
Lab Oops
Lab Oops
BASAVAKALYANENGINEERINGCOLLEGE,
BASAVAKALYAN
Approved by AICTE and Affilated toVTU Belagavi
[ISO Certified:9001-2015]
Basavakalyan-585327Dist.Bidar,Karnataka
Program01:MatrixAddition
JavaCode
import java.util.Scanner;
input.close();
}
Output
8 10
Program02:StackOperations
JavaCode
package com.jsp;
import java.util.Scanner;
Output
StackMenu:
1. Push
2. Pop
3. Peek
4. DisplayStackContents
5. Checkifthestackis empty
6. Check ifthestack is full
0. Exit
Enteryourchoice:4 Stack
is empty.
StackMenu:
1. Push
2. Pop
3. Peek
4. DisplayStackContents
5. Checkifthestackis empty
6. Check ifthestack is full
0. Exit
Enteryourchoice:5
Isthe stack empty?true
StackMenu:
1. Push
2. Pop
3. Peek
4. DisplayStackContents
5. Checkifthestackis empty
6. Checkifthestackis full
0. Exit
Enter your choice: 6
Isthestackfull?false
StackMenu:
1. Push
2. Pop
3. Peek
4. DisplayStackContents
5. Checkifthestackis empty
6. Check ifthestack is full
0. Exit
Enteryourchoice:1
Dept of CSE BKEC ,Basavakalyan Page6
Oops with Java Laboratory Manual
Enterthevaluetopush:10
Pushed: 10
StackMenu:
1. Push
2. Pop
3. Peek
4. DisplayStackContents
5. Checkifthestackis empty
6. Check ifthestack is full
0. Exit
Enteryourchoice:1
Enterthevaluetopush:20
Pushed: 20
StackMenu:
1. Push
2. Pop
3. Peek
4. DisplayStackContents
5. Checkifthestackis empty
6. Check ifthestack is full
0. Exit
Enter your choice: 4
StackContents:1020
StackMenu:
1. Push
2. Pop
3. Peek
4. DisplayStackContents
5. Checkifthestackis empty
6. Check ifthestack is full
0. Exit
Enteryourchoice:3
Peeked: 20
StackMenu:
1. Push
2. Pop
3. Peek
4. DisplayStackContents
5. Checkifthestackis empty
6. Check ifthestack is full
0. Exit
Enteryourchoice:1
StackMenu:
1. Push
2. Pop
3. Peek
4. DisplayStackContents
5. Checkifthestackis empty
6. Check ifthestack is full
0. Exit
Enter your choice: 4
StackContents:102030
StackMenu:
1. Push
2. Pop
3. Peek
4. DisplayStackContents
5. Checkifthestackis empty
6. Check ifthestack is full
0. Exit
Enteryourchoice:2
Popped: 30
StackMenu:
1. Push
2. Pop
3. Peek
4. DisplayStackContents
5. Checkifthestackis empty
6. Check ifthestack is full
0. Exit
Enteryourchoice:3
Peeked: 20
StackMenu:
1. Push
2. Pop
3. Peek
4. DisplayStackContents
5. Checkifthestackis empty
6. Check ifthestack is full
0. Exit
Enter your choice: 4
StackContents:1020
StackMenu:
1. Push
2. Pop
3. Peek
4. DisplayStackContents
5. Checkifthestackis empty
6. Check ifthestack is full
0.Exit
Enteryourchoice:0
Exitingtheprogram.Goodbye!
Program03:EmployeeClass
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)increasesthesalarybythegivenpercentage.Developthe
Employee class and suitable main method for demonstration.
JavaCode
package com.practice;
// 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;
}
Output
Initial Details:
ID: 101
Name: John Doe
Salary: $50000.0
John Doe's salary has been increased by 10.0%.
Program04:2DPointClass
Twoinstancevariablesx(int)andy(int).
Adefault(or“no-arg”)constructorthatconstructapointatthedefault
location of (0, 0).
Aoverloadedconstructorthatconstructs apointwiththegivenxandy
coordinates.
AmethodsetXY()tosetbothxand y.
AmethodgetXY()whichreturnsthexandyina2-elementintarray.
AtoString()methodthatreturnsastring descriptionoftheinstanceinthe
format “(x, y)”.
Amethodcalleddistance(intx,inty)that returnsthedistancefromthispoint to
another point at the given (x, y) coordinates
Anoverloadeddistance(MyPointanother)thatreturnsthedistancefromthis
point to the given MyPoint instance (called another)
Another overloaded distance() method that returns the distance from this
pointtothe origin(0,0)Developthecode fortheclassMyPoint.Alsodevelopa JAVA
program (called TestMyPoint) to test all the methods defined in the class.
JavaCode
package com.practice;
public 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;
}
// toString method
@Override
public String toString() {
return "(" + x + ", " + y + ")";
}
// Overloaded method to calculate distance from this point to another MyPoint instance
public double distance(MyPoint another) {
return distance(another.x, another.y);
}
// Another overloaded method to calculate distance from this point to the origin (0,0)
public double distance() {
return distance(0, 0);
}
Output
Point 1: (0, 0)
Point 2: (3, 4)
New coordinates for Point 1: (1, 2)
Coordinates of Point 2 as array: [3, 4]
Distance from Point 1 to (0, 0): 2.23606797749979
Distance from Point 2 to Point 1: 2.8284271247461903
Distance from Point 1 to (3, 4): 2.8284271247461903
Program05:Inheritance&Polymorphism–ShapeClass
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
Java Code
package com.practice;
class Shape {
// Member functions
public void draw() {
System.out.println("Drawing a shape");
}
Output
Drawing a circle
Erasing a circle
Drawing a triangle
Erasing a triangle
Drawing a square
Erasing a square
Java Code
package com.prcts;
//Subclass Circle
class Circle extends Shape {
private double radius;
// Constructor
public Circle(double radius) {
this.radius = radius;
}
@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}
}
//Subclass Triangle
class Triangle extends Shape {
private double side1, side2, side3;
// Constructor
public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
Dept of CSE BKEC ,Basavakalyan Page18
Oops with Java Laboratory Manual
}
@Override
public double calculatePerimeter() {
return side1 + side2 + side3;
}
}
Output
Circle:
Area: 78.53981633974483
Perimeter: 31.41592653589793
Triangle:
Area: 6.0
Perimeter: 12.0
Program07:Resizable interface
JavaCode
package com.prcts1;
interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
// Constructor
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
// Getter methods
public int getWidth() {
return width;
}
@Override
public void resizeHeight(int height) {
if (height > 0) {
this.height = height;
System.out.println("Height resized to: " + height);
} else {
System.out.println("Invalid height. Height remains unchanged.");
}
}
}
Output
Initial Dimensions:
Width: 10
Height: 5
Width resized to: 15
Height resized to: 8
Program08:Outerclass
JavaCode
package com.prcts1;
class Outer {
// Outer class display function
void display() {
System.out.println("Outer class display");
}
// Inner class
class Inner {
// Inner class display function
void display() {
System.out.println("Inner class display");
}
}
}
//Main class
public class OuterInnerDemo {
public static void main(String[] args) {
// Create an instance of the outer class
Outer outerObj = new Outer();
// Create an instance of the inner class using the outer class instance
Outer.Inner innerObj = outerObj.new Inner();
Output
Program09:CustomException
JavaCode
package com.prcts1;
//Main class
public class CustomExceptionExample {
// Method that performs division and raises the custom exception
public static double performDivision(int numerator, int denominator) throws
DivisionByZeroException {
if (denominator == 0) {
throw new DivisionByZeroException("Division by zero is not allowed.");
}
return (double) numerator / denominator;
}
Output
Exception caught: Division by zero is not allowed.
Finally block executed.
Program10:Packages
Java Code
package mypack;
import mypack.MyClass;
OUTPUT
Program11:Runnable Interface
Java Code
package pack;
// The run method contains the code that will be executed in the new thread
@Override
public void run() {
try {
for (int i = 1; i <= 5; i++) {
System.out.println(threadName + ": Count " + i);
Thread.sleep(500); // Suspend the thread for 500 milliseconds
}
} catch (InterruptedException e) {
System.out.println(threadName + " interrupted.");
}
}
}
Output
Thread 2: Count 1
Thread 1: Count 1
Thread 2: Count 2
Thread 1: Count 2
Thread 2: Count 3
Thread 1: Count 3
Thread 2: Count 4
Thread 1: Count 4
Thread 1: Count 5
Thread 2: Count 5
Program12:ThreadClass
JavaCode
package pack;
// The run method contains the code that will be executed in the new thread
@Override
public void run() {
for (int i = 1; i <= 5; i++) {
System.out.println(Thread.currentThread().getName() + ": Count " + i);
try {
Thread.sleep(500); // Suspend the thread for 500 milliseconds
} catch (InterruptedException e) {
System.out.println(Thread.currentThread().getName() + " interrupted.");
}
}
}
}
OUTPUT
main: Count 1
Child Thread: Count 1
Child Thread: Count 2
main: Count 2
Child Thread: Count 3
main: Count 3
main: Count 4
Child Thread: Count 4
main: Count 5
Child Thread: Count