Multithreading: A Fibonacci Conundrum Steven wrote a bit of code to try to calculate the first 10 Fibonacci numbers using multithreading so it will be super fast. Unfortunately he doesn’t really understand multithreading so it doesn’t work. Identify an output the code might give and why. Furthermore, fix the code so that it gives the correct output. public class Fibonacci{ private static Queue<Integer> q = new LinkedList<>(); Fibonacci(){ q.add(1); q.add(0); } public void getNext(){ int x1 = q.poll(); int x2 = q.poll(); q.add(x1 + x2); q.add(x1); } public void printNumber(){ System.out.println(q.peek()); } public static void main(String[] args) { Fibonacci m = new Fibonacci(); Thread temp = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 5; i++) { m.getNext(); m.printNumber(); } } }); Thread temp2 = new Thread(new Runnable() { @Override public void run() { for (int i = 0; i < 5; i++) { m.getNext(); m.printNumber(); } } }); temp.start(); temp2.start(); } } Recursion: Adding numbers in Poland A Polish scientist takes notes in only prefix notation, and his disciple needs to quickly evaluate their values. Given a prefix notation expression (Polish notation), calculate its value. A prefix notation describes an expression that is started by an operator and followed by 2 arguments. If the arguments also start with an operator then you have a sub expression to analyze. You only have to handle adding numbers. For example: “+2+34” → 2 + (3+4); notice how the first argument was 2 and the second was +34 which became 3+4 Your job is to help the disciple by making a prefix notation solver using recursion. Unfortunately his computer lacks memory so your solution will have to be less than 10 lines. Your input is a parsed expression in the form of a string array. All numbers are single digits Ex: “+2+55” → ["+", "2","+","5","5"] public int calculateExpression(String[] exp1){ } Socket Programming: Sending a message Sally has set up her server and client to connect to each other. Sending the messages between the server and the client are not working correctly. She needs help to implement the writeToServer() method in the Client class to send a message to the server. The Client has the field: private Socket socket; Which should be used to send a message object to the server. Message Class: public class Message { public String mes; public int id; private static int counter = 0; Message(String mes, int id){ this.mes = mes; this.id = id; } public void updateCounter(int count){ counter = count; } public void printString(){ System.out.println(mes +" " + id + " " +counter); } } 1. Implement the following method in the Client Class: public void writeToServer(Message message){ message.updateCounter(5); // finish the method } 2. If the server were to call the printString method on the message from the client, what would get printed? Assuming everything is correct. Reflection: SORTING SHENANIGANS class Sorter{ public int[] Sort(int arr[]){ int n = arr.length; for (int i = 0; i < n - 1; i++) for (int j = 0; j < n - i - 1; j++) if (arr[j] > arr[j + 1]) { swap(arr, j+1, j); } return arr; } public boolean isSorted(int[] arr){ for (int i = 1; i < arr.length; i++) if (arr[i] < arr[i-1]){ return false; } return true; } public void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } } class BogoSorter extends Sorter{ public int[] Sort(int[] arr){ do { shuffle(arr); } while(isSorted(arr)); return arr; } void shuffle(int[] a) { for (int i = 0; i < a.length; i++){ swap(a, i, (int)(Math.random() * i)); } } } You are given the above code. Which may or may not work as intended, but that doesn’t matter. The scientist who wrote that is using “print statement debugging” to figure out the output of his code. Analyze the following snippets and write a possible output. public static void main(String[] args) { Sorter s = new Sorter(); Sorter b = new BogoSorter(); System.out.println("Test1"); System.out.println(Arrays.toString(b.getClass().getDeclaredMethods())); System.out.println("Test2"); try { int temp[] = new int[]{5,7,3,2,1}; Sorter t = (Sorter) b.getClass().getDeclaredConstructors()[0].newInstance(); System.out.println(Arrays.toString(t.Sort(temp))); } catch (Exception e) { e.printStackTrace(); } System.out.println("Test3"); try { int temp[] = new int[]{5,7,3,2,1}; Sorter x = (Sorter) b.getClass().getDeclaredConstructors()[0].newInstance(); Sorter y = (Sorter) s.getClass().getDeclaredConstructors()[0].newInstance(); System.out.println(Arrays.toString(y.Sort(temp))); System.out.println(Arrays.toString(x.Sort(temp))); } catch (Exception e) { e.printStackTrace(); } System.out.println("Test4"); try { int temp[] = new int[]{5,7,3,2,1}; Sorter j = new Sorter(); Method h = b.getClass().getDeclaredConstructors()[0].newInstance().getClass().getDeclaredMethods()[0]; h.invoke(j, temp); } catch (Exception e) { e.printStackTrace(); } } Trees: Tree balancing Yea…there is no story to this one. Just write a method to determine if a binary tree is balanced. A tree is made of nodes and is balanced if the height of subtrees on the right and left do not differ by more than one. Implement the isBalanced(Node node) method. Assume the tree is initialized properly and the root is not null. Helpers are permitted. class Node{ public Node right; public Node left; public int data; Node(int data){ this.data = data; this.left = this.right = null; } } public class Tree { public Node root = null; public boolean isBalanced(Node node){ } }