Need a graphical program (console, GUI, AWT, Swing, or Applet) that consists of
ID: 3661199 • Letter: N
Question
Need a graphical program (console, GUI, AWT, Swing, or Applet) that consists of three classes. The first class will be the actual program. The second class(inner classes) will simply convert a string to lower case. The third class(inner classes) will have three methods: public static String trimmed(String str) and public static String trimmed(String str, int len) and public static String squeeze(String str) The 1st trimmed method will return str without leading or trailing whitespace and will return an empty string if str is null. The 2nd trimmed method will return the first len characters after trimming the string. The 2nd method must make use of the 1st method. The squeeze method will replace all sequences of 2 spaces with 1 space. The program will read a file containing the data below (cut and paste it into notepad and save it), and will display each line: 1. as entered but trimmed and squeezed. 2. as entered but trimmed, squeezed, and shortened to 10 characters 3. as entered but trimmed, squeezed, converted to lower case, and shortened to 20 characters.Explanation / Answer
import java.io.*; public class A3BE2300780 { BufferedReader in = getReader ("input.txt"); private static class LowerCase { public static String convertToLowerCase(String input) { if (input==null) return ""; return input.toLowerCase(); } } public static class ThirdStringManip{ //This method will trim the white space from the //beginning and end of the string public static String trimmed(String s){ if (s == null){ return ""; } return s.trim(); } //This method will return a trimmed string of size len public static String trimmed(String s, int len){ String retVal = ThirdStringManip.trimmed(s); if (len > retVal.length()){ len = retVal.length(); } return retVal.substring(0,len); } //This method will convert all double spaces to a single space public static String squeeze(String s){ return s.replace(" ", " "); } } //This method will read strings from the input file //and perform manipulations on each string. The results of the //manipulations are displayed in the text area private void displayManipulatedStrings() throws Exception{ while(loop) { //Get the next line in the file String curString = s.nextLine(); //Trim and Squeeze System.out.print ( "Trim & Squeeze: " + ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)) + " "); //Trim, Squeeze, and Shorten to 10 characters System.out.print ( "Trim, Squeeze, Shorten to 10: " + ThirdStringManip.trimmed(ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)),10) + " "); //Trim, Squeeze, lower-case and shorten to 20 characters System.out.print ( "Trim, Squeeze, Shorten to 20, lower-case: " + toLower(ThirdStringManip.trimmed(ThirdStringManip.squeeze(ThirdStringManip.trimmed(curString)),20)) + " "); System.out.print ( " "); } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.