Suppose somePhrase is any String and assume that it has alre

Suppose somePhrase is any String, and assume that it has already been created. Also, assume that a second String variable, anotherPhrase, has already been declared. Using the StringBuilder class, write code to place in anotherPhrase a palindrome version of somePhrase with a * character in the middle. For example, if somePhrase initially is \"abcd\", then anotherPhrase should ultimately hold \"abcd*dcba\"; and if somePhrase holds \"Oh dear!\", then ultimately anotherPhrase should hold \"Oh dear!*!raed hO\". When you\'ve built the final version of anotherPhrase, then print it to the console. Hint: first make two distinct StringBuilder versions of somePhrase. Then reverse the second one and use the append method to assemble and then print your final answer. The reverse method is tricky. If s and t are StringBuilder variables, and if s references \"xyz\", then the statement t = s.reverse(); puts the reverse of \"xyz\", \"yzx\", in both s and t. Enter your sequence of commands, ending with a print statement, in the box below.

Solution

java statemnets to add the palindrome of the string using stringbuilder with * in middle
       //Create a string object
       String somePhrase=\"Oh dear!\";
       //declare anohter string object
       String anotherPhrase;
      
       //assign the somePhrase to anotherPhrase
       anotherPhrase=somePhrase;
      
      
       //Create string builder of anotherPhrase
       StringBuilder sb1=new StringBuilder(anotherPhrase);
      
       //Create string builder of somePhrase
       StringBuilder sb2=new StringBuilder(somePhrase);
      
       //reverse the sb1 and assign to the anotherPhrase string
       anotherPhrase=new String(sb1.reverse());
      
      
       //call append on sb2 and set * and anotherPhrase string
       sb2.append(\"*\"+anotherPhrase);
      
       //print to console
       System.out.println(sb2);

Suppose somePhrase is any String, and assume that it has already been created. Also, assume that a second String variable, anotherPhrase, has already been decla

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site