Question

In: Computer Science

String Mutator: Can you guy finish the last 2 method for me and correct me if...

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

Solutions

Expert Solution

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 :


Related Solutions

Could you please also give me a code that does not use the string split method...
Could you please also give me a code that does not use the string split method but instead gives an input and output file that you have to compile using "type output.txt" in java. This is the code: Record.java public class Record { private String stateCode, districCode, districtName; private int totalPopulation, childPopulation, childPovertyPopulation; private String miscStats; public Record(String stateCode, String districCode, String districtName, int totalPopulation, int childPopulation, int childPovertyPopulation, String miscStats) { this.stateCode = stateCode; this.districCode = districCode; this.districtName =...
Hi can you do me this 3 exercises? if you answer them all and are correct...
Hi can you do me this 3 exercises? if you answer them all and are correct I will put thumbs up! thanks :) 7) Focus groups of 14 people are randomly selected to discuss products of the Famous Company. It is determined that the mean number (per group) who recognize the Famous brand name is 9, and the standard deviation is 0.79. Would it be unusual to randomly select 14 people and find that greater than 13 recognize the Famous...
can someone finish and check my code on main. cpp? Its not working for me even...
can someone finish and check my code on main. cpp? Its not working for me even though im sure my code make sense is it possible to output each function to show they work. this is supposed to be a vector class library made from allocated memory i have included templated functions in the class file to help create the rest of the functions. Thank you so much note: i did not include main.cpp because it  was empty- im hoping someone...
Just anwser the question for me to finish the essays. Thank you! [10 marks] Given the...
Just anwser the question for me to finish the essays. Thank you! [10 marks] Given the following two articles: Google Says a Change in Its Algorithm Will Highlight 'Original Reporting’ THE NEWYORK TIMES By Marc Tracy Sept. 12, 2019 https://www.nytimes.com/2019/09/12/business/media/google-algorithm-original-reporting.html THE STATE OF AI IN 2019 By James Vincent Jan 28, 2019, 8:00am EST This is what's happening in artificial intelligence right now https://www.theverge.com/2019/1/28/18197520/ai-artificial-intelligence-machine-learning-computational-science INSTRUCTIONS: • Write an essay for each article; total two (2) essays. In each essay, answer...
Part 1 readFile(String filename) In this method you are passed a String with the name of...
Part 1 readFile(String filename) In this method you are passed a String with the name of a file. This method will read the file in line by line and store each line in a String array. This String array is then returned. An example is shown below. File Contents: Purple Rain by Prince I never meant to cause you any sorrow I never meant to cause you any pain I only wanted one time to see you laughing I only...
can you give me an overview for ias 28 and explain the equity method for ias...
can you give me an overview for ias 28 and explain the equity method for ias 28 and how we use the equity method in ias 28?
I just wanted to double check my answers, can you just provide me with the correct...
I just wanted to double check my answers, can you just provide me with the correct answer for each one in order to cross-check, thank you. The United States' class system is heavily dependent upon an individual's social background. True False At which level of the factors which put women at risk does the following statement fit? "Tolerance of violence as a means of conflict resolution". Family/Relationship Level Individual Level Community Level Societal Level Which of the following countries is...
I just wanted to double check my answers, can you just provide me with the correct...
I just wanted to double check my answers, can you just provide me with the correct answer for each one in order to cross-check, thank you. When women carry the burden of poverty and are treated as non-equals when compared to men, which of the following terms describes this circumstance? Twice Burdened Double Discrimination Double Deprivation Double Jeopardy Which of the following countries was not a colony of Britain? Singapore Korea Hong Kong In 2012, which country utilized 2.8% of...
Can you please tell me, what would be the correct way of doing it? def updateRepresentation(blank,...
Can you please tell me, what would be the correct way of doing it? def updateRepresentation(blank, secret, letter): """ This function replaces the appropriate underscores with the guessed letter. Eg. letter = 't', secret = "tiger", blank = "_i_er" --> returns "ti_er" Paramters: blank, secret are strings letter is a string, but a single letter. Returns: a string """ #TODO -- complete this function so that it produces a new string #from blank with letter inserted into the appropriate locations....
catch me if you can (2002) write a report about catch me if you can (2002)...
catch me if you can (2002) write a report about catch me if you can (2002) must be 900 words that fully describes the movie including describing the main characters, the fraud situation in the movie, and how the situation plays out. The second part of the paper should discuss the actual persons and/or companies on which the movie focuses: Identify these actual persons / company; identify the type(s) of fraud(s) are alleged in the film; discuss your personal feelings...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT