Download Java Lab 2: Understanding Strings in Java - Prof. Paul C. Grabow and more Lab Reports Software Engineering in PDF only on Docsity! CSI 3471 Name ______________________ Lab 2 1 of 8 9/11/2008 Lab 2: Strings, Strings, Strings Part 1 Overview Strings are such an integral part of programming, but unfortunately each programming language deals with them slightly differently. So, it is worth our while to spend some time understanding how Java represents and manipulates strings. Here we will consider the String class. Later on we will look at Class StringBuilder, used to create and manipulate modifiable strings (and a replacement for StringBuffer), as well as Class Character, Class Pattern, and Class Matcher. Unlike C++, a String object in Java cannot be modified, i.e., it is immutable. But immutable strings have one great advantage: the compiler can see that strings are shared. The designers of Java decided that the efficiency of sharing outweighs the inefficiency of string editing.1 Directions: This lab has two parts. • Part 1 is with pencil and paper, answering a series of questions that relate to code fragments. It has three sections: A, B, and C. When you are finished with Part 1, please tell the instructor and they will give you Part 2. • Part 2 uses pre-existing code to illustrate what the individual code fragments actually produce. You will use the results from Part 2 to correct the answers that you gave in Part 1. Then there are some additional questions to answer. 1 {{736 Horstmann,Cay S. 2008 /s55}} CSI 3471 Name ______________________ Lab 2 2 of 8 9/11/2008 Section A. After each code fragment, indicate what output it produces when executed. Fragments 1 through 5 assume the following declarations String s1 = new String( "Baylor" ); String s2 = new String( "Go Bears!"); String s3 = "Go Bears!"; String s4 = "Go Bears!"; String s5 = "Bears"; String s6 = "bears"; Fragment 1 System.out.println("s1: " + s1 ); System.out.println("s2: " + s2 ); System.out.println("s3: " + s3 ); System.out.println("s4: " + s4 ); System.out.println("s5: " + s5 ); System.out.println("s6: " + s6 ); System.out.println(); CSI 3471 Name ______________________ Lab 2 5 of 8 9/11/2008 Section B. As with the previous section, indicate what output is produced for each code fragment. For this section, assume the following declaration: String strings[] = { “started”, “starting”, “ended”, “ending” }; String str1 = "Mississippi"; String str2 = "miss"; Fragment 1 for( String aString : strings ) { if( aString.startsWith( “st” ) ) { System.out.println(aString + “ starts with ‘st’”); } } for( String aString : strings ) { if( aString.startsWith( “art”, 2 ) ) { System.out.println(aString + “ starts with ‘art’ at position 2”); } } CSI 3471 Name ______________________ Lab 2 6 of 8 9/11/2008 Fragment 2 // *************************************** if( str1.regionMatches( 0, str2, 0, 4)) { System.out.println( "true"); } else { System.out.println( "false"); } // *************************************** if( str1.regionMatches( true, 0, str2, 0, 4)) { System.out.println( "true"); } else { System.out.println( "false"); } Section C. List the output for each fragment, as you did in the previous sections. Here, assume the following declaration (which is repeated on the next two pages): // ********************************************** String letters = "abcdefghijklmabcdefghijklm"; Fragment 1 System.out.println ("'c' is located at index" + letters.indexOf('c')); System.out.println ("'a' is located at index" + letters.indexOf('a', 1)); System.out.println ("'$' is located at index" + letters.indexOf('$')); CSI 3471 Name ______________________ Lab 2 7 of 8 9/11/2008 // ********************************************** String letters = "abcdefghijklmabcdefghijklm"; Fragment 2 // ********************************************** System.out.println("Last 'c' is located at index" + letters.lastIndexOf('c')); System.out.println("Last 'a' is located at index" + letters.lastIndexOf('a', 25)); System.out.println("Last '$' is located at index" + letters.lastIndexOf('$')); Fragment 3 // ********************************************** System.out.println ("\"def\" is located at index" + letters.indexOf("def")); System.out.println ("\"def\" is located at index" + letters.indexOf("def", 7)); System.out.println ("\"hello\" is located at index" + letters.indexOf("hello")); Fragment 4 // ********************************************** System.out.println ("Last \"def\" is located at index" + letters.lastIndexOf("def")); System.out.println ("Last \"def\" is located at index" + letters.lastIndexOf("def", 25)); System.out.println ("Last \"hello\" is located at index" + letters.lastIndexOf("hello"));