Java Manual
Java Manual
1.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;
public class MatrixAddition {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the order of matrices (N): ");
int N = scanner.nextInt();
int[][] matrixA = new int[N][N];
int[][] matrixB = new int[N][N];
System.out.println("Enter elements of matrix A:");
inputMatrixElements(matrixA, scanner);
System.out.println("Enter elements of matrix B:");
inputMatrixElements(matrixB, scanner);
System.out.println("Matrix A:");
printMatrix(matrixA);
System.out.println("Matrix B:");
printMatrix(matrixB);
Matrix A:
12
34
Matrix B:
56
78
}
public void push(int value) {
if (isFull()) {
System.out.println("Stack is full. Cannot push " + value);
} else {
top++;
stackArray[top] = value;
System.out.println(value + " pushed to stack");
}
}
this.x = x;
this.y = y;
}
public int[] getXY() {
return new int[]{x, y};
}
public String toString() {
return "(" + x + ", " + y + ")";
}
public double distance(int x, int y) {
int xDiff = this.x - x;
int yDiff = this.y - y;
return Math.sqrt(xDiff * xDiff + yDiff * yDiff);
}
public double distance(MyPoint another) {
return distance(another.x, another.y);
}
public double distance() {
return distance(0, 0);
}
}
public class TestMyPoint {
public static void main(String[] args) {
// Testing MyPoint class methods
MyPoint point1 = new MyPoint();
MyPoint point2 = new MyPoint(3, 4);
point1.setXY(5, 7);
System.out.println("point1 coordinates: " + point1.getXY());
System.out.println("point1: " + point1);
System.out.println("point2: " + point2);
System.out.println("Distance between point1 and (3,4): " + point1.distance(3, 4));
System.out.println("Distance between point1 and point2: " + point1.distance(point2));
@Override
public void erase() {
System.out.println("Erasing Circle");
}
}
class Triangle extends Shape {
@Override
public void draw() {
System.out.println("Drawing Triangle");
}
@Override
public void erase() {
System.out.println("Erasing Triangle");
}
}
square.erase();
}
}
Output: Drawing Circle
Erasing Circle
Drawing Triangle
Erasing Triangle
Drawing Square
Erasing Square
6)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.
class Rectangle {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double calculateArea() {
return length * width;
}
public double calculatePerimeter() {
return 2 * (length + width);
}
}
public class Main {
public static void main(String[] args) {
// Creating a rectangle object with length 5 and width 3
Rectangle rectangle = new Rectangle(5, 3);
// Calculating and printing the area of the rectangle
double area = rectangle.calculateArea();
interface Resizable {
void resizeWidth(int width);
void resizeHeight(int height);
}
class Rectangle implements Resizable {
private int width;
private int height;
public Rectangle(int width, int height) {
this.width = width;
this.height = height;
}
public void resizeWidth(int width) {
this.width = width;
}
public void resizeHeight(int height) {
this.height = height;
}
public void display() {
rectangle.display();
rectangle.resizeWidth(15);
rectangle.resizeHeight(25);
rectangle.display();
}
}
Output: Rectangle Width: 10, Height: 20
Rectangle Width: 15, Height: 25
8) Develop a JAVA program to create an outer class with a function display. Create
another class inside the outer class named inner with a function called display and call
the two functions in the main class.
class Outer {
void display() {
System.out.println("Outer display method");
}
class Inner {
void display() {
System.out.println("Inner display method");
}
}
}
try {
if (divisor == 0) {
throw new DivisionByZeroException("Division by zero is not allowed");
}
}
}
}