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.

Linked Lists and Queues

General Example

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);
Linked List: [Apple, Banana, Cherry, Date]
Queue: [Apple, Banana, Cherry, Date]
Removed element from Queue: Apple
Merged Queue: [1, 3, 5, 7, Banana, Cherry, Date]
Reversed Queue: [D, C, B, A]
Stack from Linked List: [1, 2, 3]

In Project Example

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);
Question List: [What is the capital of France?, Who wrote the novel 'To Kill a Mockingbird'?, What is the smallest planet in our solar system?, What is the highest mountain in the world?]
Student Answers Queue: [Paris, Harper Lee, Mercury, Mount Everest]
Removed Answer from Queue: Paris
Merged Queue: [Harper Lee, Mercury, Mount Everest, Paris, Harper Lee, Mercury, Mount Everest]
Shuffled Student Answers Queue: []
Reversed Correct Answers Queue: [D, C, B, A]
Score Stack from Linked List: [1, 2, 3]

For Each Loops

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);
0 1 2 3 4 5 6 7 8 9 
10 11 12 13 14 15 16 17 18 19 
20 21 22 23 24 25 26 27 28 29 
30 31 32 33 34 35 36 37 38 39 
40 41 42 43 44 45 46 47 48 49