Lab Tutorial 4
Lab Tutorial 4
Fall 2016
Lab 4
LOOPS
In the last lab we covered Control Statements. A loop is basically a control statement. It makes the
use of boolean expressions to control the execution of code.
It is a way to go over several lines of code repeatedly, in order- that is from top to bottom.
There are three kinds of loops:
1. The while loop
2. The do-while loop
3. The for loop
This loop runs the code in the //do something block until the boolean_expression given to it returns
false.
Let us now look at a simple example. Let’s say we have to print numbers from 1 to 10. This can be
done using a while loop as follows:
int n = 0;
while (n<10)
{
n++;
System.out.println(n);
}
Now that you’ve looked at an example of the while loop,try this out on your own:
Activity1:
Declare a variable n and initialise it to 5.
Write a program with a method factorial in class Calculate that takes in an integer number n
and calculates the factorial for this number using a while loop. Use the main function to call
this method and print the factorial of n.
Now let us look at the do while loop. This loop is very similar to the while loop with the only
difference being that the code within the loop executes at least once before the loop condition is
checked.
It can be seen that the only difference between the while and the do-while loop is that of structure. In
the case of the former the code to be executed is typed up after the condition and in the latter it is
written before the actual condition.
int n = 0;
do
{
n++;
System.out.println(n);
}while(n<10);
Activity 2:
The Fibonacci numbers form what we call a recurrent sequence. Beginning with 1, each term
of the Fibonacci sequence is the sum of the two preceding numbers.
Using a do-while loop, create a program which produces the first 10 integers in the Fibonacci
sequence.
COMP 295 JAVA Programming
Fall 2016
The for loop is somewhat different compared to the while and do while control structures. Lets us
look at the syntax for a typical for loop:
The following program prints the first ten integers upto 10.
Activity 3:
Make a program that prints the following pattern using a for loop:
*
**
***
****
*****
******
*******
To generate a random number, you must first create an Object of the Random class and then create a
variable to store the value generated. This is done as follows:
import java.util.*;
}
}
The arguments passed to the method represent the range starting from zero. In the example given
above, the method generates numbers from 0-2 and then adds 2 to it. This means that the range is
from 2 to 4. For negative numbers you may subtract. The following data types may be produced
randomly using this method:
boolean nextBoolean()
Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's
sequence.
Generates random bytes and places them into a user-supplied byte array.
double nextDouble()
Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random
number generator's sequence.
float nextFloat()
Returns the next pseudorandom, uniformly distributed float value between 0.0 and 1.0 from this random
COMP 295 JAVA Programming
Fall 2016
double nextGaussian()
Returns the next pseudorandom, Gaussian ("normally") distributed double value with mean 0.0 and
standard deviation 1.0 from this random number generator's sequence.
int nextInt()
Returns the next pseudorandom, uniformly distributed int value from this random number generator's
sequence.
int nextInt(int n)
Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value
(exclusive), drawn from this random number generator's sequence.
long nextLong()
Returns the next pseudorandom, uniformly distributed long value from this random number generator's
sequence.
Sets the seed of this random number generator using a single long seed.
Activity 4:
Make a class called NumberGenerator. Create and initialise variables as follows:
int start;
int end;
int n;
Create a method that generates random numbers n between start and end , saves them in a
string and returns this string. Call this function in main and print the results.
Activity 5:
In this activity you will design a two player game for rock paper scissors. Assign an integer
value to the three with 0 for rock, 1 for paper and 2 for scissors. Use loop and print each value
generated. Ensure the game does not end until a player wins.