Generic Types
creating queues and stuff
Armaan: 2.8/3
Class Details
Access modifier: It specifies the level of access to the class members from outside the class. The most common access modifiers are public, private, protected, and internal.
Constructor: It is a special method that is called when an object of the class is created. It initializes the object's state and sets the initial values of its properties.
Modifiers/setters: These are methods that allow the values of the class's properties to be changed after the object has been created. They are also known as setter methods.
Getters: These are methods that allow the values of the class's properties to be retrieved from the object. They are also known as accessor methods.
Instance variables: These are the variables that store the state of the object. They are declared inside the class and are accessed using the object of the class.
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
public class ListQueueStackExample {
public static void main(String[] args) {
// Create a linked list and add some elements to it
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("Apple");
linkedList.add("Banana");
linkedList.add("Cherry");
linkedList.add("Date");
// Print the linked list
System.out.println("Linked List: " + linkedList);
// Create a queue and add some elements to it
Queue<String> queue = new LinkedList<>();
queue.add("Apple");
queue.add("Banana");
queue.add("Cherry");
queue.add("Date");
// Print the queue
System.out.println("Queue: " + queue);
// Remove an element from the queue and print it
String removedElement = queue.remove();
System.out.println("Removed element from Queue: " + removedElement);
// Create another queue and add some elements to it
Queue<Integer> queue2 = new LinkedList<>();
queue2.add(1);
queue2.add(3);
queue2.add(5);
queue2.add(7);
// Merge the two queues into a new, ordered queue
Queue<Object> mergedQueue = new LinkedList<>();
while (!queue.isEmpty() && !queue2.isEmpty()) {
if (queue.peek().compareTo(String.valueOf(queue2.peek())) < 0) {
mergedQueue.add(queue.remove());
} else {
mergedQueue.add(queue2.remove());
}
}
if (!queue.isEmpty()) {
mergedQueue.addAll(queue);
}
if (!queue2.isEmpty()) {
mergedQueue.addAll(queue2);
}
// Print the merged queue
System.out.println("Merged Queue: " + mergedQueue);
// Reverse the second queue using a stack
Queue<Character> queue3 = new LinkedList<>();
queue3.add('A');
queue3.add('B');
queue3.add('C');
queue3.add('D');
Stack<Character> stack = new Stack<>();
while (!queue3.isEmpty()) {
stack.push(queue3.remove());
}
while (!stack.isEmpty()) {
queue3.add(stack.pop());
}
System.out.println("Reversed Queue: " + queue3);
// Implement a stack from the linked list
LinkedList<Integer> linkedList2 = new LinkedList<>();
linkedList2.add(1);
linkedList2.add(2);
linkedList2.add(3);
Stack<Integer> stack2 = new Stack<>();
stack2.addAll(linkedList2);
System.out.println("Stack from Linked List: " + stack2);
}
}
ListQueueStackExample.main(null);
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.Stack;
public class QuizExample {
public static void main(String[] args) {
// Create a linked list of questions and add some elements to it
LinkedList<String> questionList = new LinkedList<>();
questionList.add("What is the capital of France?");
questionList.add("Who wrote the novel 'To Kill a Mockingbird'?");
questionList.add("What is the smallest planet in our solar system?");
questionList.add("What is the highest mountain in the world?");
// Print the linked list of questions
System.out.println("Question List: " + questionList);
// Create a queue of student answers and add some elements to it
Queue<String> studentAnswers = new LinkedList<>();
studentAnswers.add("Paris");
studentAnswers.add("Harper Lee");
studentAnswers.add("Mercury");
studentAnswers.add("Mount Everest");
// Print the queue of student answers
System.out.println("Student Answers Queue: " + studentAnswers);
// Remove an answer from the queue and print it
String removedAnswer = studentAnswers.remove();
System.out.println("Removed Answer from Queue: " + removedAnswer);
// Create another queue of correct answers and add some elements to it
Queue<String> correctAnswers = new LinkedList<>();
correctAnswers.add("Paris");
correctAnswers.add("Harper Lee");
correctAnswers.add("Mercury");
correctAnswers.add("Mount Everest");
// Merge the two queues into a new, ordered queue
Queue<String> mergedQueue = new LinkedList<>();
while (!studentAnswers.isEmpty() && !correctAnswers.isEmpty()) {
if (studentAnswers.peek().compareTo(correctAnswers.peek()) < 0) {
mergedQueue.add(studentAnswers.remove());
} else {
mergedQueue.add(correctAnswers.remove());
}
}
if (!studentAnswers.isEmpty()) {
mergedQueue.addAll(studentAnswers);
}
if (!correctAnswers.isEmpty()) {
mergedQueue.addAll(correctAnswers);
}
// Print the merged queue
System.out.println("Merged Queue: " + mergedQueue);
// Shuffle the first queue of student answers
Collections.shuffle((LinkedList<String>) studentAnswers);
System.out.println("Shuffled Student Answers Queue: " + studentAnswers);
// Reverse the second queue of correct answers using a stack
Queue<Character> correctAnswersQueue = new LinkedList<>();
correctAnswersQueue.add('A');
correctAnswersQueue.add('B');
correctAnswersQueue.add('C');
correctAnswersQueue.add('D');
Stack<Character> correctAnswersStack = new Stack<>();
while (!correctAnswersQueue.isEmpty()) {
correctAnswersStack.push(correctAnswersQueue.remove());
}
while (!correctAnswersStack.isEmpty()) {
correctAnswersQueue.add(correctAnswersStack.pop());
}
System.out.println("Reversed Correct Answers Queue: " + correctAnswersQueue);
// Implement a stack of scores from the linked list of questions
LinkedList<Integer> scoresList = new LinkedList<>();
scoresList.add(1);
scoresList.add(2);
scoresList.add(3);
Stack<Integer> scoreStack = new Stack<>();
scoreStack.addAll(scoresList);
System.out.println("Score Stack from Linked List: " + scoreStack);
}
}
QuizExample.main(null);
public class Enhanced{
public static void main(String[] args){
int[][] numbers = new int[5][10];
int x = 0;
for (int[] rows: numbers){
for(int column: rows){
column = column + x;
x++;
System.out.print(column +" ");
}
System.out.println();
}
}
}
Enhanced.main(null);