Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

JAVA PROGRAM

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 beforeonePastLastPosition. 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 Stringconstructed by starting with currentString, and removes the letters starting with startPositionand stopping before onePastLastPosition. For example, cut(abcd, 1, 3)returns the Stringad

paste

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

StringMangle

You are to write a class StringManglethat has only a mainmethod. This program allows a user to enter a Stringthat well call currentString. It maintains another Stringthat 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 currentStringinto clipboardusing the copymethod above. It asks the user for the starting and one-past the ending positions in currentString.
  • x assigns a new value to currentStringusing the cutmethod above. It asks the user for the starting and one-past the ending positions in currentString.
  • v assigns a new value to currentStringusing the pastemethod above to insert the clipboardinto currentString. It asks the user for the starting position incurrentString.
  • q quits the program.

Notes

  • You may assume that the user enters valid input
  • Use Scannersnext()method to read the currentStringfrom the user. This means that we wont have spaces in the currentString.
  • Use StringUtils.genGaugeto output currentStringto the user. It puts character positions (mod 10) underneath the currentStringscharacters 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

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

Object Oriented Databases Prentice Hall International Series In Computer Science

Authors: John G. Hughes

1st Edition

0136298745, 978-0136298748

More Books

Students also viewed these Databases questions

Question

=+a. Journalize the adjusting entry to record the accrued fees.

Answered: 1 week ago