jaredec18

Untitled

Sep 24th, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. Program screenshots:
  2. https://media.cheggcdn.com/media%2F0e5%2F0e55792c-2a1f-4990-a6b4-ae828d65debf%2Fphp5Xs0c7.png
  3.  
  4. https://media.cheggcdn.com/media%2F5e3%2F5e399620-3abb-4e46-bcc8-f9af7884f27a%2Fphpa1cTgR.png
  5.  
  6. https://media.cheggcdn.com/media%2Fed3%2Fed39192b-08fa-469a-a4de-cf3e8526200f%2FphpuYksBg.png
  7.  
  8. https://media.cheggcdn.com/media%2Fdfa%2Fdfac9b58-90cd-4ded-997c-c520696d3ac6%2Fphpdy1LT6.png
  9.  
  10. Sample output:
  11. https://media.cheggcdn.com/media%2F9cc%2F9cc03563-c452-43fc-a73d-97ae0ee2ee2d%2Fphpo3HFVf.png
  12.  
  13. Code to be copied:
  14. //import required packages
  15. import java.util.Scanner;
  16. //Declare the class SillyString
  17. public class SillyString
  18. {
  19. //Main method
  20. public static void main(String[] args)
  21. {
  22. //Declare local variables
  23. String last_name,unit,vegetable,original_quote,replaced_quote;
  24.  
  25. //Classic Tongue Twister
  26. original_quote = "Peter Piper picked a peck of pickled peppers." ;
  27. //Create a scanner object
  28. Scanner sc = new Scanner(System.in);
  29.  
  30. //Prompt and read the last name
  31. System.out.print("Enter your last name:");
  32. last_name = sc.nextLine();
  33. //Prompt and read the unit of measurement
  34. System.out.print("Enter a unit of measurement:");
  35. unit = sc.nextLine();
  36.  
  37. //remove the spaces
  38. unit = unit.trim();
  39.  
  40. //converts to lowercase if any upper case is present
  41. unit = unit.toLowerCase();
  42.  
  43. //prompt the vegetable
  44. System.out.print("Enter the name of a vegetable:");
  45. vegetable = sc.nextLine();
  46.  
  47. //remove the spaces
  48. vegetable = vegetable.trim();
  49. //convert to lower case
  50. vegetable = vegetable.toLowerCase()+"s";
  51. //call the test case
  52. testProperCase();
  53.  
  54. //Test the real input
  55. last_name = properCase(last_name);
  56. //displaying all the finalised input strings
  57. System.out.println("\nFinalised Last name : "+last_name);
  58. System.out.println("Finalised Unit : "+unit);
  59. System.out.println("Finalised veggie : "+vegetable);
  60. //replacing all the string in the original Tongue
  61. //Twister with the inputted formatted strings
  62. replaced_quote = original_quote.replace("Piper",last_name);
  63. replaced_quote = replaced_quote.replace("peck",unit);
  64. replaced_quote = replaced_quote.replace("peppers",vegetable);
  65. //displaying both Original and Replaced Tongue Twisters
  66. System.out.println("\nOriginal Tongue Twister : "
  67. +original_quote);
  68. System.out.println("Replaced Tongue Twister : "
  69. +replaced_quote);
  70. }//end of main method
  71. //Implement the method properCase
  72. public static String properCase(String name)
  73. {
  74. //declare local variables
  75. String lastname="",secondname="";
  76. int space;
  77. //returns the name with the first letter
  78. //in upper case and the rest lower case
  79. lastname = name.substring(0, 1).toUpperCase()
  80. + name.substring(1).toLowerCase();
  81.  
  82. //Remove the space starting and ending
  83. lastname = lastname.trim();
  84. //get the index of the space
  85. space = name.indexOf(" ");
  86.  
  87. //For multiple words
  88. if(space != -1)
  89. {
  90. //Get the string
  91. secondname = lastname.substring(space);
  92.  
  93. //make the 2nd word (which is present after space) to lower case
  94. lastname = lastname.substring(0,space)+secondname.toLowerCase();
  95. }
  96. //return the name
  97. return lastname;
  98. }
  99. //Runs tests on the properCase method.
  100. public static void testProperCase()
  101. {
  102. //Test cases
  103. System.out.println("Test Case 1 zHang = "
  104. +properCase("zHang"));
  105. System.out.println("Test Case 2 zHang = "
  106. +properCase(" zHang "));
  107. System.out.println("Test Case 3 z_Hang = "
  108. +properCase("z_Hang"));
  109. System.out.println("Test Case 4 zHang Piper = "
  110. +properCase("zHang Piper"));
  111. }
  112. }//end of class
Advertisement
Add Comment
Please, Sign In to add comment