Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

These exercises will allow you to have some practice with some basic ideas involving classes and objects. In addition you will learn about a new

These exercises will allow you to have some practice with some basic ideas involving classes and objects. In addition you will learn about a new class that is part of the Java API - the StringBuilder class.

Objectives

Practice with programming fundamentals

Variables - Declaration and Assignment

Primitive types

Arithmetic Expressions

Simple keyboard input and text display output

Branching - if-elseif-else syntax

Loops - simple while loops, nested while loops

Methods - functions and procedures

Classes

Works towards the following Course Goals:

Competency with writing computer programs to implement given simple algorithms

Familiarity with using methods and classes to produce well-structured programs

Exposure to data abstraction concepts and other more advanced programming ideas

Overall Lab 13 Instructions

Using the instructions from Closed Lab 01, create a new folder named ClosedLab13. You will need to import the following file into your new lab folder. Follow the instructions from Clsoed Lab 01 to import this file into your ClosedLab13 folder.

Lab13a.java

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.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

/**

* 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:

1

2

3

4

5

6

7

8

9

10

/**

* 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!

File to import:

/* * 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) { } } 

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

Database Design And SQL For DB2

Authors: James Cooper

1st Edition

1583473572, 978-1583473573

More Books

Students also viewed these Databases questions

Question

=+1 Do we have knowledgeable staffing for a global strategy?

Answered: 1 week ago

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago