// steve eilertsen // String ex 3 number 3 package string3_3; public class String3_3_UI { public static void main(String[] args) { String firstLast = null; String backwards = null; String frontBack = null; String middleChar = null; String isVowel = null; String spaces = null; System.out.println("Hello UI class"); String3_3_Manager sm = new String3_3_Manager(); // Each called manager method returns a String to the UI class for printing // to the monitor. firstLast = sm.isolateFirstLast(); System.out.println(firstLast); backwards = sm.backwards(); System.out.println(backwards); frontBack = sm.frontBack(); System.out.println(frontBack); middleChar = sm.middleCharacter(); System.out.println("The middle characters are: " + middleChar); isVowel = sm.vowelsOnly(); System.out.println(isVowel); spaces = sm.numberSpaces(); System.out.println(spaces); } } ====================================================== // steve eilertsen // Ex 3 3 String handling method exercise // Featurs some defensive coding ie the work must be longer than zero // characters. /* SAMPLE OUTPUT ============= Hello UI class Hello String Manager Word: abcde The first letter is: a The last letter is: e The word backwards is: edcba Letter from the front, letter from the back: aebdc The middle characters are: c The vowels are: ae The number of spaces is: 0 */ package string3_3; import javax.swing.JOptionPane; public class String3_3_Manager { //global varialbles String word = null; int theLength = 0; // Constructor method // 3.1 public String3_3_Manager() { System.out.println("Hello String Manager"); // 3.1 word = JOptionPane.showInputDialog(null, "Enter your word or phrase"); if(word.length() == 0) { System.out.println("Word must be longer than zero characters"); System.exit(0); } System.out.println("Word: " + word); } // end constructor // All regular methods return a String back to the UI class for // printing to the monitor // 3.2 public String isolateFirstLast() { char first = word.charAt(0); theLength = word.length(); char last = word.charAt(theLength - 1); return "The first letter is: " + first + "\n" + "The last letter is: " + last; } // end isolateFirstLast // 3.3 public String backwards() { String temp = ""; int thePosition = theLength; for(int x = 0; x < theLength; x++) { temp = temp + word.charAt(thePosition - 1); thePosition = thePosition - 1; } return "The word backwards is: " + temp; } // end backwards // 3.4 public String frontBack() { // local variables the must be outside the loop // default temp String String temp = ""; char middleCharacter; // if the word is only one letter this method cannot run if(word.length() == 1) { System.out.println("Word is too short for this method"); } else { // integer division throws away the decimal fraction and gives // a whole number answer (it truncates the answer int halfLoop = theLength/2; int counterF = 0; int counterL = word.length() - 1; for(int x = 0; x < halfLoop; x++) { char letterF = word.charAt(counterF); // letter first char letterL = word.charAt(counterL); // letter last // code the exception first if((counterL - counterF) == 2) { // harvest from front, from the back temp = temp + letterF + letterL; // harvest the middle character counterF = counterF + 1; middleCharacter = word.charAt(counterF); temp = temp + middleCharacter; } else // now code the default { // harvest a letter from front and from the back only temp = temp + letterF + letterL; counterF = counterF + 1; counterL = counterL - 1; } } // end loop } return "Letter from the front, letter from the back: " + temp; } // end front back method // 3.5 public String middleCharacter() { int theMiddleInt = theLength / 2; int theMiddleMOD = theLength % 2; String middleChar = ""; if(theMiddleMOD > 0) middleChar = word.substring(theMiddleInt, theMiddleInt + 1); else middleChar = word.substring(theMiddleInt -1, theMiddleInt + 1); return middleChar; } // 3.6 public String vowelsOnly() { boolean isVowel = false; String temp = ""; for(int x = 0;x < word.length();x++) { char a = word.charAt(x); // pass letter to the check_for_vowel method isVowel = checkVowels(a); // if true, build the temp String if(isVowel) temp = temp + word.charAt(x); } // end loop return "The vowels are: " + temp; } // end vowelsOnly // This private helper method reduces redundant code for 3.6 which // reduces possible typo or logic errors. private boolean checkVowels(char a) { // test for presence of vowel boolean flag = false; // map the parameter to the local variable char checkForVowel = a; // parse the char to a String so that we can use the equals method // because char does not have such a method String checkForVowelSt = "" + checkForVowel; if(checkForVowelSt.equalsIgnoreCase("a") || checkForVowelSt.equalsIgnoreCase("e") || checkForVowelSt.equalsIgnoreCase("i") || checkForVowelSt.equalsIgnoreCase("o") || checkForVowelSt.equalsIgnoreCase("u")) { flag = true; } return flag; } // end checkVowels // 3.7 public String numberSpaces() { int spaces = 0; if(!word.contains(" ")) // does NOT contain spaces spaces = 0; String temp = word; // make a duplicate copy of the word // Reduce temp removing spaces one at a time. // Count until there are no more spaces while(temp.contains(" ")) { int position = temp.indexOf(" "); temp = temp.substring(position + 1); spaces = spaces + 1; } return "The number of spaces is: " + spaces; } // end numberSpaces } // end class =======================================================================