1) non_generic
public class NonGenericMethod {
public static void main(String[] args) {
print("Hello");
print(100);
print(99.99);
}
public static void print(Object obj) {
System.out.println(obj);
}
}
2)generic class with one type parameter
class GenericClass<T> {
private T value;
public GenericClass(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public static void main(String[] args) {
GenericClass<String> stringObj = new GenericClass<>("Hello");
System.out.println(stringObj.getValue());
GenericClass<Integer> intObj = new GenericClass<>(100);
System.out.println(intObj.getValue());
}
}
3)generic class with two type parameter
class Pair<K, V> {
private K key;
private V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public static void main(String[] args) {
Pair<String, Integer> pair = new Pair<>("Age", 25);
System.out.println("Key: " + pair.getKey() + ", Value: " +
pair.getValue());
}
}
4)generic class with bounded type
class BoundedType<T extends Number> {
private T value;
public BoundedType(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
public static void main(String[] args) {
BoundedType<Integer> intObj = new BoundedType<>(100);
System.out.println(intObj.getValue());
BoundedType<Double> doubleObj = new BoundedType<>(99.99);
System.out.println(doubleObj.getValue());
}
}
5)wildcard demonstration
import java.util.Arrays;
import java.util.List;
public class WildcardsExample {
public static void printList(List<?> list) {
for (Object elem : list) {
System.out.println(elem);
}
}
public static void main(String[] args) {
List<Integer> intList = Arrays.asList(1, 2, 3, 4, 5);
List<String> strList = Arrays.asList("A", "B", "C");
printList(intList);
printList(strList);
}
}
6)inheritance in java
class Animal {
void eat() {
System.out.println("This animal eats food.");
}
}
class Dog extends Animal {
void bark() {
System.out.println("The dog barks.");
}
}
public class InheritanceExample {
public static void main(String[] args) {
Dog dog = new Dog();
dog.eat();
dog.bark();
}
}
7)Display Unique Words Greater Than Length 3 from File in Ascending Order:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class UniqueWordsFromFile {
public static void main(String[] args) {
Set<String> uniqueWords = new TreeSet<>();
try (Scanner scanner = new Scanner(new File("input.txt"))) {
while (scanner.hasNext()) {
String word = scanner.next();
if (word.length() > 3) {
uniqueWords.add(word);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
for (String word : uniqueWords) {
System.out.println(word);
}
}
}
8)linked list operations
import java.util.LinkedList;
public class LinkedListFunctions {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
// Adding elements
list.add(10);
list.add(20);
list.add(30);
// Displaying elements
System.out.println("LinkedList: " + list);
// Removing element
list.remove(1);
System.out.println("After removal: " + list);
// Getting element
int element = list.get(1);
System.out.println("Element at index 1: " + element);
// Iterating through elements
for (Integer i : list) {
System.out.println(i);
}
}
}
9)comparator usage in treeset
import java.util.Comparator;
import java.util.TreeSet;
public class TreeSetComparatorExample {
public static void main(String[] args) {
TreeSet<String> treeSet = new TreeSet<>(new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s2.compareTo(s1);
}
});
treeSet.add("Apple");
treeSet.add("Banana");
treeSet.add("Cherry");
for (String fruit : treeSet) {
System.out.println(fruit);
}
}
}
10)stack operations using arraylist
import java.util.ArrayList;
public class StackUsingArrayList {
private ArrayList<Integer> stack;
public StackUsingArrayList() {
stack = new ArrayList<>();
}
public void push(int value) {
stack.add(value);
}
public int pop() {
if (!stack.isEmpty()) {
return stack.remove(stack.size() - 1);
}
return -1; // Stack is empty
}
public int peek() {
if (!stack.isEmpty()) {
return stack.get(stack.size() - 1);
}
return -1; // Stack is empty
}
public static void main(String[] args) {
StackUsingArrayList stack = new StackUsingArrayList();
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println("Top element: " + stack.peek());
System.out.println("Popped element: " + stack.pop());
System.out.println("Top element after pop: " + stack.peek());
}
}
11)generate n binary numbers using queue
import java.util.LinkedList;
import java.util.Queue;
public class BinaryNumbersUsingQueue {
public static void generateBinaryNumbers(int n) {
Queue<String> queue = new LinkedList<>();
queue.add("1");
for (int i = 0; i < n; i++) {
String current = queue.poll();
System.out.println(current);
queue.add(current + "0");
queue.add(current + "1");
}
}
public static void main(String[] args) {
int n = 10; // Number of binary numbers to generate
generateBinaryNumbers(n);
}
}
12)display numbers in ascending order using priority queue
import java.util.PriorityQueue;
public class PriorityQueueExample {
public static void main(String[] args) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.add(20);
pq.add(10);
pq.add(30);
while (!pq.isEmpty()) {
System.out.println(pq.poll());
}
}
}
13)deque operations
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeOperations {
public static void main(String[] args) {
Deque<Integer> deque = new ArrayDeque<>();
// Adding elements
deque.addFirst(10);
deque.addLast(20);
deque.addFirst(5);
// Displaying elements
System.out.println("Deque: " + deque);
// Removing elements
System.out.println("Removed from first: " + deque.removeFirst());
System.out.println("Removed from last: " + deque.removeLast());
// Displaying remaining elements
System.out.println("Deque after removals: " + deque);
}
}
14)hashset operations
import java.util.HashSet;
public class HashSetExample {
public static void main(String[] args) {
HashSet<Integer> set = new HashSet<>();
// Adding elements
set.add(10);
set.add(20);
set.add(30);
// Displaying elements
System.out.println("HashSet: " + set);
// Removing element
set.remove(20);
System.out.println("After removal: " + set);
// Checking if element exists
System.out.println("Contains 10: " + set.contains(10));
}
}
15)implementing a linkedlist
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
public class LinkedList {
Node head;
public void add(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
}
public void display() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add(10);
list.add(20);
list.add(30);
list.display();
}
}
16)implementing stack uisng array
public class StackUsingArray {
private int maxSize;
private int[] stackArray;
private int top;
public StackUsingArray(int size) {
this.maxSize = size;
this.stackArray = new int[maxSize];
this.top = -1;
}
public void push(int value) {
if (top < maxSize - 1) {
stackArray[++top] = value;
} else {
System.out.println("Stack is full");
}
}
public int pop() {
if (top >= 0) {
return stackArray[top--];
} else {
System.out.println("Stack is empty");
return -1;
}
}
public int peek() {
if (top >= 0) {
return stackArray[top];
} else {
System.out.println("Stack is empty");
return -1;
}
}
public static void main(String[] args) {
StackUsingArray stack = new StackUsingArray(5);
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println("Top element: " + stack.peek());
System.out.println("Popped element: " + stack.pop());
System.out.println("Top element after pop: " + stack.peek());
}
}
17)implementing stack using linkedlist
class StackNode {
int data;
StackNode next;
StackNode(int data) {
this.data = data;
this.next = null;
}
}
public class StackUsingLinkedList {
StackNode top;
public void push(int data) {
StackNode newNode = new StackNode(data);
if (top == null) {
top = newNode;
} else {
newNode.next = top;
top = newNode;
}
}
public int pop() {
if (top == null) {
System.out.println("Stack is empty");
return -1;
} else {
int value = top.data;
top = top.next;
return value;
}
}
public int peek() {
if (top == null) {
System.out.println("Stack is empty");
return -1;
} else {
return top.data;
}
}
public static void main(String[] args) {
StackUsingLinkedList stack = new StackUsingLinkedList();
stack.push(10);
stack.push(20);
stack.push(30);
System.out.println("Top element: " + stack.peek());
System.out.println("Popped element: " + stack.pop());
System.out.println("Top element after pop: " + stack.peek());
}
}
18)implementing queue using array
public class QueueUsingArray {
private int maxSize;
private int[] queueArray;
private int front;
private int rear;
private int nItems;
public QueueUsingArray(int size) {
this.maxSize = size;
this.queueArray = new int[maxSize];
this.front = 0;
this.rear = -1;
this.nItems = 0;
}
public void insert(int value) {
if (rear == maxSize - 1) {
rear = -1;
}
queueArray[++rear] = value;
nItems++;
}
public int remove() {
int temp = queueArray[front++];
if (front == maxSize) {
front = 0;
}
nItems--;
return temp;
}
public int peekFront() {
return queueArray[front];
}
public static void main(String[] args) {
QueueUsingArray queue = new QueueUsingArray(5);
queue.insert(10);
queue.insert(20);
queue.insert(30);
System.out.println("Front element: " + queue.peekFront());
System.out.println("Removed element: " + queue.remove());
System.out.println("Front element after removal: " + queue.peekFront());
}
}
19)implementing queue using linkedlist
class QueueNode {
int data;
QueueNode next;
QueueNode(int data) {
this.data = data;
this.next = null;
}
}
public class QueueUsingLinkedList {
private QueueNode front, rear;
public void insert(int data) {
QueueNode newNode = new QueueNode(data);
if (rear == null) {
front = rear = newNode;
} else {
rear.next = newNode;
rear = newNode;
}
}
public int remove() {
if (front == null) {
System.out.println("Queue is empty");
return -1;
} else {
int value = front.data;
front = front.next;
if (front == null) {
rear = null;
}
return value;
}
}
public int peekFront() {
if (front == null) {
System.out.println("Queue is empty");
return -1;
} else {
return front.data;
}
}
public static void main(String[] args) {
QueueUsingLinkedList queue = new QueueUsingLinkedList();
queue.insert(10);
queue.insert(20);
queue.insert(30);
System.out.println("Front element: " + queue.peekFront());
System.out.println("Removed element: " + queue.remove());
System.out.println("Front element after removal: " + queue.peekFront());
}
}
20)implementing sorted chain
class SortedNode {
int data;
SortedNode next;
SortedNode(int data) {
this.data = data;
this.next = null;
}
}
public class SortedChain {
private SortedNode head;
public void insert(int data) {
SortedNode newNode = new SortedNode(data);
if (head == null || head.data >= newNode.data) {
newNode.next = head;
head = newNode;
} else {
SortedNode current = head;
while (current.next != null && current.next.data < newNode.data) {
current = current.next;
}
newNode.next = current.next;
current.next = newNode;
}
}
public void display() {
SortedNode current = head;
while (current != null) {
System.out.print(current.data + " ");
current = current.next;
}
}
public static void main(String[] args) {
SortedChain sortedChain = new SortedChain();
sortedChain.insert(30);
sortedChain.insert(10);
sortedChain.insert(20);
sortedChain.display();
}
}
21)linear probing operations
class HashTable {
private int[] table;
private int size;
public HashTable(int size) {
this.size = size;
table = new int[size];
for (int i = 0; i < size; i++) {
table[i] = -1; // -1 indicates empty
}
}
public void insert(int value) {
int hash = value % size;
while (table[hash] != -1) {
hash = (hash + 1) % size;
}
table[hash] = value;
}
public boolean search(int value) {
int hash = value % size;
while (table[hash] != -1) {
if (table[hash] == value) {
return true;
}
hash = (hash + 1) % size;
}
return false;
}
public void display() {
for (int i = 0; i < size; i++) {
if (table[i] != -1) {
System.out.print(table[i] + " ");
} else {
System.out.print("- ");
}
}
}
public static void main(String[] args) {
HashTable hashTable = new HashTable(10);
hashTable.insert(10);
hashTable.insert(20);
hashTable.insert(30);
hashTable.display();
System.out.println("\nSearch 20: " + hashTable.search(20));
System.out.println("Search 40: " + hashTable.search(40));
}
}
22)maxheap using priority queue
import java.util.Collections;
import java.util.PriorityQueue;
public class MaxHeapExample {
public static void main(String[] args) {
PriorityQueue<Integer> maxHeap = new
PriorityQueue<>(Collections.reverseOrder());
maxHeap.add(10);
maxHeap.add(20);
maxHeap.add(30);
while (!maxHeap.isEmpty()) {
System.out.println(maxHeap.poll());
}
}
}