Question

JAVA, please help asap! Its due very soon and I cant figure it out StringUtils You...

JAVA, please help asap! Its due very soon and I cant figure it out

StringUtils

You are to complete the class StringUtils, which is below.

public static void genGauge(String currentString) {

System.out.println(currentString);

int c = 0;

int l = currentString.length();

String genGauge = "";

while(c < l) {

genGauge = genGauge + c;

if (c < 9) {c++;}

else {c = 0; l = l - 10;}

}

System.out.println(genGauge);

}




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 we’ll call currentString. It maintains another String that we’ll 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 Scanner’s next() method to read the currentString from the user. This means that we won’t have spaces in the currentString.
Use StringUtils.genGauge to output currentString to the user. It puts character positions (mod 10) underneath the currentString’s characters to help the user enter data. For example, System.out.println(StringUtils.genGauge(currentString));


Sample Output:

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


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program Screen Shot:

Sample Output:

Program Code to Copy:


public class StringUtils {
  
   public static void genGauge(String currentString) {
       System.out.println(currentString);
       int c = 0;
       int l = currentString.length();
       String genGauge = "";
       while(c < l) {
           genGauge = genGauge + c;
           if (c < 9) {c++;}
           else {c = 0; l = l - 10;}
       }
       System.out.println(genGauge);
       }
  
   // returns a substring of currentString, including startPosition, but ending before onePastLastPosition
   public static String copy(String currentString, int startPosition, int onePastLastPosition) {
       return currentString.substring(startPosition, onePastLastPosition);
   }
  
   // returns a String constructed by starting with currentString, and removes the letters starting with
   // startPosition and stopping before onePastLastPosition
   public static String cut(String currentString, int startPosition, int onePastLastPosition) {
       return currentString.substring(0,startPosition) +
           currentString.substring(onePastLastPosition,currentString.length());
   }
  
   // returns the String constructed by inserting stringToInsert into currentString before position insertBefore
   public static String paste(String currentString, int insertBefore, String stringToInsert) {
       return currentString.substring(0,insertBefore) + stringToInsert +
           currentString.substring(insertBefore,currentString.length());
   }

}

_______________________________________________________

import java.util.Scanner;

public class StringMangle {

   public static void main(String[] args) {
      
       Scanner input = new Scanner(System.in);
       // get string from console/user
       System.out.println("Enter a string: ");
       String currentString = input.next();
      
       String clipboard = ""; // initialize clipboard
       int startPosition, onePastLastPosition;
       String choice; // menu option
  
       // start sentinel-controlled loop
       do {
          
           System.out.println("The current string is");
           StringUtils.genGauge(currentString);
          
           if(clipboard.length()>0)
           System.out.println("The clipboard is \n"+ clipboard +" and it has a length of "+clipboard.length());
          
           System.out.println("Enter c to copy, x to cut, v to paste, q to quit");
           choice = input.next().toLowerCase(); // convert to lower for easy comparison in switch-case below
          
           switch(choice) {
           case "c": // copy
               System.out.println("Enter the starting position ");
               startPosition = input.nextInt();
               System.out.println("Enter one past the ending position ");
               onePastLastPosition= input.nextInt();
               clipboard = StringUtils.copy(currentString, startPosition, onePastLastPosition);
               break;
           case "x": // cut
               System.out.println("Enter the starting position ");
               startPosition = input.nextInt();
               System.out.println("Enter one past the ending position ");
               onePastLastPosition= input.nextInt();
               currentString = StringUtils.cut(currentString, startPosition, onePastLastPosition);
               break;
           case "v": // paste
               System.out.println("Enter the position the paste should come before ");
               startPosition = input.nextInt();
               currentString = StringUtils.paste(currentString,startPosition,clipboard);
               break;
           case "q": // will end sentinel-controlled loop
               break;
           default: // unknown input
               System.out.println("Invalid input");
               break;
           }
          
       }while(!choice.equals("q")); // loop again if choice is not q
   }

}


------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~

Add a comment
Know the answer?
Add Answer to:
JAVA, please help asap! Its due very soon and I cant figure it out StringUtils You...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Objectives By the end of this program, the student will have demonstrated the ability to 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...

  • Write the Java code for the class WordCruncher. Include the following members:

    **IN JAVAAssignment 10.1 [95 points]The WordCruncher classWrite the Java code for the class WordCruncher. Include the following members:A default constructor that sets the instance variable 'word' to the string "default".A parameterized constructor that accepts one String object as a parameter and stores it in the instance variable. The String must consist only of letters: no whitespace, digits, or punctuation. If the String parameter does not consist only of letters, set the instance variable to "default" instead. (This restriction will make...

  • Need help with implementing a While, Do-while and for loops in a program. UML: - scnr...

    Need help with implementing a While, Do-while and for loops in a program. UML: - scnr : Scanner - rows : int - MAX_ASCII : int ------------------------------------------------------------------------------- + CharacterTables() : + CharacterTables(rows : int) : - getStartingValue() : int -displayTable(startValue : int) : void - userContinue() : boolean + main(args : String[]) : void In Section 5.10 we saw that there is a close relationship between char and int. Variables of type char store numbers. (Actually, variables of all types...

  • Breaking it Down Problem One problem that I find in new Java programmers is a tendency...

    Breaking it Down Problem One problem that I find in new Java programmers is a tendency to put too much code in "main()". It is important to learn how to break a program down into methods and classes. To give you practice at this, I have a program that has too much code in main. Your job will be to restructure the code into appropriate methods to make it more readable. I will provide you with lots of coaching on...

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having...

    cs55(java) please Part 1: You are a programming intern at the student transfer counselor's office. Having heard you are taking CS 55, your boss has come to ask for your help with a task. Students often come to ask her whether their GPA is good enough to transfer to some college to study some major and she has to look up the GPA requirements for a school and its majors in a spreadsheet to answer their question. She would like...

  • Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In...

    Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and cannot figure it out. Thank you. Online shopping cart (continued) (Java) Hello, I need help with Java to figure this out. In my Shopping Cart Manager Class (Bottom Code), I get "Resource leak: 'sc' is never closed." I have tried multiple things and...

  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • Can you please help me with creating this Java Code using the following pseudocode? Make Change C...

    Can you please help me with creating this Java Code using the following pseudocode? Make Change Calculator (100 points + 5 ex.cr.)                                                                                                                                  2019 In this program (closely related to the change calculator done as the prior assignment) you will make “change for a dollar” using the most efficient set of coins possible. In Part A you will give the fewest quarters, dimes, nickels, and pennies possible (i.e., without regard to any ‘limits’ on coin counts), but in Part B you...

  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT