/** * Created by karou on 15/09/2015. */ public class JavaExercises1To5 { /** * "Global" variable used for error checking. * Always check if the value is false after an input * operation, otherwise the result can be incorrect! */ public static boolean IOError = false; public static void main(String[] args) { ex1(new int[]{1,2,3}); ex2(new int[] {1,2,2,4,5}); ex3(new int[] {1,2,2,4,5,2,2,4,7,8}); ex4(new int[]{1, 0, 1, 0, 0, 1, 1}); ex5(new String[]{"Lorena", "Javier", "Monica"}); } /** * Write a function such that given an int array of any length, * returns a new array of its first 2 elements. If the array is smaller * than length 2, use whatever elements are present. Test your function * on arrays of different size. * * Example: * * Suppose the function is called "frontPiece", then * * >frontPiece({1, 2, 3}) returns {1, 2} * >frontPiece({1, 2}) returns {1, 2} * >frontPiece({1}) returns {1} */ public static int[] ex1(int[] array) { if (array.length > 1) { return new int[]{array[0], array[1]}; } else if (array.length == 1) { return new int[]{array[0]}; } else { return new int[]{}; } } /** * Given an int array of any length, write a function that returns true * if the array contains number 2 twice, and returns false otherwise. * Test your subroutine on arrays of different size. */ public static boolean ex2(int[] array) { boolean containsOnce = false; boolean containsTwice = false; for (int i = 0; i < array.length; i++) { if (containsTwice) break; if (array[i] == 2) { if (containsOnce) { containsTwice = true; } else { containsOnce = true; } } } return containsTwice; } /** * Given an array of ints, return true if the sum of all the 2's in the * array is exactly 8, false otherwise. Write a function to implement * this check. */ public static boolean ex3(int[] array) { int sum = 0; for (int i = 0; i < array.length; i++) { if (array[i] == 2) { sum += 2; } } return sum == 8; } /** * Write a function such that given an array of ints returns an array * that contains the exact same numbers as the given array, but rearranged * so that all the even numbers come before all the odd numbers. * Other than that, the numbers can be in any order. * * Example: * Suppose the subroutine is called "evenOdd", then * * >evenOdd({1, 0, 1, 0, 0, 1, 1}) returns {0, 0, 0, 1, 1, 1, 1} * >evenOdd({3, 3, 2}) returns {2, 3, 3} * >evenOdd({2, 2, 2}) returns {2, 2, 2} */ public static int[] ex4(int[] array) { int result[] = new int[array.length]; for (int i = 0, j = 0, k = array.length - 1; i < array.length; i++) { if (array[i] % 2 == 0) { result[j] = array[i]; j++; } else { result[k] = array[i]; k--; } } return result; } /** * Write a function such that given an array of student names * (i.e. Strings) prints out the list of name that start with the * letter "M". Test your subroutine by writing a program that inputs * a list of names provided by the user and prints out the names * starting with letter "M". * * Example: * * >startWithM({"Musard", "John", "Marco"}) -> The following names * start with "M": Musard, Marco * >startWithM({"Lorena", "Javier"}) -> The array contains no names * starting with "M" * >startWithM({"Lorena", "Javier", "Monica"}) -> There is only one name * starting with "M": Monica */ public static void ex5(String[] names) { String result = ""; String separator = ", "; int count = 0; for (int i = 0; i < names.length; i++) { String name = names[i]; if (name.charAt(0) == 'M') { result += name + separator; count++; } } // remove last separator if (count > 0) result = result.substring(0, result.length() - 2); if (count == 0) { println("The array contains no names starting with \"M\""); } else if (count == 1) { println("There is only one name starting with \"M\": " + result); } else { println("The following names start with \"M\": " + result); } } /** * Generic println. * @param o object to print */ public static void println(Object o) { System.out.println(o); IOError = false; } }