Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

. Write all of this code in main( ) in a test class in JAVA. a) Write a main( )

ID: 3892068 • Letter: #

Question

.  Write all of this code in main( ) in a test class in JAVA.

a)     Write a main( ) method that declares a String reference variable.

b)    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.

c)     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.

a.     Print the number of characters your sentence contains.

b.     Print the first letter of your sentence

c.     Print the last letter of your sentence

d.     Print whether your sentence contains the letter ‘e’

e.     Print whether your sentence contains “ay” or not.

f.      Print the number of times the letter ‘e’ appears in your sentence

g.     Find the position of the last occurrence of the letter ‘e’ in your sentence

h.     Find the position of the second occurrence of the letter ‘e in your sentence

i.      Print how many characters your sentence contains besides the space character.

j.      Add the words “you know” to the sentence

k.     Print a completely upper case version of your sentence

l.      Extract and print a substring of five characters from your sentence

m.   Print a String where all the chars ‘a’ are replaced with the char ‘x’

Explanation / Answer

StringObjectTester.java

import java.util.Scanner;

public class StringObjectTester {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter a sentence:");

String s = scan.nextLine();

System.out.println("Number of characters: "+s.length());

System.out.println("First letter: "+s.charAt(0));

System.out.println("Last letter: "+s.charAt(s.length()-1));

System.out.println("Contains 'e': "+s.contains("e"));

System.out.println("Contains 'ay': "+s.contains("ay"));

int eCount = 0, eLastPosition = -1,eSecondIndex = -1,spaceCount = 0;

for(int i=0;i<s.length();i++) {

if(s.charAt(i)=='e') {

eCount++;

if(eCount==2){

eSecondIndex=i;

}

eLastPosition=i;

}

if(s.charAt(i)==' ') {

spaceCount++;

}

}

System.out.println("'e' Count: "+eCount);

System.out.println("'e' Last index: "+eLastPosition);

System.out.println("'e' second index: "+eSecondIndex);

System.out.println("sentence contains besides the space character: "+(s.length()-spaceCount));

s =s.concat("you know");

System.out.println("Upper case: "+s.toUpperCase());

System.out.println("substring of five characters from your sentence: "+s.substring(0,5));

s=s.replace('a', 'e');

System.out.println("String where all the chars ‘a’ are replaced with the char ‘x’: "+s);

System.out.println();

}

}

Output:

Enter a sentence:
Hi everyone how are you
Number of characters: 23
First letter: H
Last letter: u
Contains 'e': true
Contains 'ay': false
'e' Count: 4
'e' Last index: 18
'e' second index: 5
sentence contains besides the space character: 19
Upper case: HI EVERYONE HOW ARE YOUYOU KNOW
substring of five characters from your sentence: Hi ev
String where all the chars ‘a’ are replaced with the char ‘x’: Hi everyone how ere youyou know