You are on page 1of 6

COMP 295 JAVA Programming

Fall 2016

FORMAN CHRISTIAN COLLEGE (A Chartered University)


COMP 295 Java Programming
Fall 2016
Section : A
Instructor: Tahir Javaid

Lab 4

In this lab we will be covering the following:


● Loops
● Pseudorandom numbers
● RandomGenerator class

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

First, let’s look at the while loop.

The basic syntax for the while loop is as follows:


while (boolen_expression)
{
//do something
}
COMP 295 JAVA Programming
Fall 2016

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.

Here is the syntax:


do
{
//do something
}while(boolean_expression);
COMP 295 JAVA Programming
Fall 2016

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.

Let’s look at the same example with a do-while loop:

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.

0+1=1 1+1=2 1+2=3 2+3=5 3+5=8 5+8=13

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:

Now let’s look at an example:

The following program prints the first ten integers upto 10.

for(int i =1; i<=10; i++)


{
System.out.println(i);
}

Activity 3:
Make a program that prints the following pattern using a for loop:

*
**
***
****
*****
******
*******

PSEUDORANDOM NUMBERS AND RANDOM GENERATOR CLASS

In Java, the RandomGenerator class is used to generate pseudorandom numbers.

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.*;

public class Numbers {


COMP 295 JAVA Programming
Fall 2016

public static void main(String[] args) {

Random n = new Random(); //creating an instance of the random class

int x = n.nextInt(3)+2; // saving the value generated


System.out.println(x); //printing the random value

}
}

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:

Modifier and Type Method and Description

protected int next(int bits)

Generates the next pseudorandom number.

boolean nextBoolean()

Returns the next pseudorandom, uniformly distributed boolean value from this random number generator's
sequence.

void nextBytes(byte[] bytes)

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

number generator's sequence.

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.

void setSeed(long seed)

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.

You might also like