Unit 5 (Review)

Review: Less Common Programming concepts

Stacks: A Stack uses the Last In First Out (LIFO) principle. Think of it as a stack of papers. The first one you pick up would be the last one you laid down. There are three basic operations you should know for stacks (push, pop, peek).

Push: This adds an element to the front (or the top) of the stack.

Peek: Returns the frontmost (or topmost) element in the stack. It does not affect the stack.

Pop: This removes the topmost element of the stack and returns it.

Note: Pop() throws an exception if empty.

Example in Processing: (We will go over in class)

import java.util.Stack;

public void setup()

{

Stack<String> stackOfCards = new Stack<String>();

// Pushing new items to the Stack

stackOfCards.push("Jack");

stackOfCards.push("Queen");

stackOfCards.push("King");

stackOfCards.push("Ace");

println("Stack => " + stackOfCards);

println();

// Popping items from the Stack

String cardAtTop = stackOfCards.pop(); // Throws EmptyStackException if

the stack is empty

println("Stack.pop() => " + cardAtTop);

println("Current Stack => " + stackOfCards);

println();

// Get the item at the top of the stack without removing it

cardAtTop = stackOfCards.peek();

println("Stack.peek() => " + cardAtTop);

println("Current Stack => " + stackOfCards);

}

Queue: Queues use a First in First out (FIFO) principle. Think of a call queue. Values are added to the end of the list but taken out from the beginning of the list. The next person/value to get taken out of the queue would be the first person that went into the queue.

Push (or add in some languages): adds the element to the end of the queue.

Peek: returns the element at the beginning of the queue

Pop (or remove in some languages): removes the element at the beginning of the queue and returns it.

Example in Processing (we will go over in class):

import java.util.LinkedList;

import java.util.Queue;

public void setup()

{

Queue<Integer> q = new LinkedList<Integer>();

q.add(6);

q.add(1);

q.add(8);

q.add(4);

q.add(7);

println("The queue is: " + q);

int num1 = q.remove();

println("The element deleted from the head is: " + num1);

println("The queue after deletion is: " + q);

int head = q.peek();

println("The head of the queue is: " + head);

int size = q.size();

println("The size of the queue is: " + size);

}

Unit 5 Problem Set:

Problem 1)

import java.util.Stack;

public void setup()

{

Stack<String> names = new Stack<String>();

names.push("Joe");

names.push("Craig");

names.push("Alex");

names.push("Ron");

names.pop();

String name = names.pop();

println(name);

name = names.peek();

println(name);

names.push(“Ray”);

println(names);

}

What will get printed for this code segment?

Problem 2:

import java.util.LinkedList;

import java.util.Queue;

public void setup()

{

Queue<Integer> q = new LinkedList<Integer>();

q.add(3);

q.add(2);

q.add(9);

q.add(40);

q.add(17);

println(q);

int value = q.remove();

println(value);

int peekValue = q.peek();

println(peekValue);

println(q);

}

What will be printed?

Passed by Value vs. Passed by Reference:

Examples in processing:

Example 1: (passed by value)

int c = 7;

int d = c;

c++;

println(d);

Example 2: (passed by reference)

int[] a = {5,3,4};

int[] b = a;

a[1]++;

println(b[1]);

Example 3:

public void change(int x, Rectangle r)

{

x = x%2;

println(x);

r.setLength(7);

println(r.getLength());

}

public void setup()

{

int number = 17;

Rectangle rect = new Rectangle(5,5);

change(number, rect);

println(number);

println(rect);

}

Note: Whether a variable is passed by value or reference varies among different languages.

_______________________________________________________________________________________________________________________________

TO PREPARE FOR THE GACE:

The Internet

Layers of the Internet

Cybersecurity and Global Impacts

Vocabulary List: Use this list of vocabulary to familiarize/refamiliarize yourself with some common CS terminology.

Assistments: GACE Review (100 coding questions)

Review pseudocode notation:

From Test at a Glance: