Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Program screenshots:
- https://media.cheggcdn.com/media%2F0e5%2F0e55792c-2a1f-4990-a6b4-ae828d65debf%2Fphp5Xs0c7.png
- https://media.cheggcdn.com/media%2F5e3%2F5e399620-3abb-4e46-bcc8-f9af7884f27a%2Fphpa1cTgR.png
- https://media.cheggcdn.com/media%2Fed3%2Fed39192b-08fa-469a-a4de-cf3e8526200f%2FphpuYksBg.png
- https://media.cheggcdn.com/media%2Fdfa%2Fdfac9b58-90cd-4ded-997c-c520696d3ac6%2Fphpdy1LT6.png
- Sample output:
- https://media.cheggcdn.com/media%2F9cc%2F9cc03563-c452-43fc-a73d-97ae0ee2ee2d%2Fphpo3HFVf.png
- Code to be copied:
- //import required packages
- import java.util.Scanner;
- //Declare the class SillyString
- public class SillyString
- {
- //Main method
- public static void main(String[] args)
- {
- //Declare local variables
- String last_name,unit,vegetable,original_quote,replaced_quote;
- //Classic Tongue Twister
- original_quote = "Peter Piper picked a peck of pickled peppers." ;
- //Create a scanner object
- Scanner sc = new Scanner(System.in);
- //Prompt and read the last name
- System.out.print("Enter your last name:");
- last_name = sc.nextLine();
- //Prompt and read the unit of measurement
- System.out.print("Enter a unit of measurement:");
- unit = sc.nextLine();
- //remove the spaces
- unit = unit.trim();
- //converts to lowercase if any upper case is present
- unit = unit.toLowerCase();
- //prompt the vegetable
- System.out.print("Enter the name of a vegetable:");
- vegetable = sc.nextLine();
- //remove the spaces
- vegetable = vegetable.trim();
- //convert to lower case
- vegetable = vegetable.toLowerCase()+"s";
- //call the test case
- testProperCase();
- //Test the real input
- last_name = properCase(last_name);
- //displaying all the finalised input strings
- System.out.println("\nFinalised Last name : "+last_name);
- System.out.println("Finalised Unit : "+unit);
- System.out.println("Finalised veggie : "+vegetable);
- //replacing all the string in the original Tongue
- //Twister with the inputted formatted strings
- replaced_quote = original_quote.replace("Piper",last_name);
- replaced_quote = replaced_quote.replace("peck",unit);
- replaced_quote = replaced_quote.replace("peppers",vegetable);
- //displaying both Original and Replaced Tongue Twisters
- System.out.println("\nOriginal Tongue Twister : "
- +original_quote);
- System.out.println("Replaced Tongue Twister : "
- +replaced_quote);
- }//end of main method
- //Implement the method properCase
- public static String properCase(String name)
- {
- //declare local variables
- String lastname="",secondname="";
- int space;
- //returns the name with the first letter
- //in upper case and the rest lower case
- lastname = name.substring(0, 1).toUpperCase()
- + name.substring(1).toLowerCase();
- //Remove the space starting and ending
- lastname = lastname.trim();
- //get the index of the space
- space = name.indexOf(" ");
- //For multiple words
- if(space != -1)
- {
- //Get the string
- secondname = lastname.substring(space);
- //make the 2nd word (which is present after space) to lower case
- lastname = lastname.substring(0,space)+secondname.toLowerCase();
- }
- //return the name
- return lastname;
- }
- //Runs tests on the properCase method.
- public static void testProperCase()
- {
- //Test cases
- System.out.println("Test Case 1 zHang = "
- +properCase("zHang"));
- System.out.println("Test Case 2 zHang = "
- +properCase(" zHang "));
- System.out.println("Test Case 3 z_Hang = "
- +properCase("z_Hang"));
- System.out.println("Test Case 4 zHang Piper = "
- +properCase("zHang Piper"));
- }
- }//end of class
Advertisement
Add Comment
Please, Sign In to add comment