In: Computer Science
String Mutator: Can you guy finish the last 2 method for me and correct me if I have any mistake. It is due in the next 2 hours. Thank you!
//use equals ( ) to determine equality of string phrases
//use compareTo( ) to determine which comes first alphabetically
The code is below and it need to be written in Java language.
/**
* Programmer:
*Date:
* StringMutation works with string methods, printing length, 1st
half, 2nd half, middle 3
* characters, switch 1st & 2nd halfs, replace a character, use
compareTo & equals
*Strings are immutable so need new variables to store modified
versions of strings
*/
public class StringMutation
{
public static void main(String[] args)
{
String myString = new String("This is a test String.");
String myString2 = "This is a second test String.";
int phraseLength, middleIndex;
String firstHalf, secondHalf, switchPhrase, middle3;
char charMid,
//manipulation of myString (If you would like more practice, repeat
for myString2)
//determine and store length
phraselength = myString.length();
//find and store middleIndex
middleIndex = phraseLength / 2;
//store first half
firstHalf = myString.substring(O,middleIndex);
//store second half (can be done 2 ways)
secondHalf = myString.substring(middleIndex, phraseLength -1)
;
//place first half at end of second half
switchPhrase = secondHalf.concat (firstHalf);
//find the middle 3 characters
middle3 = myString.substring(middleIndex - 1, middleIndex + 2);
//print the following:
//original phrase
system.out.printlnt("Original phrase: " + myString);
//length of phrase
system.out.println(" Length of phrase: " + phraseLength);
//middle index
system.out.println(" index of the middle: " + middleindex);
//character at middle index
charMid = myString.charAt (middleIndex);
//replace all spaces with @
system.out.println("Replace [spaces]: " + myString.replace('
','@'));
//switched string (secondHalf firstHalf)
//use equals( ) to determine equality of string phrases
//use compareTo( ) to determine which comes first
alphabetically
Here is an error free java code with last methods implemented :
import java.util.*;
import java.lang.*;
import java.io.*;
class StringMutation{
public static void main(String[] args) {
String myString = new String("This is a test String.");
String myString2 = "This is a second test String.";
int phraseLength,middleIndex;
String firstHalf, secondHalf, switchPhrase, middle3;
char charMid;
//manipulation of myString (If you would like more practice, repeat for myString2)
//determine and store length
phraseLength = myString.length();
//find and store middleIndex
middleIndex = phraseLength / 2;
//store first half
firstHalf = myString.substring(0, middleIndex);
//store second half (can be done 2 ways)
secondHalf = myString.substring(middleIndex, phraseLength - 1);
//place first half at end of second half
switchPhrase = secondHalf.concat(firstHalf);
//find the middle 3 characters
middle3 = myString.substring(middleIndex - 1, middleIndex + 2);
//print the following:
//original phrase
System.out.println("Original phrase: " + myString);
//length of phrase
System.out.println(" Length of phrase: " + phraseLength);
//middle index
System.out.println(" index of the middle: " + middleIndex);
//character at middle index
charMid = myString.charAt(middleIndex);
//replace all spaces with @
System.out.println("Replace [spaces]: " + myString.replace(' ', '@'));
System.out.println("Switched Phrase: " + switchPhrase);
//use equals( ) to determine equality of string phrases
boolean isEqual = myString.equals(myString2);
System.out.println("String 1 : " + myString);
System.out.println("String 2 : " + myString2);
System.out.println("String Equal : " + isEqual);
String s3 = "This is a test String.";
System.out.println("String 1 : " + myString);
System.out.println("String 2 : " + s3);
isEqual = myString.equals(s3);
System.out.println("String Equal : " + isEqual);
//use compareTo( ) to determine which comes first alphabetically
// if s1 > s2, it returns positive number
// if s1 < s2, it returns negative number
// if s1 == s2, it returns 0
String s1 = "abc";
String s2 = "def";
int v = s1.compareTo(s2);
if(v==0){
System.out.println(s1 + " is equal to " + s2 );
}else if(v>0){
System.out.println(s1 + " is greater than " + s2 );
}else{
System.out.println(s1 + " is less than " + s2 );
}
}
}
Output :