Upload files to "OOP/Java/Lab/Week12"

This commit is contained in:
Aadit Agrawal 2024-10-26 02:14:18 +05:30
parent 2ae5d58f75
commit 100e80ccc3
5 changed files with 226 additions and 0 deletions

View File

@ -0,0 +1,24 @@
public class DiffTypeGeneric {
public static <T> 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);
}
}

View File

@ -0,0 +1,66 @@
public class SinglyLinkedListDemo {
public static void main(String[] args) {
// Test with Integer
SinglyLinkedList<Integer> intList = new SinglyLinkedList<>();
intList.add(10);
intList.add(20);
intList.add(30);
System.out.println("Integer List:");
intList.printList();
// Test with Double
SinglyLinkedList<Double> doubleList = new SinglyLinkedList<>();
doubleList.add(1.1);
doubleList.add(2.2);
doubleList.add(3.3);
System.out.println("\nDouble List:");
doubleList.printList();
}
}
class SinglyLinkedList<T> {
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");
}
}

View File

@ -0,0 +1,82 @@
// Generic Stack class
class Stack<T> {
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<Student> 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<Employee> 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());
}
}
}

View File

@ -0,0 +1,30 @@
public class LargestOfThree {
public static <T extends Comparable<T>> 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)
);
}
}

View File

@ -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);
}
}