//Object-Oriented Programming, Complex Numbers, Matrix Operations
import java.util.Scanner;
class Complex {
private double real;
private double imaginary;
// Constructor
public Complex(double r, double i) {
this.real = r;
this.imaginary = i;
}
// Method to add two complex numbers
public Complex add(Complex other) {
return new Complex(this.real + other.real, this.imaginary +
other.imaginary);
}
// Method to multiply two complex numbers
public Complex multiply(Complex other) {
double newReal = this.real * other.real - this.imaginary * other.imaginary;
double newImaginary = this.real * other.imaginary + this.imaginary *
other.real;
return new Complex(newReal, newImaginary);
}
// Method to display a complex number
public void display() {
if (imaginary >= 0) {
System.out.printf("%.2f + %.2fi", real, imaginary);
} else {
System.out.printf("%.2f - %.2fi", real, Math.abs(imaginary));
}
}
// Method for matrix addition (only for same-sized matrices)
public static Complex[][] matrixAddition(Complex[][] A, Complex[][] B) {
int rows = A.length;
int cols = A[0].length;
Complex[][] result = new Complex[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
result[i][j] = A[i][j].add(B[i][j]);
}
}
return result;
}
// Method for matrix multiplication (A[m x n] * B[n x p] = C[m x p])
public static Complex[][] matrixMultiplication(Complex[][] A, Complex[][] B) {
int rowsA = A.length;
int colsA = A[0].length;
int rowsB = B.length;
int colsB = B[0].length;
if (colsA != rowsB) {
System.out.println("Matrix multiplication is not possible (Columns of A
must match Rows of B).");
return null;
}
Complex[][] result = new Complex[rowsA][colsB];
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsB; j++) {
result[i][j] = new Complex(0, 0);
for (int k = 0; k < colsA; k++) {
result[i][j] = result[i][j].add(A[i][k].multiply(B[k][j]));
}
}
}
return result;
}
// Method to display a matrix of complex numbers
public static void displayMatrix(Complex[][] matrix) {
for (Complex[] row : matrix) {
for (Complex c : row) {
c.display();
System.out.print("\t");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Input matrix A
System.out.print("Enter size of Matrix A (rows and columns): ");
int rowsA = scanner.nextInt();
int colsA = scanner.nextInt();
Complex[][] matrixA = new Complex[rowsA][colsA];
System.out.println("Enter elements for Matrix A (real and imaginary
parts):");
for (int i = 0; i < rowsA; i++) {
for (int j = 0; j < colsA; j++) {
double real = scanner.nextDouble();
double imag = scanner.nextDouble();
matrixA[i][j] = new Complex(real, imag);
}
}
// Input matrix B
System.out.print("Enter size of Matrix B (rows and columns): ");
int rowsB = scanner.nextInt();
int colsB = scanner.nextInt();
Complex[][] matrixB = new Complex[rowsB][colsB];
System.out.println("Enter elements for Matrix B (real and imaginary
parts):");
for (int i = 0; i < rowsB; i++) {
for (int j = 0; j < colsB; j++) {
double real = scanner.nextDouble();
double imag = scanner.nextDouble();
matrixB[i][j] = new Complex(real, imag);
}
}
// Display input matrices
System.out.println("Matrix A:");
displayMatrix(matrixA);
System.out.println("Matrix B:");
displayMatrix(matrixB);
// Perform matrix addition (only if A and B have the same dimensions)
if (rowsA == rowsB && colsA == colsB) {
Complex[][] sumMatrix = matrixAddition(matrixA, matrixB);
System.out.println("Sum of Matrices:");
displayMatrix(sumMatrix);
} else {
System.out.println("Matrix addition is not possible (both matrices must
have the same dimensions).");
}
// Perform matrix multiplication
Complex[][] productMatrix = matrixMultiplication(matrixA, matrixB);
if (productMatrix != null) {
System.out.println("Product of Matrices:");
displayMatrix(productMatrix);
}
scanner.close();
}
}
//create a file
import java.io.File;
import java.io.IOException;
public class CreateFileExample {
public static void main(String[] args) {
try {
File myFile = new File("example.txt");
if (myFile.createNewFile()) {
System.out.println("File created: " + myFile.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
// write a file
import java.io.FileWriter;
import java.io.IOException;
public class WriteFileExample {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("example.txt");
writer.write("Hello, this is a test file!\nWriting in Java is easy.");
writer.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
//Read from a File
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFileExample {
public static void main(String[] args) {
try {
File myFile = new File("example.txt");
Scanner reader = new Scanner(myFile);
while (reader.hasNextLine()) {
String data = reader.nextLine();
System.out.println(data);
}
reader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
//Append to a File
import java.io.FileWriter;
import java.io.IOException;
public class AppendFileExample {
public static void main(String[] args) {
try {
FileWriter writer = new FileWriter("example.txt", true); // 'true' for
appending
writer.write("\nThis line is appended.");
writer.close();
System.out.println("Successfully appended to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}
//Delete a File
import java.io.File;
public class DeleteFileExample {
public static void main(String[] args) {
File myFile = new File("example.txt");
if (myFile.delete()) {
System.out.println("Deleted the file: " + myFile.getName());
} else {
System.out.println("Failed to delete the file.");
}
}
}
//diffrential equation
import java.util.Scanner;
public class DifferentialEquationSolver {
// Define the function f(x, y) = dy/dx here
// You can modify this to any desired differential equation
public static double f(double x, double y) {
// Example: dy/dx = x + y
return x + y;
}
// Runge-Kutta 4th Order Method
public static void rungeKutta(double x0, double y0, double xEnd, double h) {
double x = x0;
double y = y0;
System.out.printf("x = %.4f, y = %.4f%n", x, y);
while (x < xEnd) {
double k1 = h * f(x, y);
double k2 = h * f(x + h / 2, y + k1 / 2);
double k3 = h * f(x + h / 2, y + k2 / 2);
double k4 = h * f(x + h, y + k3);
y = y + (k1 + 2 * k2 + 2 * k3 + k4) / 6;
x = x + h;
System.out.printf("x = %.4f, y = %.4f%n", x, y);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Solving dy/dx = f(x, y) using RK4 Method");
System.out.print("Enter initial x (x0): ");
double x0 = sc.nextDouble();
System.out.print("Enter initial y (y0): ");
double y0 = sc.nextDouble();
System.out.print("Enter final x (xEnd): ");
double xEnd = sc.nextDouble();
System.out.print("Enter step size h: ");
double h = sc.nextDouble();
rungeKutta(x0, y0, xEnd, h);
sc.close();
}
}
//integration
import java.util.Scanner;
public class IntegrationSolver {
// Define your function f(x) here
public static double f(double x) {
// Example: f(x) = x^2 + 2x + 1
return x * x + 2 * x + 1;
}
// Trapezoidal Rule
public static double trapezoidalRule(double a, double b, int n) {
double h = (b - a) / n;
double sum = f(a) + f(b);
for (int i = 1; i < n; i++) {
double x = a + i * h;
sum += 2 * f(x);
}
return (h / 2) * sum;
}
// Simpson's Rule
public static double simpsonsRule(double a, double b, int n) {
if (n % 2 != 0) {
throw new IllegalArgumentException("Simpson's Rule requires an even
number of intervals.");
}
double h = (b - a) / n;
double sum = f(a) + f(b);
for (int i = 1; i < n; i++) {
double x = a + i * h;
sum += (i % 2 == 0) ? 2 * f(x) : 4 * f(x);
}
return (h / 3) * sum;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Numerical Integration Solver");
System.out.println("Function being integrated: f(x) = x^2 + 2x + 1");
System.out.print("Enter lower limit (a): ");
double a = scanner.nextDouble();
System.out.print("Enter upper limit (b): ");
double b = scanner.nextDouble();
System.out.print("Enter number of intervals (n): ");
int n = scanner.nextInt();
double trapezoidalResult = trapezoidalRule(a, b, n);
double simpsonResult = 0;
System.out.printf("Result using Trapezoidal Rule: %.6f%n",
trapezoidalResult);
if (n % 2 == 0) {
simpsonResult = simpsonsRule(a, b, n);
System.out.printf("Result using Simpson's Rule: %.6f%n",
simpsonResult);
} else {
System.out.println("Simpson's Rule requires an even number of
intervals. Skipping...");
}
scanner.close();
}
}
// simultaneous equation
import java.util.Scanner;
public class SimultaneousEquationsSolver {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of variables (n): ");
int n = scanner.nextInt();
double[][] augmentedMatrix = new double[n][n + 1];
System.out.println("Enter the coefficients of the augmented matrix:");
for (int i = 0; i < n; i++) {
System.out.printf("Equation %d: ", i + 1);
for (int j = 0; j <= n; j++) {
augmentedMatrix[i][j] = scanner.nextDouble();
}
}
// Forward Elimination
for (int i = 0; i < n; i++) {
// Partial Pivoting (Optional, but improves accuracy)
for (int k = i + 1; k < n; k++) {
if (Math.abs(augmentedMatrix[k][i]) > Math.abs(augmentedMatrix[i]
[i])) {
double[] temp = augmentedMatrix[i];
augmentedMatrix[i] = augmentedMatrix[k];
augmentedMatrix[k] = temp;
}
}
// Make elements below pivot zero
for (int k = i + 1; k < n; k++) {
double factor = augmentedMatrix[k][i] / augmentedMatrix[i][i];
for (int j = i; j <= n; j++) {
augmentedMatrix[k][j] -= factor * augmentedMatrix[i][j];
}
}
}
// Back Substitution
double[] solution = new double[n];
for (int i = n - 1; i >= 0; i--) {
solution[i] = augmentedMatrix[i][n];
for (int j = i + 1; j < n; j++) {
solution[i] -= augmentedMatrix[i][j] * solution[j];
}
solution[i] /= augmentedMatrix[i][i];
}
// Output
System.out.println("\nSolution:");
for (int i = 0; i < n; i++) {
System.out.printf("x%d = %.6f%n", i + 1, solution[i]);
}
scanner.close();
}
}
// spiral pattern
import java.util.Scanner;
public class SpiralPattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter size of spiral (n x n): ");
int n = scanner.nextInt();
int[][] spiral = new int[n][n];
int value = 1;
int top = 0, bottom = n - 1;
int left = 0, right = n - 1;
while (value <= n * n) {
for (int i = left; i <= right; i++)
spiral[top][i] = value++;
top++;
for (int i = top; i <= bottom; i++)
spiral[i][right] = value++;
right--;
for (int i = right; i >= left; i--)
spiral[bottom][i] = value++;
bottom--;
for (int i = bottom; i >= top; i--)
spiral[i][left] = value++;
left++;
}
// Print the spiral
System.out.println("Spiral Pattern:");
for (int[] row : spiral) {
for (int num : row)
System.out.printf("%4d", num);
System.out.println();
}
scanner.close();
}
}
//circle pattern
import java.util.Scanner;
public class CirclePattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter radius of circle: ");
int radius = scanner.nextInt();
int diameter = 2 * radius;
for (int i = 0; i <= diameter; i++) {
for (int j = 0; j <= diameter; j++) {
// Use circle equation: (x - r)^2 + (y - r)^2 ≈ r^2
int dx = i - radius;
int dy = j - radius;
double distance = Math.sqrt(dx * dx + dy * dy);
// Print '*' if point lies on the approximate boundary of the
circle
if (Math.abs(distance - radius) < 0.8)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}