Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Objectives By the end of this program, the student will have demonstrated the ability to Write static methods Use methods in a different class Write

Objectives

By the end of this program, the student will have demonstrated the ability to

  • Write static methods
  • Use methods in a different class
  • Write a sentinel-controlled loop
  • Write nested if/else statements
  • Manipulate Strings by character position

StringUtils

You are to complete the class StringUtils, which is in the attached zip file. Add the following public static methods:

copy

Write a method String copy(String currentString, int startPosition, int onePastLastPosition). This method returns a substring of currentString, including startPosition, but ending before onePastLastPosition. For example, copy(abcd, 1, 3) returns the String bc

cut

Write a method String cut(String currentString, int startPosition, int onePastLastPosition). This method returns a String constructed by starting with currentString, and removes the letters starting with startPosition and stopping before onePastLastPosition. For example, cut(abcd, 1, 3) returns the String ad

paste

Write a method String paste(String currentString, int insertBefore, String stringToInsert). This method returns the String constructed by inserting stringToInsert into currentString before position insertBefore. For example, paste(ad, 1, bc) returns the String abcd

StringMangle

You are to write a class StringMangle that has only a main method. This program allows a user to enter a String that well call currentString. It maintains another String that well call clipboard. The program asks the user to enter currentString. It then uses a sentinel-controlled loop to ask the user to enter one of four commands:

  • c copies part of currentString into clipboard using the copy method above. It asks the user for the starting and one-past the ending positions in currentString.
  • x assigns a new value to currentString using the cut method above. It asks the user for the starting and one-past the ending positions in currentString.
  • v assigns a new value to currentString using the paste method above to insert the clipboard into currentString. It asks the user for the starting position in currentString.
  • q quits the program.

Notes

  • You may assume that the user enters valid input
  • Use Scanners next() method to read the currentString from the user. This means that we wont have spaces in the currentString.
  • Use StringUtils.genGauge to output currentString to the user. It puts character positions (mod 10) underneath the currentStrings characters to help the user enter data. For example, System.out.println(StringUtils.genGauge(currentString));
  • Write this program in baby steps. There are many ways to approach writing this program, but whichever order you use, write something and test it immediately. For example, if you decide to write the methods first, write one method and test it. Then write another and test it. And so on. When writing your main method, if you write the loop first, start by implementing the q command and test it, then move to another command and test it. Its better to have something that works, though its incomplete, than to have something complete that has nothing working.

Grading Elements

  • All class and method names are correct
  • All method signatures are correct
  • All methods are in the correct class
  • All methods return the proper value
  • All methods are public static
  • The main method uses a sentinel-controlled loop
  • The main method prompts the user for commands c, x, v, q
  • The main method handles each command appropriately
  • The main method displays the currentString and clipboard before asking the user for a command
  • The program terminates when the user enters q
  • The main method uses StringUtils.genGauge to output the currentString

Sample Execution

Enter a string

fghdeabc

The current string is

fghdeabc

01234567

Enter c to copy, x to cut, v to paste, q to quit

C

Enter the starting position

5

Enter one past the ending position

8

The current string is

fghdeabc

01234567

The clipboard is

abc and it has length of 3

Enter c to copy, x to cut, v to paste, q to quit

v

Enter the position the paste should come before

0

The current string is

abcfghdeabc

01234567890

The clipboard is

abc and it has length of 3

Enter c to copy, x to cut, v to paste, q to quit

c

Enter the starting position

6

Enter one past the ending position

8

The current string is

abcfghdeabc

01234567890

The clipboard is

de and it has length of 2

Enter c to copy, x to cut, v to paste, q to quit

v

Enter the position the paste should come before

3

The current string is

abcdefghdeabc

0123456789012

The clipboard is

de and it has length of 2

Enter c to copy, x to cut, v to paste, q to quit

x

Enter the starting position

8

Enter one past the ending position

13

The current string is

abcdefgh

01234567

The clipboard is

de and it has length of 2

Enter c to copy, x to cut, v to paste, q to quit

q

START UP CODE:

public class StringUtils { public static String genGauge(String sb) { String strAndGauge = sb + " "; for (int i = 0; i < sb.length(); i++) { strAndGauge += (i % 10); } return strAndGauge; } }

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 Principles Programming And Performance

Authors: Patrick O'Neil, Elizabeth O'Neil

2nd Edition

1558605800, 978-1558605800

More Books

Students also viewed these Databases questions

Question

44. Show that ( ) ( ). Give an interpretation involving subsets.

Answered: 1 week ago

Question

How is the NDAA used to shape defense policies indirectly?

Answered: 1 week ago

Question

Discuss the history of human resource management (HRM).

Answered: 1 week ago