Question

In: Computer Science

created a new StringBuilder sb using the following index: StringBuilder sb = new StringBuilder ("Welcome To...

created a new StringBuilder sb using the following index:

StringBuilder sb = new StringBuilder ("Welcome To ");

What would be sb after each of the following statements? Explain the methods used and how they change the content

sb.append("Java");

sb.insert(11, "HTML and ");

Solutions

Expert Solution

StringBuilder is a predefined class used to create as StringBuffer but the basic difference between both of them is String created by StringBuilder is not synchronized as in StringBuffer it is. The difference between StringBuilder and StringBuffer and common String is that StringBuilder and StringBuffer create mutable String.

As in our question, we created an object of StringBuilder(sb) and passing the value  "Welcome To". So when that line is compiled then, a new StringBuilder object will be created initialized with "Welcome To".

Now String will be created containing "Welcome To"

In the second-line, the user called the append function and pass value Java. This function appends the given string at the end of the string stored at StringBuilder.

After calling this function passing "Java" String. After compiling this line, the string will be "Welcome To Java".

And, insert function adds the given string at the given index. That line will add "HTML and " at the 11th index of the string. As we can see in the image, At the 11th index "J" is stored, so "HTML and " will be placed starting from the 11th index.

So Final String Will be

Welcome To HTML and Java


Related Solutions

USING JAVA Consider the following methods: StringBuilder has a method append(). If we run: StringBuilder s...
USING JAVA Consider the following methods: StringBuilder has a method append(). If we run: StringBuilder s = new StringBuilder(); s.append("abc"); The text in the StringBuilder is now "abc" Character has static methods toUpperCase() and toLowerCase(), which convert characters to upper or lower case. If we run Character x = Character.toUpperCase('c');, x is 'C'. Character also has a static isAlphabetic() method, which returns true if a character is an alphabetic character, otherwise returns false. You will also need String's charAt() method,...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to Zion's Pizza Restaurant and ask the user how many people are in their dinner group. If the answer is more than eight (8), print a message saying they'll have to wait for a table. Otherwise, report that their table is ready and take their order. Assume the client orders one pizza, and ask what he/she would like on their pizza, include a loop that...
1.  Complete the second printSalutation() method to print the following given personName "Holly" and customSalutation "Welcome": Welcome,...
1.  Complete the second printSalutation() method to print the following given personName "Holly" and customSalutation "Welcome": Welcome, Holly End with a newline. What I am given: import java.util.Scanner; public class MultipleSalutations { public static void printSalutation(String personName) { System.out.println("Hello, " + personName); } //Define void printSalutation(String personName, String customSalutation)... /* Your solution goes here */ public static void main (String [] args) { printSalutation("Holly", "Welcome"); printSalutation("Sanjiv"); } } 2. Write a second convertToInches() with two double parameters, numFeet and numInches, that...
Description: One problem with dynamic arrays is that once the array is created using the new...
Description: One problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This assignment asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings. The class should have the following: • A private member variable called...
a.) A sociology professor has created a new assessment of political awareness. In using the assessment,...
a.) A sociology professor has created a new assessment of political awareness. In using the assessment, she has determined that political awareness is normally distributed in college students with a population mean of 35.3 and a population standard deviation of 8.6. Using the z-score found for the question above, what proportion of the population falls below a score of 22 on this test? Round your answer to four decimal places. b.) A sociology professor has created a new assessment of...
SB The following condensed balance sheet is... The following condensed balance sheet is presented for the...
SB The following condensed balance sheet is... The following condensed balance sheet is presented for the partnership of D, E, and F who share profits and losses in the ratio of 5:3:2, respectively: Cash $ 100,000 Other Assets 480,000 $ 580,000 Liabilities $ 160,000 D, Capital 200,000 E, Capital 130,000 F, Capital 90,000 Total $ 580,000 The partners agreed to liquidate the partnership after selling the other assets. TB MC Qu. 16-12 Refer to the above information. If the other......
Using the following data, the Consumer Price Index for 2011 and 2015 using the base 1982...
Using the following data, the Consumer Price Index for 2011 and 2015 using the base 1982 = 100 ($1,210,000) are __________ and ________________. In reality, the CPI is based on a larger basket of good. Assuming the following prices, the average annual inflation rate for the 33 years (1982-2015) is_______________ The core inflation rate is similar except that it excludes ___________ and ____________ prices because ___________________. The core rate is __________________ If the average rate of inflation is the expected...
A student researcher conducted a new study (N=10) on intelligence using a newly created measure, the...
A student researcher conducted a new study (N=10) on intelligence using a newly created measure, the IQ-5000 and wanted to compare his results to a similar study that used a different, but the commonly used measure, the WAIS. Both studies indicated that intelligence is normally distributed, but the student wanted to convert scores into z scores for comparison. After converting the scores in his study to z scores, he got the following: -4.7, 4.8, 0.9, -6.5, 3.1, -2.8, -5.3, 7.2,...
How was the new religion of Islam created?
How was the new religion of Islam created?
JavaScript Create a welcome message on your webpage using three variables At least two of the...
JavaScript Create a welcome message on your webpage using three variables At least two of the variables should be concatenated together Use document.write to add a statement about what is your favorite past time? Format the HTML document appropriately with a title, and the correct HTML structure to hold the JavaScript Create an array to share what your favorite color is. The array must have at least three values and be called via the index value Correctly link the JavaScript...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT