Java Lab 2: Understanding Strings in Java - Prof. Paul C. Grabow, Lab Reports of Software Engineering

A lab exercise from a java programming course focusing on strings in java. It covers the basics of string manipulation using the string class, comparing strings using different operators, and locating specific characters in strings. Students are expected to write code fragments and observe the output to answer a series of questions.

Typology: Lab Reports

Pre 2010

Uploaded on 08/18/2009

koofers-user-uvs
koofers-user-uvs 🇺🇸

8 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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}}
pf3
pf4
pf5
pf8

Partial preview of the text

Download Java Lab 2: Understanding Strings in Java - Prof. Paul C. Grabow and more Lab Reports Software Engineering in PDF only on Docsity!

Lab 2

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}}

Lab 2

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();

Lab 2

Fragment 3

System. out .print("Based on 'compareTo' operator, "); System. out .println("s5.compareTo(s6): " + s5.compareTo(s6));

Fragment 4

System. out .print("Based on 'compareTo' operator, "); System. out .println("s6.compareTo(s5): " + s6.compareTo(s5));

Fragment 5

System. out .print("Based on 'compareToIgnoreCase' operator, "); System. out .println("s5.compareToIgnoreCase(s6): " + s5.compareToIgnoreCase(s6));

Lab 2

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”); } }

Lab 2

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"));

Lab 2

String letters = "abcdefghijklmabcdefghijklm";

Fragment 5

System. out .println("Substr from index 20 to end: "

  • letters.substring(20)); System. out .println("Substr from index 3 up to, but excluding 6: "
  • letters.substring(3, 6));