3.13. Coding Practice¶
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.
Answer: 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 )
.
Answer: 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 name1 = ALEX;
and should print Alex
. Use the toLowerCase
and substring
methods to do 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 using indexOf and substring.
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.
This is the answer to the previous question.
For more practice with Strings see problems at http://codingbat.com/java/String-1.
Here are some practice coding problems for Turtles.
Finish the code below to have t1
draw a triangle where all of the
sides are length 50.
Finish the code below to have t1
draw a rectangle. The vertical
sides should be length 50 and the horizontal length 100.
Finish the code below to have t1
draw the number seven.
Finish the code below to have t1
draw the number four.
Finish the code below to have t1
draw something interesting.