Objectives
By the end of this program, the student will have demonstrated the ability to
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 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:
Notes
Grading Elements
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 + "\n"; for (int i = 0; i < sb.length(); i++) { strAndGauge += (i % 10); } return strAndGauge; } }
Explanation::
Code in JAVA::
StringUtils.java CODE:
public class StringUtils {
public static String genGauge(String sb) {
String strAndGauge = sb +
"\n";
for (int i = 0; i < sb.length();
i++) {
strAndGauge +=
(i % 10);
}
return strAndGauge;
}
public static String copy(String currentString, int
startPosition, int onePastLastPosition) {
String answer="";
for(int i=startPosition ;
i<onePastLastPosition;i++) {
answer=answer+currentString.charAt(i);
}
return answer;
}
public static String cut(String currentString, int
startPosition, int onePastLastPosition) {
String answer="";
for(int
i=0;i<currentString.length();i++) {
if(i<startPosition || i>=onePastLastPosition) {
answer=answer+currentString.charAt(i);
}
}
return answer;
}
public static String paste(String currentString, int
insertBefore, String stringToInsert) {
String answer="";
for(int
i=0;i<currentString.length();i++) {
if(i==insertBefore) {
for(int j=0;j<stringToInsert.length();j++)
{
answer=answer+stringToInsert.charAt(j);
}
}
answer=answer+currentString.charAt(i);
}
return answer;
}
}
StringMangle.java CODE:
import java.util.Scanner;
public class StringMangle {
public static void main(String[] args) {
/**
* Scanner class object named sc is
declared and it is used to take input
* from the user
* */
Scanner sc=new
Scanner(System.in);
/**
* A String named currentString is
declared below
* And also another String named
clipboard is also declared
* */
String
currentString,clipboard="";
/**
* Prompting user to enter the
string and it is stored in currentString
* */
System.out.println("Enter a
string");
currentString=sc.next();
/**
* Printing once the
currentString
* */
System.out.println("The current
string is");
System.out.println(StringUtils.genGauge(currentString));
/**
* Three integer named
startPosition, onePastLastPosition and insertBefore
* are declared below
* */
int startPosition,
onePastLastPosition, insertBefore;
/***
* Following while loop runs until
user do not enter 'q' to end the loop
*/
while(true) {
/**
* Prompting user
to enter the choice of user
* */
System.out.println("Enter c to copy, x to cut, v to paste, q to
quit");
char
ch=sc.next().charAt(0);
/**
* Converting the
ch char to lower case
* */
ch=Character.toLowerCase(ch);
if(ch=='q')
{
/**
* If user choice is q then we break the
loop
* */
break;
}else if(ch=='c'
|| ch=='x') {
/**
* Prompting user to enter the starting
position
* and then one past the ending position
* */
System.out.println("Enter the starting
position");
startPosition=sc.nextInt();
System.out.println("Enter one past the ending
position");
onePastLastPosition=sc.nextInt();
if(ch=='c') {
/**
* Calling the copy method and
passing the parameters
* Returned value is stored in
clipboard
* */
clipboard=StringUtils.copy(currentString, startPosition,
onePastLastPosition);
}else {
currentString=StringUtils.cut(currentString, startPosition,
onePastLastPosition);
}
}else
if(ch=='v') {
/**
* Prompting user to enter the position the paste
should come before
* */
System.out.println("Enter the position the paste
should come before");
insertBefore=sc.nextInt();
/**
* Calling paste method and storing the returned
value in currentString
* */
currentString=StringUtils.paste(currentString,
insertBefore, clipboard);
}
/**
* Printing
currentString and clipboard
**/
System.out.println("The current string is");
System.out.println(StringUtils.genGauge(currentString));
System.out.println("The clipboard is");
System.out.println(clipboard+" and is has length of
"+clipboard.length());
}/*While ends here*/
}
}
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 is 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 is 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 is 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 is 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 is has length of 2
Enter c to copy, x to cut, v to paste, q to quit
q
Please provide the feedback!!
Thank You!!
Objectives By the end of this program, the student will have demonstrated the ability to Write...
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...
I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...
Project Objectives: To develop ability to write void and value returning methods and to call them -- Introduction: Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order. This also allows for efficiencies, since the method can...
**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...
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text.Ex: If the input is:Hello there Hey quitthen the output is:ereht olleH yeHThere is a mistake in my code I can not find. my output is : ereht olleH ereht olleHyeHHow can I fix this?I saw some people use void or the function reverse but we didnt...
Create a DataEntryException class whose getMessage() method returns information about invalid integer data. Write a program named GetIDAndAge that continually prompts the user for an ID number and an age until a terminal 0 is entered for both. If the ID and age are both valid, display the message ID and Age OK. Throw a DataEntryException if the ID is not in the range of valid ID numbers (0 through 999), or if the age is not in the range...
In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...
In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...
Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...
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...