Sunteți pe pagina 1din 10

GIFT School of Engineering

and Applied Sciences

Spring 2018

CS-104/114: Introduction to Programming /


Programming Fundamentals

Lab-11 Manual
Iterations and Random Numbers

v1.0
6/3/2018
Lab-11 Manual 2018

Task #1: Writing a loop

In this task, you are being asked to write a loop in Java.

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.

NOTE: Write the loop using any loop.

1. Create a program called Loops1Lab11.java.


2. Use a Scanner object for the input.
3. Correctly display appropriate messages.

Page 1
Lab-11 Manual 2018

Task #2: Writing a loop

In this task, you are being asked to write a loop in Java.

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:

String something = “GIFT University”;

//gets the number of characters


int length = something.length();

//print each character of a String


for(int i = 0; i < length; ++i)
System.out.println(something.charAt(i));

1. Create a program called Loops2Lab11.java.


2. Create appropriate variables and assign values using a Scanner object.
3. Correctly display appropriate messages.

Page 2
Lab-11 Manual 2018

Task #3: Writing a loop

In this task, you are being asked to write a loop in Java.

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

1. Create a program called Loops3Lab11.java.


2. Create appropriate variables and assign values using a Scanner object.
3. Correctly display appropriate messages.

Page 3
Lab-11 Manual 2018

Task #4: Writing a loop

In this task, you are being asked to write a loop in Java.

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.

1. Create a program called Loops4Lab11.java


2. Create appropriate variables and assign values using a Scanner object.
3. Correctly display appropriate messages.

Page 4
Lab-11 Manual 2018

Task #5: Writing a loop

In this task, you are being asked to write a loop in Java.

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

1. Create a program called BinaryDigitsLab11.java.


2. Create appropriate variables and assign values using a Scanner object.
3. Correctly display appropriate messages.

Page 5
Lab-11 Manual 2018

Task #6: Writing a nested loop

In this task, you are being asked to write a nested loop in Java.

Write a program that prints a multiplication table, like this:

1. Create a program called NestedLoopsLab11.java.


2. Correctly display appropriate messages.

Page 6
Lab-11 Manual 2018

Task #7: Writing a loop

In this task, you are being asked to write a loop in Java.

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.

1. Create a program called PrimesLab11.java.


2. Create appropriate variables and assign values using a Scanner object.
3. Correctly display appropriate messages.

Page 7
Lab-11 Manual 2018

Task #8: Random numbers

In this task, you are being asked to understand and use the class Random for generating random
numbers in Java.

Random Numbers

To generate random numbers, use the Random class as follows:

import java.util.Random;
Random randomNumbers = new Random();

// Get a random integer and assign it to number.


int number = randomNumbers.nextInt();

// Get a random integer between 0 and 99


number = randomNumbers.nextInt(100);

// Get a random integer between 1 and 10


number = randomNumbers.nextInt(10) + 1;

// Get a random integer between -50 and +49


number = randomNumbers.nextInt(100) - 50;

// Get a random real number between 0.0 and 1.0


double realNumber = randomNumbers.nextDouble();

1. Create a program called RandomNumbersLab11.java.


2. Copy the above code example and print each statement.
3. Try creating other examples of finding random integers.

Page 8
Lab-11 Manual 2018

Task #9: Random number guessing game

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.

1. Create a program called RandomGuessGameLab11.java.


2. Create appropriate variables and assign values using a Scanner object.
3. Correctly display appropriate messages.

Page 9