Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

LAB 13 Use the following skeleton: /* * Lab13a.java * * A program to learn about the operations of the StringBuilder class. * * @author

LAB 13

Use the following skeleton:

/* * Lab13a.java * * A program to learn about the operations of the StringBuilder class. * * @author ENTER YOUR NAMES HERE * */ package osu.cse1223; public class Lab13a { /** * Removes all space characters from a String * * @param input * String to have spaces removed from * @return the input String with spaces removed */ public static String removeSpaces(String input) { } public static void main(String[] args) { } }

You will be exploring the methods of the StringBuilder class. Before starting the exercises below, scan through the StringBuilder API. Read through the top part thoroughly and skim through the description of the various methods the class provides. Pay particular attention to the append, delete, deleteCharAt and toString methods. The StringBuilder API provides a class for dealing with mutable Strings. The String objects we have used so far are immutable objects - once you create a String you cannot change the characters in that String. StringBuilder provides a way of allowing you to create String objects where individual characters can be changed. This behavior is useful when we want to have Strings where we modify the individual characters rather than creating an entirely new String every time we want to make a change to it. In this lab you will explore the methods of the StringBuilder class to get familiar with what they do. In next week's closed lab you will be implementing your own version of a StringBuilder class.

Exercise 1 Description

Examine the code for the method removeSpaces below. This method takes an input String and returns a new String with any spaces removed from it.

/**

* Removes all space characters from a String

* @param input String to have spaces removed from

* @return the input String with spaces removed

*/

public static String removeSpaces(String input) {

StringBuilder result = new StringBuilder();

for (int i = 0; i < input.length(); i++) {

if (input.charAt(i) != ' ') {

result = result.append(input.charAt(i));

}

}

return result.toString();

}

Paste the above method into your Lab13a.java file and then write a main method that uses the above method to remove spaces from an input String provided by a user. The main method should produce the following transcript:

Enter a string to de-space: The quick brown fox jumped Without spaces: Thequickbrownfoxjumped Enter a string to de-space: Goodbye!

Exercise 2 Description

Create a copy of your solution for Exercise 1 and name it Lab13b.java in the same ClosedLab13 folder. For this exercise you are going to make a new method named expandString. This method should take a String as input and return back a new String that has been expanded by repeating each character in the String a number of times equal to its position in the String. For example - the character at position zero should be repated 0 times, the character at position 1 one time, the character at position 2 two times and so on. This new method should use StringBuilder to create the new String in a fashion similar to how Exercise 1 used StringBuilder. Modify your main method to call this new method. Your method should use the following method header and comments:

/**

* Expands a String by repeating characters in the String.

* The character at position 0 is repeated 0 times, position one

* is repeated once, position 2 is repeated twice and so on.

* @param input String to expand

* @return the input String expanded

*/

public static String expandString(String input) {

// your code goes here

}

And your program should be able to produce the following transcript: Enter a string to expand: Howdy Expanded: owwdddyyyy Enter a string to expand: CSE1223 Expanded: SEE111222222222333333 Goodbye! Note that the character at position zero appears 0 times, so it is not a bug if the first character you type does not appear in the output at all.

Exercise 3

Create a copy of your solution for Exercise 2 and name it Lab13c.java in the same ClosedLab13 folder. For this exercise, you are going to create a program that takes a String as input from the keyboard, and then prompts the user for characters to delete. The code must use a StringBuilder to keep track of the current String, and delete characters from that StringBuilder object as directed by the user. The program should continue to ask for positions to delete until the user enters a negative number, and the program should continue to prompt for new Strings until the user enters an empty String. You can write methods to do this or do it all in the main method for this closed lab question. A sample transcript appears below: Enter a string (empty to quit): Hello World! Enter a position to delete (-1 to end): 5 Current string: HelloWorld! Enter a position to delete (-1 to end): 9 Current string: HelloWorl! Enter a position to delete (-1 to end): 3 Current string: HeloWorl! Enter a position to delete (-1 to end): 7 Current string: HeloWor! Enter a position to delete (-1 to end): -1 Final String: HeloWor! Enter a string (empty to quit): Goodbye!

Exercise 4

If you have time, create a copy of your solution for Exercise 3 and name it Lab13d.java in the same ClosedLab13 folder. For this exercise, modify your program to remove substrings of the original string instead of positions. As a hint towards how to go about doing this, take a look at the StringBuilder methods indexOf and delete. As in Exercise 3, you can write methods to do this or do it all in the main method for this closed lab question. A sample transcript appears below: Enter a string (empty to quit): Hello World! Enter a substring to delete (empty to end): orld Current string: Hello W! Enter a position to delete (empty to end): lo Current string: Hel W! Enter a position to delete (empty to end): He Current string: l W! Enter a position to delete (empty to end): ! Current string: l W Enter a position to delete (empty to end): Final String: l W Enter a string (empty to quit): Goodbye!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Power Bi And Azure Integrating Cloud Analytics For Scalable Solutions

Authors: Kiet Huynh

1st Edition

B0CMHKB85L, 979-8868959943

More Books

Students also viewed these Databases questions

Question

Identify cultural barriers to communication.

Answered: 1 week ago