// File: MatrixAddition.
java
import [Link];
public class MatrixAddition {
public static void main(String[] args) {
// Check if command line argument is given
if ([Link] != 1) {
[Link]("Usage: java MatrixAddition <N>");
return;
}
int N = [Link](args[0]);
int[][] matrixA = new int[N][N];
int[][] matrixB = new int[N][N];
int[][] result = new int[N][N];
Scanner sc = new Scanner([Link]);
[Link]("Enter elements of Matrix A (" + N + "x" + N + "):");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
matrixA[i][j] = [Link]();
}
}
[Link]("Enter elements of Matrix B (" + N + "x" + N + "):");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
matrixB[i][j] = [Link]();
}
}
// Perform addition
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
result[i][j] = matrixA[i][j] + matrixB[i][j];
}
}
// Display result
[Link]("\nResultant Matrix (A + B):");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
[Link](result[i][j] + "\t");
}
[Link]();
}
[Link]();
}
}
File: [Link]
import [Link];
// Stack class definition
class Stack {
private int[] stack;
private int top;
private final int MAX = 10;
// Constructor
public Stack() {
stack = new int[MAX];
top = -1;
}
// Push method
public void push(int value) {
if (top == MAX - 1) {
[Link]("Stack Overflow! Cannot push " + value);
} else {
stack[++top] = value;
[Link](value + " pushed to stack.");
}
}
// Pop method
public int pop() {
if (top == -1) {
[Link]("Stack Underflow! No elements to pop.");
return -1;
} else {
int poppedValue = stack[top--];
[Link](poppedValue + " popped from stack.");
return poppedValue;
}
}
// Peek method
public int peek() {
if (top == -1) {
[Link]("Stack is empty!");
return -1;
} else {
return stack[top];
}
}
// Display stack elements
public void display() {
if (top == -1) {
[Link]("Stack is empty!");
} else {
[Link]("Stack elements (top to bottom):");
for (int i = top; i >= 0; i--) {
[Link](stack[i]);
}
}
[07/12, 7:09 pm] ~Mahesh: }
}
// Main class to illustrate stack operations
public class StackExample {
public static void main(String[] args) {
Stack s = new Stack();
Scanner sc = new Scanner([Link]);
int choice, value;
do {
[Link]("\n--- Stack Operations Menu ---");
[Link]("1. Push");
[Link]("2. Pop");
[Link]("3. Peek");
[Link]("4. Display");
[Link]("5. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
switch (choice) {
case 1:
[Link]("Enter value to push: ");
value = [Link]();
[Link](value);
break;
case 2:
[Link]();
break;
case 3:
value = [Link]();
if (value != -1)
[Link]("Top element: " + value);
break;
case 4:
[Link]();
break;
case 5:
[Link]("Exiting...");
break;
default:
[Link]("Invalid choice! Try again.");
}
} while (choice != 5);
[Link]();
}
}
Program 3
import [Link];
class Employee {
private int id;
private String name;
private double salary;
public Employee(int id, String name, double salary) {
[Link] = id;
[Link] = name;
[Link] = salary;
}
public void displayDetails() {
[Link]("Employee ID: " + id);
[Link]("Employee Name: " + name);
[Link]("Current Salary: " + salary);
}
public void raiseSalary(double percent) {
if (percent > 0) {
double raiseAmount = salary * percent / 100;
salary += raiseAmount;
[Link]("Salary increased by " + percent + "%.");
} else {
[Link]("Invalid percentage! Must be positive.");
}
}
// Getter for salary (optional)
public double getSalary() {
return salary;
}
}
public class EmployeeDemo {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter Employee ID: ");
int id = [Link]();
[Link](); // consume newline
[Link]("Enter Employee Name: ");
String name = [Link]();
[Link]("Enter Employee Salary: ");
double salary = [Link]();
Employee emp = new Employee(id, name, salary);
[Link]("\n--- Employee Details ---");
[Link]();
[Link]("\nEnter percentage to raise salary: ");
double percent = [Link]();
[Link](percent);
[Link]("\n--- Updated Employee Details ---");
[Link]();
[Link]();
}
}
class MyPoint {
// Instance variables
private int x;
private int y;
// Default constructor (no-arg)
public MyPoint() {
this.x = 0;
this.y = 0;
}
// Overloaded constructor
public MyPoint(int x, int y) {
this.x = x;
this.y = y;
}
// Getter and Setter methods
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
// Method to set both x and y
public void setXY(int x, int y) {
this.x = x;
this.y = y;
}
// Method to get both x and y in an array
public int[] getXY() {
int[] results = new int[2];
results[0] = this.x;
results[1] = this.y;
return results;
}
// toString method
public String toString() {
return "(" + x + "," + y + ")";
}
// Method to calculate distance from this point to (x, y)
public double distance(int x, int y) {
int xDiff = this.x - x;
int yDiff = this.y - y;
return [Link](xDiff * xDiff + yDiff * yDiff);
}
// Overloaded distance(MyPoint another)
public double distance(MyPoint another) {
int xDiff = this.x - another.x;
int yDiff = this.y - another.y;
return [Link](xDiff * xDiff + yDiff * yDiff);
}
// Overloaded distance() to origin
public double distance() {
return [Link](x * x + y * y);
}
}
public class TestMyPoint {
public static void main(String[] args) {
// Create points
MyPoint p1 = new MyPoint(); // default (0,0)
MyPoint p2 = new MyPoint(3, 4); // (3,4)
// Test toString()
[Link]("p1 = " + p1);
[Link]("p2 = " + p2);
// Test setXY() and getXY()
[Link](1, 2);
[Link]("After setting p1 to (1,2): " + p1);
int[] coords = [Link]();
[Link]("p1 x = " + coords[0] + ", y = " + coords[1]);
// Test distance methods
[Link]("Distance from p1 to (3,4): " + [Link](3, 4));
[Link]("Distance from p1 to p2: " + [Link](p2));
[Link]("Distance from p2 to origin: " + [Link]());
}
}
// Base class
class Shape {
void draw() {
[Link]("Drawing a Shape");
}
void erase() {
[Link]("Erasing a Shape");
}
}
// Subclass Circle
class Circle extends Shape {
void draw() {
[Link]("Drawing a Circle");
}
void erase() {
[Link]("Erasing a Circle");
}
}
// Subclass Triangle
class Triangle extends Shape {
void draw() {
[Link]("Drawing a Triangle");
}
void erase() {
[Link]("Erasing a Triangle");
}
}
class Square extends Shape {
void draw() {
[Link]("Drawing a Square");
}
void erase() {
[Link]("Erasing a Square");
}
}
public class TestShape {
public static void main(String[] args) {
Shape s;
s = new Circle(); // Circle object
[Link]();
[Link]();
s = new Triangle(); // Triangle object
[Link]();
[Link]();
s = new Square(); // Square object
[Link]();
[Link]();
}
}
import [Link];
// Abstract class
abstract class Shape {
// Abstract methods
abstract double calculateArea();
abstract double calculatePerimeter();
}
// Subclass: Circle
class Circle extends Shape {
double radius;
Circle(double radius) {
[Link] = radius;
}
@Override
double calculateArea() {
return [Link] * radius * radius;
}
@Override
double calculatePerimeter() {
return 2 * [Link] * radius;
}
}
// Subclass: Triangle
class Triangle extends Shape {
double side1, side2, side3;
Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
@Override
double calculateArea() {
// Using Heron's formula
double s = (side1 + side2 + side3) / 2;
return [Link](s * (s - side1) * (s - side2) * (s - side3));
}
@Override
double calculatePerimeter() {
return side1 + side2 + side3;
}
}
// Main class
public class TestShape {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Circle
[Link]("Enter radius of circle: ");
double radius = [Link]();
Circle c = new Circle(radius);
[Link]("Circle Area = " + [Link]());
[Link]("Circle Perimeter = " + [Link]());
[Link]();
// Triangle
[Link]("Enter sides of triangle (a b c): ");
double a = [Link]();
double b = [Link]();
double cSide = [Link]();
Triangle t = new Triangle(a, b, cSide);
[Link]("Triangle Area = " + [Link]());
[Link]("Triangle Perimeter = " + [Link]());
[Link]();
}
}