y Minnesota Wild on Twitt × 9 Course: FMIS 3232 Com: × cpb18s-hw5.pdf × Implemen
ID: 3730037 • Letter: Y
Question
y Minnesota Wild on Twitt × 9 Course: FMIS 3232 Com: × cpb18s-hw5.pdf × Implement the main me: × C Secure https//ay17.moodle.umn.edu/pluginfile.php/1657360/mod resource/content/11/cpb18s-hw5.pdf Apps DelUniversity of MinnesConnectMinneapolis/St. Pauumd-fmis.d.umn.edu Dashboard Problem #1 (15 points): String-related classes: String, StringBuffer, StringBuilder 1. Add a new package named hw5p1 to your FirstName-LastName-HWS project. 2. Right click the package and add a class named StringClasses with a main method 3. Implement the main method to print output shown below. Note that the actual time may vary depending on the computer you use Elapsed Time is 79 msec (Build String) Elapsed Time is 74 msec (Using String to reverse) Elapsed Time is 1 msec (Using StringBuffer to reverse) Elapsed Time is msec (Using StringBuffer's reverse() method) Elapsed Time is 1 msec (Using StringBuilder to reverse) Elapsed Time is msec (Using StringBuidler's reverse) method) 4. The main method should start the following code segment Build a long string String str int size = 16536; char ch = 'a'; beginTine System.currentrineMillis); for (int count 8; countExplanation / Answer
public class StringClasses {
public static void main(String[] args) {
String str = "";
int size = 16536;
char ch = 'a';
long beginTime = System.currentTimeMillis();
for(int count =0; count <size; count++) {
str += ch;
++ch;
if(ch > 'z')
ch = 'a';
}
long elapsedTime = System.currentTimeMillis() - beginTime;
System.out.println("Elapsed time is "+elapsedTime+" ms(Build String)");
beginTime = System.currentTimeMillis();
String strReverse = "";
for(int i=str.length()-1; i>=0; i--)
strReverse = strReverse + str.charAt(i);
elapsedTime = System.currentTimeMillis() - beginTime;
System.out.println("Elapsed time is "+elapsedTime+" ms(Using String to reverse)");
beginTime = System.currentTimeMillis();
StringBuffer strBuffer = new StringBuffer();
for(int i=str.length()-1; i>=0; i--)
strBuffer.append(str.charAt(i));
elapsedTime = System.currentTimeMillis() - beginTime;
System.out.println("Elapsed time is "+elapsedTime+" ms(Using StringBuffer to reverse)");
beginTime = System.currentTimeMillis();
StringBuffer strBufferMethos = strBuffer.reverse();
elapsedTime = System.currentTimeMillis() - beginTime;
System.out.println("Elapsed time is "+elapsedTime+" ms(Using StringBuffer's reverse method)");
beginTime = System.currentTimeMillis();
StringBuilder strBuilder = new StringBuilder();
for(int i=str.length()-1; i>=0; i--)
strBuilder.append(str.charAt(i));
elapsedTime = System.currentTimeMillis() - beginTime;
System.out.println("Elapsed time is "+elapsedTime+" ms(Using StringBuilder to reverse)");
beginTime = System.currentTimeMillis();
StringBuilder strBuilderMethos = strBuilder.reverse();
elapsedTime = System.currentTimeMillis() - beginTime;
System.out.println("Elapsed time is "+elapsedTime+" ms(Using StringBuilder's reverse method)");
}
}
/*
Sample run:
Elapsed time is 152 ms(Build String)
Elapsed time is 143 ms(Using String to reverse)
Elapsed time is 2 ms(Using StringBuffer to reverse)
Elapsed time is 1 ms(Using StringBuffer's reverse method)
Elapsed time is 0 ms(Using StringBuilder to reverse)
Elapsed time is 1 ms(Using StringBuilder's reverse method)
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.