Java Reference
In-Depth Information
4. Write a method called randomX that prints a lines that contain a random number of “x” characters (between 5 and 20
inclusive) until it prints a line that contains 16 or more characters. For example, the output might look like the following:
xxxxxxx
xxxxxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxxxx
xxxxxx
xxxxxxxxxxx
xxxxxxxxxxxxxxxxx
5. Write a method called randomLines that prints between 5 and 10 random strings of letters (between “a” and “z”), one
per line. Each string should have random length of up to 80 characters.
6. Write a method called makeGuesses that guesses numbers between 1 and 50 inclusive until it makes a guess of at
least 48 . It should report each guess and at the end should report the total number of guesses made. Here is a sample
execution:
guess = 43
guess = 47
guess = 45
guess = 27
guess = 49
total guesses = 5
7. Write a method called diceSum that accepts a Scanner for the console as a parameter and prompts for a desired
sum, then repeatedly simulates the rolling of 2 six-sided dice until their sum is the desired sum. Here is a sample dia-
logue with the user:
Desired dice sum: 9
4 and 3 = 7
3 and 5 = 8
5 and 6 = 11
5 and 6 = 11
1 and 5 = 6
6 and 3 = 9
8. Write a method called randomWalk that performs steps of a random one-dimensional walk. The random walk
should begin at position 0. On each step, you should either increase or decrease the position by 1 (each with equal
probability). Your code should continue making steps until a position of 3 or
3 is reached, and then report the max-
imum position that was reached during the walk. The output should look like the following:
position = 1
position = 0
position = -1
position = -2
position = -1
position = -2
position = -3
max position = 1
Search WWH ::




Custom Search