Question

In: Computer Science

You are to write a class StringPlay that has only a main method. This class contains...

You are to write a class StringPlay that has only a main method. This class contains all interaction with the user.

The main method

  • Uses a Scanner to accept user input
  • Maintains a “currentString
  • Uses a sentinel-controlled loop to accept multiple user commands, or exit
  • The program displays a message asking the user to enter one of the following commands
    • a – enter a new value for current
    • b – padLeft asks the user for the number of characters to add to the start of the current string, and the character to use for the padding
    • c – padRight asks the user for the number of characters to add to the end of the current string, and the character to use for the padding
    • d – zapLeft asks the user for the number of characters to remove from the start of the current string
    • e – zapRight asks the user for the number of characters to remove from the end of the current string
    • f – exits the program
  • The loop body performs its actions using the appropriate method from StringUtils.
  • After the action is completed, the loop displays the current string and prompts for another command.

Notes

Use Scanner’s next method for the “e” command; we will limit our user input to have no spaces or other whitespace. Similarly, padding characters will not be whitespace. You may assume valid user input.

Solutions

Expert Solution

Sample Output:

The screenshots are attached below for reference.

Please follow them for proper output.

Program code to copy:

import java.util.*;
public class StringPlay{
public static void main (String[] args) {
Scanner inp=new Scanner(System.in);
String current="";
System.out.println("Enter the current string:");
current=inp.next();//read current string
char ch='a';
int n=0;
while(ch!='f'){
System.out.println("The current string is:"+current);//print current string
System.out.println("a-Enter a new value for current:");
System.out.println("b-padLeft");//print menu options
System.out.println("c-padRight");
System.out.println("d-zapLeft");
System.out.println("e-zapRight");
System.out.println("f-exit");
System.out.println("Enter choice:");
ch=inp.next().charAt(0);//read choice from user
if(ch=='a'){
System.out.println("Enter the value for current string:");
current=inp.next();
}
else if(ch=='b'){
System.out.println("Enter number of characters to add:");
n=inp.nextInt();
System.out.println("Enter character:");//read input from user
ch=inp.next().charAt(0);
String res="";
for(int i=0;i<n;i++){
res=res+ch;//create a new string and add characters at front
}
res+=current;
current=res;///update current with res string
}
else if(ch=='c'){
System.out.println("Enter number of characters to add:");
n=inp.nextInt();
System.out.println("Enter character:");
ch=inp.next().charAt(0);//read input from user
for(int i=0;i<n;i++){
current=current+ch;//add characters
}
}
else if(ch=='d'){
System.out.println("Enter number of characters to remove:");
n=inp.nextInt();//read input from user
n=n-1;
String res="";
for(int i=n;i<current.length();i++){
res=res+current.charAt(i);//add charaters to res String from current string after n positions
}
current=res;
}
else if(ch=='e'){
System.out.println("Enter number of characters to remove:");
n=inp.nextInt();
String res="";
for(int i=0;i<current.length()-n;i++){
res=res+current.charAt(i);//add characters to res from current string till n characters from last
}
current=res;
}
else{
break;
}
  
}
}
}


Related Solutions

This class contains the main method. In this class you should: Ask the user for the...
This class contains the main method. In this class you should: Ask the user for the name, job tilte, and salary. Instantiate a Employee object with those parameters. Use the accessor methods to check the value of all three instance variables and print them. Ask the user to enter a percentage to raise the salary by. Use the raise method to increase the salary. Use the salary accessor check the new value of salary and print it.
Write a program that contains a main method and another method named userName. The main method...
Write a program that contains a main method and another method named userName. The main method should prompt the user to enter a full name. The userName method takes the full name as an argument and prints the name in reverse order and returns the number of characters in the name. See Sample Output (input shown in blue). Sample Output Please enter your FULL name Billy Joe McCallister Here is the name Billy Joe McCallister in reverse: retsillaCcM eoJ ylliB...
Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
write JAVA program have a public class named GeometricShapes that has the main() method. In the...
write JAVA program have a public class named GeometricShapes that has the main() method. In the main() method the user needs to select if he want 2D shapes or 3D shape -if user select 2D user needs to select the shape type (Square, Circle, or Triangle) after selected shape the user needs to specify whether to find the Area or the Perimeter or to find side-length (radius for the circles), accordingly the needed constructor is used. (using Polymorphism principle) -if...
Java Class Create a class with a main method. Write code including a loop that will...
Java Class Create a class with a main method. Write code including a loop that will display the first n positive odd integers and compute and display their sum. Read the value for n from the user and display the result to the screen.
Write a public class call CountInts. class CountInts has a main method. CountInts must have two...
Write a public class call CountInts. class CountInts has a main method. CountInts must have two methods: count and displayResults. 1. method count takes an array, aa, of ints and counts how many times each integer appears in aa. The integers can only be from 0 to 100 inclusive. 2. method display results displays the results of the count 3. use the template provided. insert your code in the places indicated and don't change anything else. Examples % java CountInts...
Given the main method of a driver class, write a Fraction class. Include the following instance...
Given the main method of a driver class, write a Fraction class. Include the following instance methods: add, multiply, print, printAsDouble, and a separate accessor method for each instance variable. Write a Fraction class that implements these methods: add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction. multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction. print ─ This method prints the...
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT