└── Drill02.java /Drill02.java: -------------------------------------------------------------------------------- 1 | import java.util.LinkedList; 2 | import java.util.Queue; 3 | import java.util.Stack; 4 | 5 | public class Drill02 { 6 | 7 | // -------- Stacks/Queues 8 | 9 | // Given a stack as input, return a stack that has the 10 | // same values in reverse order. 11 | static Stack reverseStack(Stack in) { 12 | Stack result = new Stack(); 13 | while (!in.empty()) { 14 | result.push(in.pop()); 15 | } 16 | return result; 17 | } 18 | 19 | // Given a queue, return a new queue that has the 20 | // same values in reverse order 21 | static Queue reverseQueue(Queue in) { 22 | Queue result = new LinkedList<>(); 23 | Stack temp = new Stack(); 24 | while (!in.isEmpty()) { 25 | temp.push(in.remove()); 26 | } 27 | while (!temp.empty()) { 28 | result.add(temp.pop()); 29 | } 30 | return result; 31 | } 32 | 33 | 34 | // -------- Recursion 35 | 36 | // takes a number of characters to print as a parameter 37 | // returns a string with one or two asterisks in the middle 38 | // surrounded by alligators 39 | // See the README 40 | public static String zigzag(int n) { 41 | if (n == 1) { 42 | return "*"; 43 | } 44 | else if (n == 2) { 45 | return "**"; 46 | } 47 | else { 48 | return "<" + zigzag(n - 2) + ">"; 49 | } 50 | } 51 | 52 | // takes a string and a character as parameters and returns 53 | // the string with all copies of the character moved to the 54 | // end and capitalized 55 | // See the README 56 | public static String moveToEnd(String s, char c) { 57 | if (s.length() == 0) { 58 | return ""; 59 | } 60 | else { 61 | String temp = s.substring(1); 62 | if (s.toLowerCase().charAt(0) == Character.toLowerCase(c)) { 63 | return moveToEnd(temp, c) + Character.toUpperCase(s.charAt(0)); 64 | } 65 | else { 66 | return s.charAt(0) + moveToEnd(temp, c); 67 | } 68 | } 69 | } 70 | 71 | } 72 | --------------------------------------------------------------------------------