ITSC 1213 Lab 3 Purpose: To read and use the Java API documentation for the String class and the StringBuilder class. Step 1. Step 1 is done on paper, not with BlueJ. Open the Java 8 API documentation and answer the following questions about the String class methods. String state = new String (“Mississippi”); String captitalCity = new String (“Jackson”); String university = new String(“Old Miss”); Using the above String object references, look at the Java API documentation and state which method(s) you would use to accomplish the following tasks: get the first letter of state. find out if state contains the substring “pi” create a new String that contains only the “Miss” from the String state. create a new String that has as its data: “Jackson, Mississippi” find out which has more characters in it: state or capitalCity create a new String with all the ‘i’s in “Mississippi changed to ‘o’s. create a new String from university, that holds the data: “OLD MISS” determine if capitalCity ends with the letters: “sen” or “son” determine if state and university contain the same letters, regardless of case. Find the positions of all the letter ‘s’s in the String state. This will take more than one Java statement. What is the difference in use/purpose between the String class and the StringBuilder class? List five methods of the StringBuilder class. Step 2. Write all of this code in main( ) in a test class. Write a main( ) method that declares a String reference variable. Read a sentence (with spaces) using keyboard input. Create a String object using this input. Input a sentence that is long enough and has enough letters to accomplish each of the following tasks. Write the String method calls and print the output for each of these tasks. Note that some of these cannot be written with one line of code and one String class method call. Print the number of characters your sentence contains. Print the first letter of your sentence Print the last letter of your sentence Print whether your sentence contains the letter ‘e’ Print whether your sentence contains “ay” or not. Print the number of times the letter ‘e’ appears in your sentence Find the position of the last occurrence of the letter ‘e’ in your sentence Find the position of the second occurrence of the letter ‘e in your sentence Print how many characters your sentence contains besides the space character. Add the words “you know” to the sentence Print a completely upper case version of your sentence Extract and print a substring of five characters from your sentence Print a String where all the chars ‘a’ are replaced with the char ‘x’ Step 3: Write a project to perform manipulations on a string of characters. Write a program that will allow the user to change every occurrence of the space character with the string, “blank”. Create two classes. Replacer class, which has at least two fields, the original string, stored in a String object and the converted string, stored in a StringBuilder object. The constructor method should take a String as a parameter and assign it to both fields. You need a replace( ) method that will take in two parameters, the character to be replaced and the sequence of characters to replace it with. This method will use StringBuilder methods and logic to change all occurrences of the original string to a new string, while still preserving a copy of the original string. Write a get( ) method to get the original string. Write a get( ) method to get the converted string. Write a set( ) method to change the original string to a new string provided by the user. Driver class: Write a class with the main( ) method that tests your Replacer class. The user can enter strings, and convert each to another string using the replace( ) method that you wrote. Use the get( ) methods to print results to the screen. Allow the user to enter a brand new string until they decide they are done.
Expert Answer
getting the first letter of state: char c = state.charAt(0);
if state contains the substring “pi”:
boolean b = state.contains(“pi”);
create a new String that contains only the “Miss” from the String state:
state = state.substring(0,4);
create a new String that has as its data: “Jackson, Mississippi”:
String s = new String(“Jackson, Mississippi”);
find out which has more characters in it: state or capitalCity
int l1 = state.length(); int l2 = capitalCity.length();
(l1>l2)?System.out.print(“state has more characters”):System.out.print(“capitalCity has more characters”);
create a new String with all the ‘i’s in “Mississippi” changed to ‘o’s
String replaceString=state;
replaceString.replace(“i”,”o”);
create a new String from university, that holds the data: “OLD MISS”
String myUniversity = university; // both myUniversity and university point to the same string
determine if capitalCity ends with the letters: “sen” or “son”
if(capitalCity.substring(capitalCity.length()-3).equals(“sen”) || capitalCity.substring(capitalCity.length()-3).equals(“son”))
if state and university contain the same letters, regardless of case:
Find the positions of all the letter ‘s’s in the String state
//replace with double quotes (state.indexOf(“s”))
What is the difference in use/purpose between the String class and the StringBuilder class?
String objects are immutable ie you cannot manipulate string objects. When you use such methods, you make the same referrence variable point to a different string in the same pool.
Advantage: Sharing can be arranged.
StringBuilder objects are useful when you want to create strings from smaller strings such as keystrokes.You can change StringBuilder objects to String objects.
StringBuilder append(String s);
StringBuilder insert(int offset, String s);
StringBuilder replace(int startIndex, int endIndex, String str);
StringBuilder delete(int startIndex, int endIndex);
StringBuilder reverse();
STEP 2
import java.util.*;
class Derived {
public static void main( String[] args )
{
String sentence;
Scanner sc = new Scanner(System.in);
sentence = sc.next();
System.out.println(“no. of characters “+ sentence.length());
System.out.println(“first letter “+ sentence.charAt(0));
System.out.println(“last letter “+ sentence.charAt(sentence.length()-1));
if(sentence.contains(“e”))
System.out.println(“sentence contains e”);
else
System.out.println(“sentence does not contain e”);
int ecount = 0;
for(char c :sentence.toCharArray())
{
if(c==’e’)
ecount++;
}
System.out.println(“no. of e’s”+ecount);
System.out.println(“last index of e”+sentence.lastIndexOf(‘e’));
int index = sentence.indexOf(‘e’);
int secondIndex = sentence.indexOf(‘e’,index);
System.out.println(“second index of i “+secondIndex);
int charCount=0;
for(char c :sentence.toCharArray())
{
if(c!=’ ‘)
ecount++;
}
System.out.println(“characters apart from space”+charCount);
sentence+=”you know”;
System.out.println(“uppercase version: “+sentence.toUpperCase());
System.out.println(sentence.replace(‘a’, ‘x’);
}
}