///////////////////////////////////////////// // TASK 1 SOLUTION ///////////////////////////////////////////// public class Task1 { public static void main(String[] args) { String s1 = "ABC"; String s2 = new String("ABC"); char[] c = {'A','B','C'}; String s3 = new String(c); } } ///////////////////////////////////////////// // TASK 2 SOLUTION ///////////////////////////////////////////// /********************************** * Exercise 11: TA Solution * Date: 04/20/2004 * * Gabe Heafitz: gjh2, 999999 **********************************/ /* * Task 2: Create a program Task2.java that converts the * array of characters {'h','e','l','l','o'} to * to an uppercase String with two techniques: * (a) using the String API * (b) only using character arithmetic (review MATLAB notes...) * Output your results for both approaches. */ public class Task2 { /** * Convert a string to all upper case using the String API. */ public String convertMethodA(String input){ return input.toUpperCase(); } /** * Convert a string to all upper case using character arithmetic. */ public String convertMethodB(String input){ char[] result = new char[input.length()]; for (int i = 0; i < input.length(); i++){ result[i] = (char)((int)(input.toCharArray())[i] + ('A' - 'a')); } return new String(result); } /** * Main method. We're using this to call our conversion methods. */ public static void main(String[] args){ String convertMe = "hello"; // This is the string we'll convert. Task2 whoNeedsStatic = new Task2(); System.out.println("This is the original string to convert: " + convertMe + "."); System.out.println("This is the string, converted using method A: " + whoNeedsStatic.convertMethodA(convertMe) + "."); System.out.println("This is the string, converted using method B: " + whoNeedsStatic.convertMethodB(convertMe) + "."); } } ///////////////////////////////////////////// // TASK 3 SOLUTION ///////////////////////////////////////////// public class Task3{ public static void main(String args[]) { String str = randomString(10); System.out.println(); System.out.println("The random string is: " + str); int count = countNonAlphabet(str); System.out.println("It has " + count + " non-alphabetic characters"); String newStr = extractAlphabet(str); System.out.println("The new string constructed by its alphabetic characters is: " + newStr); } public static String randomString(int len) { char s[] = new char[len]; for (int i = 0; i < len; i++) { s[i] = (char)myRandom(0, 127); } return new String(s); } public static int countNonAlphabet(String str) { if (str == null) return 0; int count = 0; for(int i = 0; i < str.length(); i++) { if (!Character.isLetter(str.charAt(i))) count++; } return count; } public static String extractAlphabet(String str) { if (str == null) return null; String newStr = ""; for(int i = 0; i < str.length(); i++) { if (Character.isLetter(str.charAt(i))) newStr += str.charAt(i); } return newStr; } public static int myRandom(int low, int high) { return (int) (Math.random()*(high-low+1)) + (int) low; } }