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

I was wondering if you can tell me if the following code is correct and if...
I was wondering if you can tell me if the following code is correct and if its not can it be fixed so it does not have any syntax errors. Client one /** * Maintains information on an insurance client. * * @author Doyt Perry/<add your name here> * @version Fall 2019 */ public class Client { // instance variables private String lastName; private String firstName; private int age; private int height; private int weight; /** * First constructor for...
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...
Can you please check my answers and if I am wrong correct me. Thank you! A....
Can you please check my answers and if I am wrong correct me. Thank you! A. In today's interconnected world, many central banks communicate regularly and frequently with the public about the state of the economy, the economic outlook, and the likely future course of monetary policy. Communication about the likely future course of monetary policy is known as "forward guidance.". If the central bank increases the reserve ratio, as the market has perfectly expected, which of the following will...
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...
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?
Can you fix to me this code plz I just want to print this method as...
Can you fix to me this code plz I just want to print this method as the reverse. the problem is not printing reverse. public void printBackward() {               Node curr = head;        Node prev = null;        Node next = null;               System.out.print("\nthe backward of the linkedlist is: ");               while(curr != null) {            next = curr.next;            curr.next = prev;   ...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT