└── Drill01.java /Drill01.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.HashMap; 3 | import java.util.HashSet; 4 | import java.util.List; 5 | import java.util.Map; 6 | import java.util.Set; 7 | 8 | public class Drill01 { 9 | 10 | /* -------- Arrays -------- */ 11 | 12 | /* 13 | * Return the item at the index specified by the 14 | * parameter 'index'. Only do this if 'index' exists 15 | * in your array. i.e. first check to make sure the 16 | * array is big enough to contain such an index. If 17 | * the array does not contain this index, return -1. 18 | */ 19 | static int getElementAtIndex(int index, int[] array) { 20 | // TODO: Implement the getElementAtIndex method 21 | if (index doubled = new ArrayList(); 50 | for(int i =0; i < array.length; i++) { 51 | doubled.add(2*array[i]); 52 | } 53 | int[] newArray = new int[doubled.size()]; 54 | for(int i =0; i list) { 72 | // TODO: Implement the getElementAtIndex method 73 | if(index < list.size()) { 74 | for(int i = 0; i < list.size(); i++) { 75 | if (list.get(index) == list.get(i)) { 76 | return list.get(i); 77 | } 78 | } 79 | } 80 | return -1; 81 | } 82 | 83 | /* 84 | * Return the sum of all the values in the given list. 85 | */ 86 | static int sumValues(List list) { 87 | // TODO: Implement the sumValues method 88 | int sum = 0; 89 | for(int i = 0; i < list.size(); i++) { 90 | sum = sum+list.get(i); 91 | } 92 | return sum; 93 | } 94 | 95 | /* 96 | * Return a new list that doubles every element in the 97 | * given list. 98 | */ 99 | static List doubleElements(List list) { 100 | // TODO: Implement the doubleElements method 101 | ArrayList doubled = new ArrayList(); 102 | for(int i =0; i < list.size(); i++) { 103 | doubled.add(2*list.get(i)); 104 | } 105 | return doubled; 106 | } 107 | 108 | /* -------- Sets -------- */ 109 | 110 | /* 111 | * Return whether the given set contains the given value. 112 | */ 113 | static boolean setContains(Set set, int val) { 114 | // TODO: Implement the setContains method 115 | for(int value: set) { 116 | if (val == value) { 117 | return true; 118 | } 119 | } 120 | return false; 121 | } 122 | 123 | /* 124 | * Return a new set containing the intersection 125 | * (common elements) of the given sets. 126 | */ 127 | static Set setIntersection(Set set1, Set set2) { 128 | // TODO: Implement the setIntersection method 129 | HashSet newHashSet = new HashSet(set1); 130 | newHashSet.retainAll(set2); 131 | return newHashSet; 132 | } 133 | 134 | /* -------- HashMaps -------- */ 135 | 136 | /* 137 | * Given a string, return a map with each character as 138 | * a key and the number of times that character appears 139 | * in the string as the value. 140 | */ 141 | static Map characterCount(String s) { 142 | // TODO: Implement the characterCount method 143 | 144 | HashMap Map = new HashMap(); 145 | for(int i = 0; i < s.length(); i++) { 146 | char key = s.charAt(i); 147 | if(Map.get(key)==null) { 148 | Map.put(s.charAt(i),1); 149 | }else 150 | Map.put(s.charAt(i),Map.get(key)+1); 151 | } 152 | return Map; 153 | } 154 | 155 | /* 156 | * Given a mapping of cities to their population, return the city 157 | * with the largest population. If the given map is empty, return 158 | * an empty string. 159 | */ 160 | static String largestPopulation(Map cities) { 161 | // TODO: Implement the largestPopulation method 162 | int maxPopulation = 0; 163 | String city = ""; 164 | for(String i : cities.keySet()) { 165 | if(i==null) { 166 | return ""; 167 | }else if( cities.get(i) > maxPopulation) { 168 | 169 | maxPopulation = cities.get(i); 170 | city = i; 171 | } 172 | } 173 | return city; 174 | } 175 | } 176 | 177 | 178 | 179 | --------------------------------------------------------------------------------