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

Working with Strings The following program illustrates the use of some of the me

ID: 639319 • Letter: W

Question

Working with Strings

The following program illustrates the use of some of the methods in the String class. Study the program to see what it is doing.

// ***************************************************************

// StringManips.java

//

// Test several methods for manipulating String objects

// ***************************************************************

import java.util.Scanner;

public class StringManips

{

public static void main (String[] args)

{

   String phrase;

   int phraseLength; // number of characters in the phrase String

   int middleIndex; // index of the middle character in the String

   String firstHalf; // first half of the phrase String

   String secondHalf; // second half of the phrase String

   String switchedPhrase; // a new phrase with original halves switched

   //read in a phrase

Scanner scan = new Scanner(System.in);

System.out.println("Please enter a phrase:");

   phrase = scan.nextLine();

   // compute the length and middle index of the phrase

   phraseLength = phrase.length();

   middleIndex = phraseLength / 2;

   // get the substring for each half of the phrase

   // concatenate the firstHalf at the end of the secondHalf

   // print information about the phrase

   System.out.println();

   System.out.println ("Original phrase: " + phrase);

   System.out.println ("Length of the phrase: " + phraseLength +

              " characters");

   System.out.println ("Index of the middle: " + middleIndex);

   System.out.println ("Character at the middle index: " +

              phrase.charAt(middleIndex));

   System.out.println();

}

}

The file StringManips.java contains this program. Save the file to your directory and compile and run it. Study the output and make sure you understand the relationship between the code and what is printed. Now modify the file as follows:

1.   Add statements that use the substring method to get the substring for first half and second half of phrase.

2.   Concatenate the first half at the end of the second half of phrase and assign to switchedPhrase. Add a println statement to print out switchedPhrase.

3.    Declare a variable of type String named middle3 (put your declaration with the other declarations near the top of the program) and use an assignment statement and the substring method to assign middle3 the substring consisting of the middle three characters of phrase (the character at the middle index together with the character to the left of that and the one to the right

Explanation / Answer

1,2,3,4 Answers:

Java Program:

import java.util.Scanner;

public class StringManips

{

public static void main (String[] args)

{

String phrase = new String ("This is a Stringtest.");

int phraseLength;

int middle3;

int middleIndex;

int middleplus1;

int middleminus1;
  
String firstHalf;

String secondHalf;

String switchedPhrase;

phraseLength = phrase.length();

middleIndex = phraseLength / 2;

middleplus1=(middleIndex + 1);

middleminus1=(middleIndex - 1);

middle3=(middleminus1 + middleIndex + middleplus1);

firstHalf = phrase.substring(0,middleIndex);

secondHalf = phrase.substring(middleIndex, phraseLength);

switchedPhrase = secondHalf.concat(firstHalf);

System.out.println (middle3);

System.out.println();

System.out.println ("Original phrase: " + phrase);

System.out.println ("Length of the phrase: " + phraseLength +

" characters");

System.out.println ("Index of the middle: " + middleIndex);

System.out.println ("Character at the middle index: " +

phrase.charAt(middleIndex));

System.out.println ("Switched phrase: " + switchedPhrase);

System.out.println("The 3 middle characters is" + middle3);

}

}

5. Answer:

import java.util.Scanner;

public class StringManipsORIG

{
public static void main (String[] args)

{   

String city;

String state;

Scanner input = new Scanner(System.in);

String phrase = new String ("this is a string test.");

int phraselength;

int middleindex; // index of the middle character in the string

String firsthalf; // first half of the phrase string

String secondhalf; // second half of the phrase string

String switchedphrase; // a new phrase with original halves switched

// compute the length and middle index of the phrase

phraselength = phrase.length();

middleindex = phraselength / 2;

firsthalf = phrase.substring(0,middleindex);

secondhalf = phrase.substring(middleindex, phraselength);

// concatenate the firsthalf at the end of the secondhalf

switchedphrase = secondhalf.concat(firsthalf);

switchedphrase = switchedphrase.replace(' ','*');

// print information about the phrase

System.out.println();

System.out.println ("original phrase: " + phrase);

System.out.println ("length of the phrase: " + phraselength + " characters");

System.out.println ("index of the middle: " + middleindex);

System.out.println ("character at the middle index: " + phrase.charAt(middleindex));

System.out.println ("switched phrase: " + switchedphrase);

System.out.println();

String middle3 = phrase.substring(middleindex-1,middleindex+2);

System.out.println ("Middle3:" +middle3);

System.out.print("Enter your city: ");

city = input.nextLine();

System.out.print("Enter your state: ");

state = input.nextLine();

String hometown = state.toUpperCase() + city.toLowerCase() + state.toUpperCase();

System.out.println ("Hometown: " + hometown);

}

}

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