// Kevin Quinn, CSE 143 // Here are a few problems that we particularly enjoy... import java.util.*; public class MidtermReview { public static void main(String[] args) { testDashed(); System.out.println(); testByAge(); testSorted(); } // Write a recursive method called printDashed that takes an integer as a // parameter and that prints the integer with dashes in between the digits. // // printDashed(-834) -8-3-4 // printDashed(6) 6 // printDashed(-17) -1-7 // printDashed(-4) -4 // printDashed(983) 9-8-3 // printDashed(42) 4-2 // printDashed(0) 0 // printDashed(29348) 2-9-3-4-8 public static void printDashed(int value) { if (value < 0) { System.out.print("-"); printDashed(-value); } else if (value < 10) { System.out.print(value); } else { int lastDigit = value % 10; int remainder = value / 10; printDashed(remainder); System.out.print("-" + lastDigit); } } // return a new map with information about people with ages between the min // and max, inclusive. Each key is an integer age, and the value for that // key is a string with the names of all people at that age, separated by // "and" if there is more than one person of that age. Include only ages // between the min and max inclusive, where there is at least one person of // that age in the original map. If the map passed in is // empty, or if there are no people in the map between the min/max ages, // return an empty map. // // in: {Paul=28, David=20, Janette=18, Marty=35, Stuart=98, Jessica=35, // Helene=40, Allison=18, Sara=15, Grace=25, Zack=20, Galen=15, Erik=20, // Tyler=6, Benson=48} // // byAge(ages, 16, 25): {18=Janette and Allison, 20=David and Zack and Erik, // 25=Grace} public static Map byAge(Map ages, int min, int max) { Map ageMap = new HashMap(); for (String name : ages.keySet()) { int age = ages.get(name); if (age >= min && age <= max) { if (!ageMap.containsKey(age)) { // No one of that age yet ageMap.put(age, name); } else { ageMap.put(age, ageMap.get(age) + " and " + name); } } } return ageMap; } // Write a method isSorted that accepts a stack of integers as a parameter // and returns true if the elements in the stack // occur in ascending (non-decreasing) order from top to bottom, and false // otherwise. That is, the smallest element // should be on top, growing larger toward the bottom. // // For example, passing the following stack should return true: // bottom [20, 20, 17, 11, 8, 8, 3, 2] top // // The following stack is not sorted (the 15 is out of place), so passing it // to your method should return a result of false: // bottom [18, 12, 15, 6, 1] top // // An empty or one-element stack is considered to be sorted. When your method // returns, the stack should be in the same // state as when it was passed in. In other words, if your method modifies // the stack, you must restore it before returning. public static boolean isSorted(Stack s) { if (s.size() < 2) { return true; } boolean sorted = true; int prev = s.pop(); Stack backup = new Stack(); backup.push(prev); while (!s.isEmpty()) { int curr = s.pop(); backup.push(curr); if (prev > curr) { sorted = false; } prev = curr; } while (!backup.isEmpty()) { // restore s s.push(backup.pop()); } return sorted; } public static void testDashed() { printDashed(-834); // -8-3-4 System.out.println(); printDashed(6); // 6 System.out.println(); printDashed(-17); // -1-7 System.out.println(); printDashed(42); // 4-2 System.out.println(); printDashed(-4); // -4 System.out.println(); printDashed(983); // 9-8-3 System.out.println(); printDashed(0); // 0 System.out.println(); printDashed(29348); // 2-9-3-4-8 } public static void testByAge() { Map people = new HashMap(); people.put("Paul", 28); people.put("David", 20); people.put("Janette", 18); people.put("Marty", 35); people.put("Stuart", 98); people.put("Jessica", 35); people.put("Helene", 40); people.put("Allison", 18); people.put("Sara", 15); people.put("Grace", 25); people.put("Zack", 20); people.put("Galen", 15); people.put("Erik", 20); people.put("Tyler", 6); people.put("Benson", 48); Map result = byAge(people, 16, 25); System.out.println(result); } public static void testSorted() { Stack stack = new Stack(); stack.add(1); stack.add(2); stack.add(3); stack.add(2); stack.add(5); stack.add(8); stack.add(9); stack.add(7); System.out.println("Result = " + isSorted(stack)); System.out.println(stack); } }