Question & Answer: Purpose: To read and use the Java API documentation for the String class and the StringBuilder class……

ITSC 1213

Lab 3

Purpose: To read and use the Java API documentation for the String class and the StringBuilder class.

Step 1Step 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

 

Step1:

public class StringExample {
public static void main(String[] args) {

String state = new String(“Mississippi”);

String captitalCity = new String(“Jackson”);

String university = new String(“Old Miss”);

System.out.println(“first letter of the state :”+state.charAt(0));
System.out.println(“state contains the substring “pi” “+state.indexOf(“pi”, 0));

String newstate=state.substring(0, 4);
System.out.println(newstate);

String capitalState=captitalCity+”, “+state;
System.out.println(capitalState);

int capitalLength=captitalCity.length();
int stateLength=state.length();
if(stateLength > capitalLength)
System.out.println(“state has more characters.”);
else System.out.println(“capital has more characters.”);

String stateChange=state.replace(‘i’,’o’);
System.out.println(stateChange);

String newUniversity=university.toUpperCase();
System.out.println(newUniversity);

if(captitalCity.endsWith(“son”))
System.out.println(“capital city ends with son.”);

for(int i=0;i<state.length();i++){
char ch=state.charAt(i);
if(ch==’s’)
System.out.println(“position of s’s : “+(i+1));
}
/*
* String: String is a final class. String is immutable(once we create an object,
* we cannot change the data(content)of the string. If try to change it creates new String
* object. String object can creates in “” or by using new keyword. String object created by
* “” are created in String pool.
* String Builder: It is mutable class. We can change object and
* we can append,insert and delete.
*
*
*    5 methods of string builder class:
*
*    1. public StringBuilder append(String s)
*    2.public StringBuilder insert(int offset,String s)
*    3. public StringBuilder replace(int startindex,int endindex,String s)
*    4.public StringBuilder reverse()
*    5. public int capacity().
*/
}
}

Step2:

public class Test {

public static void main(String[] args) {

java.util.Scanner scnr=new java.util.Scanner(System.in);
System.out.println(“enter string:”);
String str=scnr.nextLine();
System.out.println(“length of string : “+str.length());

System.out.println(“first letter of the sentence “+str.charAt(0));
System.out.println(“last letter of the sentence “+str.charAt(str.length()-1));

boolean e_contain=str.contains(“e”);
System.out.println(“e contain in string “+e_contain);

boolean ay_contain=str.contains(“ay”);
System.out.println(“ay contain in string “+ay_contain);

int e_length=str.length()-str.replace(“e”, “”).length();
System.out.println(“e length in string “+e_length);

System.out.println(“last occurence of e “+str.lastIndexOf(‘e’));

int e_firstOccurence=str.indexOf(‘e’);
System.out.println(” second occurence of e in string “+str.indexOf(‘e’,e_firstOccurence+1));

int spaces_count= str.length()-str.replace(” “,””).length();
System.out.println(“characters your sentence contains besides the space character “+ spaces_count);

System.out.println(str+” you know”);
System.out.println(“uppercase version “+str.toUpperCase());

System.out.println(“print a substring of five characters from your sentence “+str.substring(0, 5));

String atoxChange=str.replace(‘a’,’x’);
System.out.println(” Print a String where all the chars ‘a’ are replaced with the char ‘x’ “+ atoxChange);
}

}

step3:-

class Replacer{

String original;
StringBuilder stored;
public Replacer(String original) {
super();
this.original = original;
this.stored = new StringBuilder(original);
}
public String getOriginal() {
return original;
}
public void setOriginal(String original) {
this.original = original;
}
public StringBuilder getStored() {
return stored;
}

public String replace(String str,String s){

String afterReplace=original.replace(str, s);
return afterReplace;
}

}

public class Driver {
public static void main(String[] args) {

Replacer replacer=new Replacer(“Jackson”);
System.out.println(replacer.original);
System.out.println(replacer.stored);

replacer.setOriginal(“jack”);
String aftrset=replacer.getOriginal();
System.out.println(replacer.replace(“j”,”J”));

}
}

Still stressed from student homework?
Get quality assistance from academic writers!