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

Step 2. Write all of this code in main( ) in a test class. Write a main( ) metho

ID: 3884212 • Letter: S

Question

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.

Explanation / Answer


import java.util.Scanner;

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author Surya
*/
public class string {
  
String s;
  
string(String n)
{
this.s = n;
}
  
// Print the number of characters your sentence contains
void numberofchars(String n)
{
System.out.println("The number of characters are :"+n.length());
  
}
//Print the first letter of your sentence
void print_firstchar(String n)
{

System.out.println("First char of the sentence :"+n.charAt(0));
  
  
}
//Print the last letter of your sentence
void print_lastchar(String n)
{

System.out.println("Last char of the sentence :"+n.charAt(n.length()-1));
  
  
}
//Print whether your sentence contains the letter ‘e’
void contain_e(String n)
{
int i,m =n.length();
for(i=0;i<m;i++)
{
if(n.charAt(i)=='e' || n.charAt(i)=='E')
{
System.out.println("Yes| it contain e");
return;
}
  
}
System.out.println("No| it doesn't contain e");
}
//Print whether your sentence contains “ay”
void contain_ay(String n)
{
int i,m =n.length();
for(i=0;i<m-1;i++)
{
if(n.charAt(i)=='a' || n.charAt(i)=='A')
{
if(n.charAt(i+1)=='y' || n.charAt(i+1)=='Y')
{
System.out.println("Yes| it contain ay");
return;
}
}
  
}
System.out.println("No| it doesn't contain ay");
}
  
//Print the number of times the letter ‘e’ appears in your sentence
void numberof_e(String n)
{
int i,m =n.length();
int s=0;
for(i=0;i<m;i++)
{
if(n.charAt(i)=='e' || n.charAt(i)=='E')
{
s++;
}
  
}
System.out.println("e appears :"+s+ " times");
}
  
  
//ind the position of the last occurrence of the letter ‘e’ in your sentence
void position_of_lastoccurenceof_e(String n)
{
int i,m =n.length(),k=0;
for(i=0;i<m;i++)
{
if(n.charAt(i)=='e' || n.charAt(i)=='E')
{
k=i;
}
  
}
System.out.println("Position of last occurence of e "+k);
}
  
  
//Find the position of the second occurrence of the letter ‘e in your sentence
void position_of_second_occurenceof_e(String n)
{
int i,m =n.length(),k=0,s=0;
for(i=0;i<m;i++)
{
if(n.charAt(i)=='e' || n.charAt(i)=='E')
{
k=i;
s++;
if(s==2)break;
}
  
}
System.out.println("Position of second occurence of e "+k);
}
  
  
//number of characters your sentence contains besides the space character
void number_of_characters_beside_space(String n)
{
int i,m = n.length();
int sum=0;
for(i=0;i<m;i++)
{
if(n.charAt(i)!=' ')
sum++;
}
  
System.out.println("Number of characters beside space : "+sum);
  
}
  
  
  
public static void main(String argv[])
{
  
  
String str;
Scanner sc = new Scanner(System.in);
  
str =sc.nextLine();//reading line...
  
string s = new string(str);//creating object...intializing with str
  
//calling functions
s.numberofchars(str);
s.print_firstchar(str);
s.print_lastchar(str);
s.contain_e(str);
s.numberof_e(str);
s.contain_ay(str);
s.position_of_lastoccurenceof_e(str);
s.position_of_second_occurenceof_e(str);
s.number_of_characters_beside_space(str);
  
  
}
  
}

output:-

run:
hello this is surya
The number of characters are :19
First char of the sentence :h
Last char of the sentence :a
Yes| it contain e
e appears :1 times
No| it doesn't contain ay
Position of last occurence of e 1
Position of second occurence of e 1
Number of characters beside space : 16
BUILD SUCCESSFUL (total time: 7 seconds)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote