Академический Документы
Профессиональный Документы
Культура Документы
Spring 2018
Lab-11 Manual
Iterations and Random Numbers
v1.0
6/3/2018
Lab-11 Manual 2018
Write a program that reads a set of floating-point values. Use the sentinel value of -999 to stop
the input. Ask the user to enter the values (prompting only a single time for the values), then
print:
• the average of the values.
• the smallest of the values.
• the largest of the values.
• the range, that is the difference between the smallest and largest.
Page 1
Lab-11 Manual 2018
Write a program that reads a word and prints each character of the word on a separate line. For
example, if the user provides the input "Harry", the program prints:
H
a
r
r
y
HINT: Use nextLine() method of Scanner to get the String input. Use the length() method of
String to find the total number of characters in the String, and then use the chatAt(index)
method of String to get the single character input.
For example:
Page 2
Lab-11 Manual 2018
Write a program that reads a word and prints the word in reverse. For example, if the user
provides the input "Harry", the program prints:
yrraH
Page 3
Lab-11 Manual 2018
Write a program that reads a word and prints the number of vowels in the word. For this
exercise, assume that a e i o u y are vowels. For example, if the user provides the input "Harry",
the program prints:
2 vowels.
Page 4
Lab-11 Manual 2018
Write a program that reads a number and prints all of its binary digits: Print the remainder
number % 2, then replace the number with number / 2. Keep going until the number is 0.
For example, if the user provides the input 11, the output should be:
1011
Page 5
Lab-11 Manual 2018
In this task, you are being asked to write a nested loop in Java.
Page 6
Lab-11 Manual 2018
Write a program that prompts the user for an integer and then prints out all prime numbers up to
that integer.
For example, when the user enters 20, the program should print:
2 3 5 7 11 13 17 19
Recall that a number is a prime number if it is not divisible by any number except 1 and itself.
Page 7
Lab-11 Manual 2018
In this task, you are being asked to understand and use the class Random for generating random
numbers in Java.
Random Numbers
import java.util.Random;
Random randomNumbers = new Random();
Page 8
Lab-11 Manual 2018
In this task, you are being asked to use the Random class and write a number guessing game.
Write a program that generates a random number between 1 and 100 and asks the user to guess
what the number is. If the user’s guess is higher than the random number, the program should
display “Too high, try again.” If the user’s guess is lower than the random number, the program
should display “Too low, try again.” The program should use a loop that repeats until the user
correctly guesses the random number.
Page 9