Hacks Week 2
Queues, Merged Queues, Enhanced For-Loops
- Queue - Generics + Enhanced For-Loops
- Hack 1: Queue Example - Output w/ Enqueue + Dequeue
- Hack 2: Merge Queues 1 and 2
- Java Stacks
Queue<Integer> queue = new LinkedList<>();
queue.add(0);
queue.add(10);
queue.add(20);
queue.add(30);
queue.add(40);
queue.add(50);
queue.add(60);
queue.add(70);
queue.add(80);
queue.add(90);
queue.add(100);
Integer[] index = queue.toArray(new Integer[queue.size()]);
int total = 0;
int len = index.length;
double average;
for (int grade : index)
total += grade;
average = (double) total / len;
System.out.println("The average of the scores in the class is: " + average + "%");
//for:each loop directly iterates through all elements automatically
//calculates test average by assigning to a double
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);
public static Queue<Integer> mergeQueues(Queue<Integer> one, Queue<Integer> two) {
Queue<Integer> merged = new LinkedList<>();
Queue<Integer> q1 = new LinkedList<>(one);
Queue<Integer> q2 = new LinkedList<>(two);
while (!q1.isEmpty() && !q2.isEmpty()) {//runs as long as both queues aren't empty
if (q1.peek() <= q2.peek()) {// peek function checks which should come first in merge list
merged.add(q1.poll());// if one element in q1 is more than q2, q2 number goes first to merge, element removed from q2, etc.
} else {//in this case q1 = first, removed element from q1
merged.add(q2.poll());//otherwise does so to q2
}//checks if q1 elements are more than or equal to, essentially, elements of q2
}
while (!q1.isEmpty()) { //based on remaining elements, adds them to merge list, etc.
merged.add(q1.poll());
}
while (!q2.isEmpty()) {
merged.add(q2.poll());
}
return merged; //returns final list
}
//tests code
Queue<Integer> q1 = new LinkedList<>(Arrays.asList(1, 4, 5, 8));
//"Arrays.asList" converts the integer arguments into a List, which is
//then passed to the LinkedList constructor to create the queue.
Queue<Integer> q2 = new LinkedList<>(Arrays.asList(2, 3, 6, 7));
Queue<Integer> merged = mergeQueues(q1, q2); //calls method w input
System.out.println("Queue 1: " + q1.toString());
System.out.println("Queue 2: "+ q2.toString());
System.out.println("Merged Queue: " + merged); // should print [1, 2, 3, 4, 5, 6, 7, 8]
public abstract class Generics {
public final String masterType = "Generic";
private String type; // extender should define their data type
// generic enumerated interface
public interface KeyTypes {
String name();
}
protected abstract KeyTypes getKey(); // this method helps force usage of KeyTypes
// getter
public String getMasterType() {
return masterType;
}
// getter
public String getType() {
return type;
}
// setter
public void setType(String type) {
this.type = type;
}
// this method is used to establish key order
public abstract String toString();
// static print method used by extended classes
public static void print(Generics[] objs) {
// print 'Object' properties
System.out.println(objs.getClass() + " " + objs.length);
// print 'Generics' properties
if (objs.length > 0) {
Generics obj = objs[0]; // Look at properties of 1st element
System.out.println(
obj.getMasterType() + ": " +
obj.getType() +
" listed by " +
obj.getKey());
}
// print "Generics: Objects'
for(Object o : objs) // observe that type is Opaque
System.out.println(o);
System.out.println();
}
}
public class Food extends Generics {
// Class data
public static KeyTypes key = KeyType.title; // static initializer
public static void setOrder(KeyTypes key) { Food.key = key; }
public enum KeyType implements KeyTypes {title, name, culture}
// Instance data
private final String name;
private final String culture;
/* constructor
*
*/
public Food(String name, String culture)
{
super.setType("Food");
this.name = name;
this.culture = culture;
}
/* 'Generics' requires getKey to help enforce KeyTypes usage */
@Override
protected KeyTypes getKey() { return Food.key; }
/* 'Generics' requires toString override
* toString provides data based off of Static Key setting
*/
@Override
public String toString() {
String output = "";
if (KeyType.name.equals(this.getKey())) {
output += this.name;
} else if (KeyType.culture.equals(this.getKey())) {
output += this.culture;
} else {
output += super.getType() + ": " + this.name + ", " + this.culture;
}
return output;
}
// Test data initializer
public static Food[] food() {
return new Food[]{
new Food("Paneer", "Indian"),
new Food("Chow Mein", "Chinese"),
new Food("Burgers", "American"),
};
}
/* main to test Animal class
*
*/
public static void main(String[] args)
{
// Inheritance Hierarchy
Food[] objs = food();
// print with title
// Food.setOrder(KeyType.title);
// Food.print(objs);
Food.setOrder(KeyType.name);
Food.print(objs);
// print name only
Food.setOrder(KeyType.culture);
Food.print(objs);
}
}
Food.main(null);