In: Computer Science
Assignment Purpose
The purpose of this lab is to write a well-commented java program that demonstrates the use of loops, and generation of random integers.
Instructions
Sample Output 1
/*******************Bahamas Vacation Report BEGIN***************\
Generating random numbers……
4 6 3 11 9 2 6 20 11 3
“HURRAY!! DOUBLE SIXES GENERATED”
Largest random number is: 11
The multiplication table is as follows:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
.
.
11 22 33 44 55 66 77 88 99 110
/*******************Bahamas Vacation Report END ***************\
Sample Output 2
/*******************Bahamas Vacation Report BEGIN***************\
Generating random numbers……
4 6 3 11 9 2 2 20 11 3
“SORRY!! DOUBLE SIXES NOT ENCOUNTERED”
Largest random number is: 11
The multiplication table is as follows:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
.
.
11 22 33 44 55 66 77 88 99 110
/*******************Bahamas Vacation Report END ***************\
A complete functional program is given below in java. Also a screenshot of output is attached below.
import java.io.*;
import java.util.*;
public class Main{
public static void main(String args[])
{
//creating object of random class
Random rnd = new Random();
//creating a vector to store random numbers
Vector <Integer> v = new Vector();
int min=1,max=20,count=0;
System.out.println("*******************Bahamas Vacation Report
BEGIN***************");
System.out.println("Generating random numbers……");
/*this loop will generate random numbers between 1 and 20
add the generated random number in the vector
and check if the number is equal to 6, if yes: counter ++ */
for(int i=0;i<10;i++)
{
int rand = rnd.nextInt((max-min)+1)+min;
v.add(rand);
if(rand==6){
count++;
}
}
//printing the random numbers
//toString() and replace() methods are used to print
//vector elements without "[]" and comma.
System.out.println(v.toString().replace("[","").replace("]","").replace(",",""));
//checking if no of sixes is>2
if(count>=2)
{
System.out.println(" “HURRAY!! DOUBLE SIXES GENERATED” ");
}
else{
System.out.println(" “SORRY!! DOUBLE SIXES NOT ENCOUNTERED”
");
}
//finding and printing the maximum from random numbers
int largest = Collections.max(v);
System.out.println("Largest random number is: "+largest);
//this loop will print the tables from 1 to largest number
for(int i=1;i<=largest;i++)
{
for(int j=1;j<=largest;j++)
{
System.out.print(i*j+"\t");
if(j>largest)
break;
}
System.out.println();
}
System.out.println("*******************Bahamas Vacation Report END
***************");
}
}