Lab A

Count the number of times one String occurs in a second String.

Requirements:

  • You must use the Scanner class nextLine() method, to read in two words from the keyboard.
  • You must extract the 2 words and put the values into two String variables.
  • You must use the 2-parameter indexOf() method and a while loop to step through the second String to count the number of times the first String occurs in the second String.
  • You must print a message stating your findings. For example, if the first word was "ack" and the second was "backtackacka", you would print this: "The word "ack" was found in "backtackacka" 3 times.
  • Your solution must work for any combination of Strings with a length greater than 0.

Lab B

Manipulate upper and lower case letters in a String and String representations of numeric values.

Requirements:

  • Use the Scanner class nextLine() method, to read in a sentence from the keyboard. Your code will manipulate the sentence in the following ways and then print out the resulting sentence:
    • For each word in the sentence, if the first letter is capitalized, your code must use the toUpperCase() method to capitalize each letter in the word in the resulting sentence
    • For each word in a sentence, if the first letter is not capitalized, your code must use the toLowerCase() method to make sure each letter in the word is not be capitalized in the resulting sentence.
    • For each integer number in the sentence, you must use the parseInt() method to store the value in a integer variable.
    • For each decimal number in the sentence, you must use the parseDouble() method to store the value in a decimal variable.
  • The resulting sentence must be created by concatenating the numeric variables and the words from the sentence.
  • The original sentence must contain words, an integer (read in as a String) and a decimal number (also read in as a String).
  • Some but not all of the words must be capitalized.
  • Your solution must work for any sentence entered.

Lab C

Explore how the compareTo() method works on numeric values read in as numbers but converted to Strings.

Requirements:

  • Using the nextInt() and nextDouble() Scanner class methods, read in an integer and a decimal value from the keyboard.
  • Using the valueOf() method, convert both these numeric values into Strings.
  • You must prompt yourself for a prediction of the result when the 2 String numeric representations are compared using the compareTo() method and then to print out the result of the compareTo() method in a sentence stating if your prediction was right.
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.