Checkpoint 2 Code + Notes
Notes To Look Back On
Access Modifiers: Access modifiers determine the level of access that other classes or code have to the variables and methods within a class. There are four access modifiers in Java:
Public: variables or methods marked as public can be accessed from any class or package. Private: variables or methods marked as private can only be accessed within the same class. Protected: variables or methods marked as protected can only be accessed within the same class or a subclass. Default: variables or methods without an explicit access modifier can only be accessed within the same package. Constructor: A constructor is a special method that is called when an object of a class is created. It initializes the instance variables of the class and sets up the object. The constructor has the same name as the class and doesn't have a return type. A class can have multiple constructors with different parameters.
Modifiers/Setters: Modifiers or setters are methods that allow us to modify the values of instance variables. They provide a way to change the state of an object after it has been created. Modifiers or setters are typically named with the prefix "set" followed by the name of the variable they modify.
Getters: Getters are methods that allow us to access the values of instance variables from outside the class. They provide a way to retrieve the state of an object. Getters are typically named with the prefix "get" followed by the name of the variable they retrieve.
// Example Code for Stacks etc\
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class LinkedListQueueStack {
public static void main(String[] args) {
// Creating a linked list
LinkedList<String> linkedList = new LinkedList<>();
// Adding elements to the linked list
linkedList.add("Ritvik");
linkedList.add("Aryan");
linkedList.add("Pranav");
linkedList.add("Jay");
// Printing the linked list
System.out.println("Linked List:");
System.out.println(linkedList);
// Creating a queue using linked list
Queue<String> queue = new LinkedList<>();
// Adding elements to the queue
queue.add("Ritvik");
queue.add("Aryan");
queue.add("Pranav");
queue.add("Jay");
// Printing the queue
System.out.println("Queue:");
System.out.println(queue);
// Removing elements from the queue
System.out.println("Removing an element from the queue: " + queue.remove());
System.out.println("Queue after removing an element:");
System.out.println(queue);
// Creating a stack using linked list
Stack<String> stack = new Stack<>();
// Adding elements to the stack
stack.push("Ritvik");
stack.push("Aryan");
stack.push("Pranav");
stack.push("Jay");
// Printing the stack
System.out.println("Stack:");
System.out.println(stack);
// Removing elements from the stack
System.out.println("Popping an element from the stack: " + stack.pop());
System.out.println("Stack after popping an element:");
System.out.println(stack);
}
}
LinkedListQueueStack.main(null);
//Hack 1
public class QueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<String>();
// Adding elements to the queue
queue.add("seven");
System.out.println("Enqueued data: " + "seven");
printQueue(queue);
queue.add("slimy");
System.out.println("Enqueued data: " + "slimy");
printQueue(queue);
queue.add("snakes");
System.out.println("Enqueued data: " + "snakes");
printQueue(queue);
queue.add("sallying");
System.out.println("Enqueued data: " + "sallying");
printQueue(queue);
queue.add("slowly");
System.out.println("Enqueued data: " + "slowly");
printQueue(queue);
queue.add("slithered");
System.out.println("Enqueued data: " + "slithered");
printQueue(queue);
queue.add("southward");
System.out.println("Enqueued data: " + "southward");
printQueue(queue);
// Removing elements from the queue
String data =queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
data = queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
data = queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
data = queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
data = queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
data = queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
data = queue.remove();
System.out.println("Dequeued data: " + data);
printQueue(queue);
}
// Helper method to print the contents of the queue
public static void printQueue(Queue<String> queue) {
System.out.println("Words count: " + queue.size() + ", data: " + String.join(" ", queue));
System.out.println();
}
}
QueueExample.main(null);
//Hack 2
import java.util.*;
public class Merge {
public static Queue<Integer> merge(Queue<Integer> q1, Queue<Integer> q2) {
Queue<Integer> result = new LinkedList<>();
while (!q1.isEmpty() && !q2.isEmpty()) {
if (q1.peek() < q2.peek()) {
result.add(q1.poll());
} else {
result.add(q2.poll());
}
}
// Add any remaining elements from q1 or q2
result.addAll(q1);
result.addAll(q2);
return result;
}
public static void main(String[] args) {
Queue<Integer> q1 = new LinkedList<>(Arrays.asList(1, 3, 5, 7));
Queue<Integer> q2 = new LinkedList<>(Arrays.asList(2, 4, 6, 8));
Queue<Integer> merged = merge(q1, q2);
System.out.println(merged); // Output: [1, 2, 3, 4, 5, 6, 7, 8]
}
}
Merge.main(null);
//Hack 3
public static class Queue{
public static void main(String[] args){
private static class Node{
private int data;
private Node next;
private Node(int data){
this.data = data;
}
}
private Node current;
while (node.current!= null){
node.current = Math.random() *10;
}
}
}
//Hack 4
//Shows output in normal form, then in reverse form (stack hack)
public class reverse{
public static void main(String[] args){
Queue <Integer> queue = new ArrayDeque<>();
queue.add(10);
queue.add(20);
queue.add(30);
System.out.println(queue);
Stack<Integer> stack = new Stack<>();
while(!queue.isEmpty()){
stack.push(queue.remove());
}
while(!stack.isEmpty()){
queue.add(stack.pop());
}
System.out.print(queue);
}
}
reverse.main(null);