Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Compiled class files
*.class

# Package files
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# Virtual machine crash logs
hs_err_pid*

# IDE specific files
.idea/
*.iml
.vscode/
.settings/
.project
.classpath

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# Temporary files
*.tmp
*.temp
student_data.txt

# Build directories
bin/
build/
target/
10 changes: 10 additions & 0 deletions Complex/compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
# Compile script for Complex level programs

echo "Compiling Complex level Java programs..."
cd src
javac *.java
echo "Compilation complete! You can now run:"
echo " java MultithreadingExample"
echo " java DesignPatterns"
echo " java AdvancedAlgorithms"
179 changes: 179 additions & 0 deletions Complex/src/AdvancedAlgorithms.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/**
* Complex Program 3: Advanced Algorithms
* Description: Implements sorting algorithms, binary search, and recursive solutions
* Concepts: Algorithms, recursion, time complexity, searching and sorting
*/
import java.util.Arrays;
import java.util.Random;

public class AdvancedAlgorithms {

// Quick Sort Algorithm
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
}

private static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;

for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
swap(arr, i, j);
}
}
swap(arr, i + 1, high);
return i + 1;
}

private static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

// Merge Sort Algorithm
public static void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}

private static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;

int[] leftArr = new int[n1];
int[] rightArr = new int[n2];

System.arraycopy(arr, left, leftArr, 0, n1);
System.arraycopy(arr, mid + 1, rightArr, 0, n2);

int i = 0, j = 0, k = left;

while (i < n1 && j < n2) {
if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}

while (i < n1) {
arr[k] = leftArr[i];
i++;
k++;
}

while (j < n2) {
arr[k] = rightArr[j];
j++;
k++;
}
}

// Binary Search Algorithm
public static int binarySearch(int[] arr, int target) {
int left = 0, right = arr.length - 1;

while (left <= right) {
int mid = left + (right - left) / 2;

if (arr[mid] == target) {
return mid;
}

if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}

return -1; // Element not found
}

// Fibonacci using recursion with memoization
private static long[] fibMemo = new long[100];

public static long fibonacci(int n) {
if (n <= 1) {
return n;
}

if (fibMemo[n] != 0) {
return fibMemo[n];
}

fibMemo[n] = fibonacci(n - 1) + fibonacci(n - 2);
return fibMemo[n];
}

// Tower of Hanoi recursive solution
public static void towerOfHanoi(int n, char source, char destination, char auxiliary) {
if (n == 1) {
System.out.println("Move disk 1 from " + source + " to " + destination);
return;
}

towerOfHanoi(n - 1, source, auxiliary, destination);
System.out.println("Move disk " + n + " from " + source + " to " + destination);
towerOfHanoi(n - 1, auxiliary, destination, source);
}

public static void main(String[] args) {
System.out.println("=== Advanced Algorithms Example ===");

// Generate random array for sorting
Random random = new Random();
int[] originalArray = new int[10];
for (int i = 0; i < originalArray.length; i++) {
originalArray[i] = random.nextInt(100);
}

System.out.println("Original Array: " + Arrays.toString(originalArray));

// Quick Sort Demo
int[] quickSortArray = originalArray.clone();
long startTime = System.nanoTime();
quickSort(quickSortArray, 0, quickSortArray.length - 1);
long quickSortTime = System.nanoTime() - startTime;
System.out.println("Quick Sort Result: " + Arrays.toString(quickSortArray));
System.out.println("Quick Sort Time: " + quickSortTime + " nanoseconds");

// Merge Sort Demo
int[] mergeSortArray = originalArray.clone();
startTime = System.nanoTime();
mergeSort(mergeSortArray, 0, mergeSortArray.length - 1);
long mergeSortTime = System.nanoTime() - startTime;
System.out.println("Merge Sort Result: " + Arrays.toString(mergeSortArray));
System.out.println("Merge Sort Time: " + mergeSortTime + " nanoseconds");

// Binary Search Demo
int target = quickSortArray[5]; // Pick a random element
int index = binarySearch(quickSortArray, target);
System.out.println("\nBinary Search for " + target + ": Found at index " + index);

// Fibonacci Demo
System.out.println("\nFibonacci Sequence (first 15 numbers):");
for (int i = 0; i < 15; i++) {
System.out.print(fibonacci(i) + " ");
}
System.out.println();

// Tower of Hanoi Demo
System.out.println("\nTower of Hanoi (3 disks):");
towerOfHanoi(3, 'A', 'C', 'B');
}
}
156 changes: 156 additions & 0 deletions Complex/src/DesignPatterns.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* Complex Program 2: Design Patterns - Singleton and Factory
* Description: Demonstrates Singleton and Factory design patterns
* Concepts: Design patterns, thread-safe singleton, factory method pattern
*/

