Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

WRITE IN JAVA, please include comments and explain what you did Write a program

ID: 3914201 • Letter: W

Question

WRITE IN JAVA, please include comments and explain what you did

Write a program which will accept a string from the keyboard, “Java is fun!” Your program must determine the capacity of the string. Then, append a second string, “I love it!” to the first string. At this point you will enter a string “Yes, “ and insert this string so that the output of the entire string will display “Java is fun. Yes, I love it!”

Directions

Create a class called Builder.

Using the StringBuilder class, write a program which will accept a string from the keyboard, “Java is fun!”

Your program must determine and display the capacity of the string.

Append a second string, “I love it!” to the first string.

Require the user to enter a string “Yes, “

Insert the string, “Yes ” at the correct location, such that the output of the entire string will be “Java is fun. Yes, I love it!”

Display your output using clear and appropriate messages.

Grading

Task

Points

iClass created and named

1

String variables declared and initialized

2

Appropriate use of StringBuilder

2

Appropriate use of string methods

3

Appropriate messages displayed

1

Proper documentation / Pseudocode

1

Total

10

Task

Points

iClass created and named

1

String variables declared and initialized

2

Appropriate use of StringBuilder

2

Appropriate use of string methods

3

Appropriate messages displayed

1

Proper documentation / Pseudocode

1

Total

10

Explanation / Answer

Builder.java

import java.util.Scanner;

public class Builder {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Enter the string: ");

String str = scan.nextLine();

StringBuilder sb = new StringBuilder(str);

System.out.println("Capacity: "+sb.capacity());

sb.append(" ");

sb.append("I love it!");

System.out.println("Enter another string: ");

String s = scan.nextLine();

sb.insert(str.length()+1, s);

System.out.println(sb.toString());

}

}

Output:

Enter the string:
Java is fun.
Capacity: 28
Enter another string:
Yes,
Java is fun. Yes, I love it!