diff --git a/OOP/Java/Lab/Week12/DiffTypeGeneric.java b/OOP/Java/Lab/Week12/DiffTypeGeneric.java new file mode 100644 index 0000000..f060f3f --- /dev/null +++ b/OOP/Java/Lab/Week12/DiffTypeGeneric.java @@ -0,0 +1,24 @@ +public class DiffTypeGeneric { + + public static void printArray(T[] array) { + for (T element : array) { + System.out.print(element + " "); + } + System.out.println(); + } + + public static void main(String[] args) { + Integer[] intArray = { 1, 2, 3, 4, 5 }; + Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4, 5.5 }; + String[] stringArray = { "Hello", "World", "Generic", "Method" }; + + System.out.println("Integer Array:"); + printArray(intArray); + + System.out.println("Double Array:"); + printArray(doubleArray); + + System.out.println("String Array:"); + printArray(stringArray); + } +} diff --git a/OOP/Java/Lab/Week12/GenList.java b/OOP/Java/Lab/Week12/GenList.java new file mode 100644 index 0000000..7148734 --- /dev/null +++ b/OOP/Java/Lab/Week12/GenList.java @@ -0,0 +1,66 @@ +public class SinglyLinkedListDemo { + + public static void main(String[] args) { + // Test with Integer + SinglyLinkedList intList = new SinglyLinkedList<>(); + intList.add(10); + intList.add(20); + intList.add(30); + + System.out.println("Integer List:"); + intList.printList(); + + // Test with Double + SinglyLinkedList doubleList = new SinglyLinkedList<>(); + doubleList.add(1.1); + doubleList.add(2.2); + doubleList.add(3.3); + + System.out.println("\nDouble List:"); + doubleList.printList(); + } +} + +class SinglyLinkedList { + + private class Node { + + T data; + Node next; + + Node(T data) { + this.data = data; + this.next = null; + } + } + + private Node head; + + public SinglyLinkedList() { + head = null; + } + + public void add(T data) { + Node newNode = new Node(data); + + if (head == null) { + head = newNode; + return; + } + + Node current = head; + while (current.next != null) { + current = current.next; + } + current.next = newNode; + } + + public void printList() { + Node current = head; + while (current != null) { + System.out.print(current.data + " -> "); + current = current.next; + } + System.out.println("null"); + } +} diff --git a/OOP/Java/Lab/Week12/GenStack.java b/OOP/Java/Lab/Week12/GenStack.java new file mode 100644 index 0000000..83c2819 --- /dev/null +++ b/OOP/Java/Lab/Week12/GenStack.java @@ -0,0 +1,82 @@ +// Generic Stack class +class Stack { + + private Object[] elements; + private int top; + private static final int DEFAULT_SIZE = 10; + + public Stack() { + elements = new Object[DEFAULT_SIZE]; + top = -1; + } + + public void push(T item) { + elements[++top] = item; + } + + public T pop() { + if (isEmpty()) return null; + return (T) elements[top--]; + } + + public boolean isEmpty() { + return top == -1; + } +} + +// Student class +class Student { + + private String name; + private int id; + + public Student(String name, int id) { + this.name = name; + this.id = id; + } + + public String toString() { + return "Student [name=" + name + ", id=" + id + "]"; + } +} + +// Employee class +class Employee { + + private String name; + private String department; + + public Employee(String name, String department) { + this.name = name; + this.department = department; + } + + public String toString() { + return "Employee [name=" + name + ", department=" + department + "]"; + } +} + +public class Main { + + public static void main(String[] args) { + // Stack of Students + Stack studentStack = new Stack<>(); + studentStack.push(new Student("John", 101)); + studentStack.push(new Student("Mary", 102)); + + System.out.println("Students popped from stack:"); + while (!studentStack.isEmpty()) { + System.out.println(studentStack.pop()); + } + + // Stack of Employees + Stack employeeStack = new Stack<>(); + employeeStack.push(new Employee("Bob", "IT")); + employeeStack.push(new Employee("Alice", "HR")); + + System.out.println("\nEmployees popped from stack:"); + while (!employeeStack.isEmpty()) { + System.out.println(employeeStack.pop()); + } + } +} diff --git a/OOP/Java/Lab/Week12/LargestOfThree.java b/OOP/Java/Lab/Week12/LargestOfThree.java new file mode 100644 index 0000000..8c850ee --- /dev/null +++ b/OOP/Java/Lab/Week12/LargestOfThree.java @@ -0,0 +1,30 @@ +public class LargestOfThree { + + public static > T findLargest(T a, T b, T c) { + T max = a; + + if (b.compareTo(max) > 0) { + max = b; + } + if (c.compareTo(max) > 0) { + max = c; + } + return max; + } + + public static void main(String[] args) { + Integer a = 10; + Integer b = 5; + Integer c = 15; + + System.out.println("The largest number is: " + findLargest(a, b, c)); + + String str1 = "apple"; + String str2 = "orange"; + String str3 = "banana"; + + System.out.println( + "The largest string is: " + findLargest(str1, str2, str3) + ); + } +} diff --git a/OOP/Java/Lab/Week12/WildCard.java b/OOP/Java/Lab/Week12/WildCard.java new file mode 100644 index 0000000..67dc55f --- /dev/null +++ b/OOP/Java/Lab/Week12/WildCard.java @@ -0,0 +1,24 @@ +class Wildcard { + + static void printArray(Object[] elements) { + for (Object element : elements) { + System.out.print(element + " "); + } + System.out.println(); + } + + public static void main(String[] args) { + Integer[] intArray = { 1, 2, 3, 4, 5 }; + String[] stringArray = { "Hello", "World" }; + Double[] doubleArray = { 1.1, 2.2, 3.3 }; + + System.out.println("Integer Array:"); + printArray(intArray); + + System.out.println("String Array:"); + printArray(stringArray); + + System.out.println("Double Array:"); + printArray(doubleArray); + } +}