# 2.12. Coding Practice¶

The coding problems below are mostly about Strings, but more problems on other topics in this unit will be added in the future.

Write the code to print a random number from 1 to 100. You can use Math.random() to get a value between 0 and not quite 1.

First multiply the output from Math.random() times 100 and then cast it to an integer. This will result in a random number from 0 to 99. Add one to make it from 1 to 100.

This is the answer to the previous question.

The following code should get the first letter of the first name, middle name, and last name and append (concatenate) them together and then return them all in lowercase. However, the code has errors. Fix the code so that it compiles and runs correctly.

Line 5 has an ending ' instead of ". Line 7 is missing a =. Line 8 has firstname, but it should be firstName. Remember that you should uppercase the first letter of each new word, after the first word, to make the variable name easier to read (use camel case). Line 9 has subString, but the method name is substring. Line 11 is missing a ).

This is the answer to the previous question.

The following code should print the first 3 letters of the string message all in lowercase letters. However, the code has errors. Fix the errors so that the code runs as intended.

Line 5 ends with : when it should be ;. Line 6 should be substring(0,3). Line 7 should be part not message. Line 8 should be System.out.println.

This is the answer to the previous question.

The following code starts with String firstNameCaps = ALEX; and should print Alex. Use the toLowerCase and substring methods to do accomplish this task.

Create a string that is all lowercase. Create a new string from a substring of the original string (first letter) and a substring of the rest of the string that is all lowercase (all except the first letter). Print that string.

This is the answer to the previous question.

The following code should remove the word “very ” (and following space) from the message and print the new message. You can use indexOf to find the position of a substring in your string. You can use substring to create a new string removing the word.

Use indexOf to find the position and then create a new message up to the pos and again after the target string.

This is the answer to the previous question.

The following code should replace lol in the message with laugh out loud and print the new message.
Use indexOf to find the position of the “lol” then create a new string from up to that position and append the “laugh out loud” and the substring after it.