CB Vid

  • An access modifier is a keyword that determines the accessibility of a class, method, or variable. In Java, there are four access modifiers: public, protected, private, and default. Public classes, methods, and variables can be accessed from anywhere, while private ones can only be accessed within the same class. Protected ones can be accessed within the same package or through inheritance, and default ones can be accessed within the same package.
  • A constructor is a special method that is used to initialize objects of a class. It has the same name as the class and doesn't have a return type. Constructors can have parameters or no parameters, and they are called automatically when an object is created using the "new" keyword.
  • Modifiers are methods that are used to modify the values of the variables of a class. They can be public or private, and they are used to encapsulate the class by hiding the internal details. Modifiers can also have validation checks to ensure that the values are within certain limits.
  • Getters are methods that are used to retrieve the values of the variables of a class. They are used to encapsulate the class by hiding the internal details, and they are usually public. Getters can also be used to return calculated values based on the variables of the class.
  • Variables are used to store data within a class. They can be of different data types, such as int, double, String, and so on. Variables can also have different access modifiers, such as public, private, protected, and default.
  • Methods are functions that are defined within a class. They can be used to perform operations on the variables of the class, and they can also be used to return values. Methods can have parameters or no parameters, and they can be public or private.

Hack Challenge #1

public class Queue<T> implements Iterable<T> {
    private LinkedList<T> head = null;
    private LinkedList<T> tail = null;
    private int wordCount = 0;

    public void add(T data) {
        // add new object to end of Queue
        LinkedList<T> tail = new LinkedList<>(data, null);

        if (this.head == null) { // initial condition
            this.head = this.tail = tail;
        } else { // nodes in queue
            this.tail.setNextNode(tail); // current tail points to new tail
            this.tail = tail; // update tail
        }

        wordCount++;
        System.out.printf("Enqueued data: %s\n", data);
        System.out.printf("Words count: %d, data: %s\n", wordCount, this.toString());
    }

    public T delete() {
        T data = this.peek();
        if (this.tail != null) { // initial condition
            this.head = this.head.getNext(); // current tail points to new tail
            if (this.head != null) {
                this.head.setPrevNode(null);
            }
        }

        wordCount--;
        System.out.printf("Dequeued data: %s\n", data);
        System.out.printf("Words count: %d, data: %s\n", wordCount, this.toString());
        return data;
    }
    
    public T peek() {
        return this.head.getData();
    }

    public LinkedList<T> getHead() {
        return this.head;
    }

    public LinkedList<T> getTail() {
        return this.tail;
    }

    public Iterator<T> iterator() {
        return new QueueIterator<>(this.head);
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        LinkedList<T> current = head;
        while (current != null) {
            sb.append(current.getData().toString());
            sb.append(" ");
            current = current.getNext();
        }
        return sb.toString().trim();
    }
}
/**
 * Driver Class
 * Tests queue with string, integers, and mixes of Classes and types
 */
class QueueTester {
    public static void main(String[] args)
    {
        // Create iterable Queue of Words
        Object[] words = new String[] { "seven", "slimy", "snakes", "sallying", "slowly", "slithered", "southward"};
        QueueManager qWords = new QueueManager("Words", words );
        // qWords.queue.add("Test");
        // qWords.queue.delete("seven");
        qWords.printQueue();

    }
}
QueueTester.main(null);
Enqueued data: seven
Words count: 1, data: seven
Enqueued data: slimy
Words count: 2, data: seven slimy
Enqueued data: snakes
Words count: 3, data: seven slimy snakes
Enqueued data: sallying
Words count: 4, data: seven slimy snakes sallying
Enqueued data: slowly
Words count: 5, data: seven slimy snakes sallying slowly
Enqueued data: slithered
Words count: 6, data: seven slimy snakes sallying slowly slithered
Enqueued data: southward
Words count: 7, data: seven slimy snakes sallying slowly slithered southward
Words count: 7
Words data: seven slimy snakes sallying slowly slithered southward