// Singleton Pattern - Database Connection
class DatabaseConnection {
private static volatile DatabaseConnection instance;
private String connectionString;

// Private constructor to prevent instantiation
private DatabaseConnection() {
connectionString = "jdbc:mysql://localhost:3306/testdb";
System.out.println("Database connection created!");
}

// Thread-safe singleton implementation
public static DatabaseConnection getInstance() {
if (instance == null) {
synchronized (DatabaseConnection.class) {
if (instance == null) {
instance = new DatabaseConnection();
}
}
}
return instance;
}

public void connect() {
System.out.println("Connected to: " + connectionString);
}

public void disconnect() {
System.out.println("Disconnected from database");
}
}

// Factory Pattern - Shape Factory
abstract class Shape {
protected String color;

public Shape(String color) {
this.color = color;
}

public abstract void draw();
public abstract double getArea();
}

class Circle extends Shape {
private double radius;

public Circle(String color, double radius) {
super(color);
this.radius = radius;
}

@Override
public void draw() {
System.out.println("Drawing " + color + " circle with radius: " + radius);
}

@Override
public double getArea() {
return Math.PI * radius * radius;
}
}

class Rectangle extends Shape {
private double width, height;

public Rectangle(String color, double width, double height) {
super(color);
this.width = width;
this.height = height;
}

@Override
public void draw() {
System.out.println("Drawing " + color + " rectangle " + width + "x" + height);
}

@Override
public double getArea() {
return width * height;
}
}

class Triangle extends Shape {
private double base, height;

public Triangle(String color, double base, double height) {
super(color);
this.base = base;
this.height = height;
}

@Override
public void draw() {
System.out.println("Drawing " + color + " triangle with base: " + base + ", height: " + height);
}

@Override
public double getArea() {
return 0.5 * base * height;
}
}

class ShapeFactory {
public static Shape createShape(String shapeType, String color, double... dimensions) {
switch (shapeType.toLowerCase()) {
case "circle":
return new Circle(color, dimensions[0]);
case "rectangle":
return new Rectangle(color, dimensions[0], dimensions[1]);
case "triangle":
return new Triangle(color, dimensions[0], dimensions[1]);
default:
throw new IllegalArgumentException("Unknown shape type: " + shapeType);
}
}
}

public class DesignPatterns {
public static void main(String[] args) {
System.out.println("=== Design Patterns Example ===");

// Singleton Pattern Demo
System.out.println("\n1. Singleton Pattern:");
DatabaseConnection db1 = DatabaseConnection.getInstance();
DatabaseConnection db2 = DatabaseConnection.getInstance();

System.out.println("Same instance? " + (db1 == db2));
db1.connect();
db2.disconnect();

// Factory Pattern Demo
System.out.println("\n2. Factory Pattern:");
try {
Shape circle = ShapeFactory.createShape("circle", "Red", 5.0);
Shape rectangle = ShapeFactory.createShape("rectangle", "Blue", 4.0, 6.0);
Shape triangle = ShapeFactory.createShape("triangle", "Green", 3.0, 4.0);

Shape[] shapes = {circle, rectangle, triangle};

for (Shape shape : shapes) {
shape.draw();
System.out.println("Area: " + String.format("%.2f", shape.getArea()));
System.out.println();
}
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Loading