Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Oopl

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

1. MatrixAddition.

java
public class MatrixAddition {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Usage: java MatrixAddition N");
System.exit(1);
}
int N = Integer.parseInt(args[0]);
if (N <= 0) {
System.out.println("N must be a positive integer.");
System.exit(1);
}
int[][] matrixA = new int[N][N];
int[][] matrixB = new int[N][N];
int[][] result = new int[N][N];
fillMatrix(matrixA, N);
fillMatrix(matrixB, N);
addMatrices(matrixA, matrixB, result, N);
System.out.println("Matrix A:");
printMatrix(matrixA,N);

System.out.println("Matrix B:");
printMatrix(matrix,N);

System.out.println("Result of Matrix Addition:");


printMatrix(result,N);
}
public static void fillMatrix(int[][] matrix, int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
matrix[i][j] = i + j; // You can change this to your desired values
}
}
}
public static void addMatrices(int[][] A, int[][] B, int[][] result, int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
result[i][j] = A[i][j] + B[i][j];
}
}
}
public static void printMatrix(int[][] matrix, int N) {
for (int i=0;i<N;i++) {
for (int j=0;j<N;j++) {
System.out.print(matrix[i][j]+ " ");
}
System.out.println();
}
}
}

2.StackExample.java

class Stack {

private int maxSize = 10;

private int top;

private int[] stackArray;

public Stack() {

stackArray = new int[maxSize];

top = -1;

public void push(int item) {

if (top < maxSize - 1) {

stackArray[++top] = item;

System.out.println("Pushed: " + item);

} else {

System.out.println("Stack is full. Cannot push " + item);

public int pop() {

if (top >= 0) {

int item = stackArray[top--];

System.out.println("Popped: " + item);

return item;

} else {

System.out.println("Stack is empty. Cannot pop.");

return -1;

}
}

public void display() {

System.out.print("Stack: ");

for (int i = top; i >=0; i--) {

System.out.println(stackArray[i]);

System.out.println();

public class StackExample {

public static void main(String[] args) {

Stack stack = new Stack();

stack.push(5);

stack.push(10);

stack.push(15);

stack.display();

stack.pop();

stack.display();

stack.push(20);

stack.push(25);

stack.display();

3.Employee.java

public 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) {

salary *= (1 + percent / 100);

public void displayEmployeeDetails() {

System.out.println("Employee ID: " + id);

System.out.println("Employee Name: " + name);

System.out.println("Employee Salary: " + salary);

public static void main(String[] args) {

Employee emp1 = new Employee(101, "John Doe", 50000.0);

System.out.println("Initial Employee Details:");

emp1.displayEmployeeDetails();

emp1.raiseSalary(10.0);

System.out.println("\nEmployee Details After Raise:");

emp1.displayEmployeeDetails();

4.TestMyPoint.java

package p1;

class MyPoint {

private int x, y;

public MyPoint() {

this(0, 0);
}

public MyPoint(int x, int y) {

this.x = x;

this.y = y;

public void setXY(int x, int y) {

this.x = x;

this.y = y;

public int[] getXY() {

return new int[]{x, y};

public double distance(int x, int y) {

return Math.hypot(this.x - x, this.y - y);

public double distance(MyPoint another) {

return distance(another.x, another.y);

public double distance() {

return distance(0, 0);

@Override

public String toString() {

return "(" + x + ", " + y + ")";

}
TestMyPoint.java

package p1;

import p1.MyPoint;

public class TestMyPoint {

public static void main(String[] args) {

MyPoint point1 = new MyPoint(1, 2);

MyPoint point2 = new MyPoint(3, 4);

point1.setXY(5, 6);

System.out.println("Point1 coordinates: " + point1.getXY()[0] + ", " + point1.getXY()[1]);

System.out.println("Distance between Point1 and Point2: " + point1.distance(point2));

System.out.println("Distance from Point1 to origin: " + point1.distance());

5.Main.java

class Shape {

void draw() {

System.out.println("Drawing a shape");

void erase() {

System.out.println("Erasing a shape");

class Circle extends Shape {

@Override

void draw() {

System.out.println("Drawing a circle");

@Override

void erase() {

System.out.println("Erasing a circle");

}
}

class Triangle extends Shape {

@Override

void draw() {

System.out.println("Drawing a triangle");

@Override

void erase() {

System.out.println("Erasing a triangle");

class Square extends Shape {

@Override

void draw() {

System.out.println("Drawing a square");

@Override

void erase() {

System.out.println("Erasing a square");

public class Main {

public static void main(String[] args) {

Shape[] shapes = {new Circle(), new Triangle(), new Square()};

for (Shape shape : shapes) {

shape.draw();

shape.erase();

System.out.println();

}
6.Main.java

abstract class Shape {

public abstract double calculateArea();

public abstract double calculatePerimeter();

class Circle extends Shape {

private double radius;

public Circle(double radius) {

this.radius = radius;

@Override

public double calculateArea() {

return Math.PI * radius * radius;

@Override

public double calculatePerimeter() {

return 2 * Math.PI * radius;

class Triangle extends Shape {

private double side1, side2, side3;

public Triangle(double side1, double side2, double side3) {

this.side1 = side1;

this.side2 = side2;

this.side3 = side3;

@Override

public double calculateArea() {

double s = (side1 + side2 + side3) / 2.0;

return Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));

}
@Override

public double calculatePerimeter() {

return side1 + side2 + side3;

public class Main {

public static void main(String[] args) {

Circle circle = new Circle(5.0);

System.out.println("Circle - Area: " + circle.calculateArea());

System.out.println("Circle - Perimeter: " + circle.calculatePerimeter());

Triangle triangle = new Triangle(3.0, 4.0, 5.0);

System.out.println("Triangle - Area: " + triangle.calculateArea());

System.out.println("Triangle - Perimeter: " + triangle.calculatePerimeter());

7.Main.java

interface Resizable {

void resizeWidth(int width);

void resizeHeight(int height);

class Rectangle implements Resizable {

private int width, height;

public Rectangle(int width, int height) {

this.width = width;

this.height = height;

@Override

public void resizeWidth(int newWidth) {

width = newWidth;

@Override
public void resizeHeight(int newHeight) {

height = newHeight;

public void displayDimensions() {

System.out.println("Width: " + width);

System.out.println("Height: " + height);

public class Main {

public static void main(String[] args) {

Rectangle rectangle = new Rectangle(5, 7);

System.out.println("Original Dimensions:");

rectangle.displayDimensions();

rectangle.resizeWidth(8);

rectangle.resizeHeight(10);

System.out.println("Resized Dimensions:");

rectangle.displayDimensions();

8.Main.java

class Outer {

void displayOuter() {

System.out.println("This is the outer class's display method.");

class Inner {

void displayInner() {

System.out.println("This is the inner class's display method.");

public class Main {


public static void main(String[] args) {

Outer outerObj = new Outer();

Outer.Inner innerObj = outerObj.new Inner();

outerObj.displayOuter();

innerObj.displayInner();

9.CustomExceptionExample.java

class DivisionByZeroException extends Exception {

public DivisionByZeroException(String message) {

super(message);

public class CustomExceptionExample {

public static void main(String[] args) {

try {

int numerator = 10;

int denominator = 0;

if (denominator == 0) {

throw new DivisionByZeroException("Division by zero is not allowed.");

int result = numerator / denominator;

System.out.println("Result: " + result);

} catch (DivisionByZeroException e) {

System.err.println("Caught Custom Exception: " + e.getMessage());

} finally {

System.out.println("Finally block executed.");

10.Calculator.java
package mypack;

public class Calculator {

public static int add(int a, int b) {

return a + b;

public static int subtract(int a, int b) {

return a - b;

public static int multiply(int a, int b) {

return a * b;

public static int divide(int a, int b) {

if (b != 0) {

return a / b;

} else {

throw new ArithmeticException("Division by zero is not allowed.");

CalculatorApp.java

import mypack.Calculator;

public class CalculatorApp {

public static void main(String[] args) {


int num1=6;

int num2=4;

System.out.println("Sum:"+Calculator.add(num1,num2));

System.out.println("Difference:"+Calculator.subtract(num1,num2));

System.out.println("Product:"+Calculator.multiply(num1,num2));

System.out.println("Quotient:"+Calculator.divide(num1,num2));

11.RunnableThreadExample.java

class MyRunnable implements Runnable {

public void run() {

try {

System.out.println("Thread started: " + Thread.currentThread().getName());

Thread.sleep(500); // Suspend the thread for 500 milliseconds

System.out.println("Thread ended: " + Thread.currentThread().getName());

} catch (InterruptedException e) {

System.out.println("Thread interrupted: " + Thread.currentThread().getName());

public class RunnableThreadExample {

public static void main(String[] args) {

System.out.println("Main thread started: " + Thread.currentThread().getName());

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

Thread thread = new Thread(new MyRunnable());

thread.start();

System.out.println("Main thread ended: " + Thread.currentThread().getName());

12.ThreadExample.java
class MyThread extends Thread {

public MyThread(String name) {

super(name); // Call the Thread class constructor with a thread name

start(); // Start the thread

public void run() {

for (int i = 1; i <= 5; i++) {

System.out.println(getName() + ": Count " + i);

try {

Thread.sleep(500); // Sleep for 500 milliseconds

} catch (InterruptedException e) {

System.out.println(getName() + " interrupted.");

public class ThreadExample {

public static void main(String[] args) {

System.out.println("Main thread started: " + Thread.currentThread().getName());

MyThread thread1 = new MyThread("Child Thread 1");

MyThread thread2 = new MyThread("Child Thread 2");

for (int i = 1; i <= 5; i++) {

System.out.println("Main thread: Count " + i);

try {

Thread.sleep(500); // Sleep for 500 milliseconds

} catch (InterruptedException e) {

System.out.println("Main thread interrupted.");

System.out.println("Main thread ended: " + Thread.currentThread().getName());

}
}

You might also like