In: Computer Science
Write a well-commented java program that demonstrates the use of loops, and generation of random integers (Code will be written in Dr. Java)
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 ***************\
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments(read them for better understanding)
.
Images of the Code:
Note: If the below code is missing indentation please refer code Images
Typed Code:
public class Bahamas
{
public static void main(String[] args) {
// a value is used to store random numbers
//max and min values used to store range of random
numbers
//c is used for count of "6" and high is used to store
largest number among all random numbers
int a = 0,max = 20, min = 1, c = 0, high = 0;
System.out.println("/**************************Bahamas Vacation
Report BEGIN**********************\\");
System.out.println("Generating
random numbers……");
//for loop will iterate till 10
times because we need 10 random variables
//i value initialized to 0 and
increment till it reaches 10
for(int i = 0; i< 10;i++)
{
//storing random value in a
//math package is used for random
numbers
//math.random number give the
double value of greater than 0.0 and less than 1.0
//for example :- random number =
0.9 , max = 20 , min = 1
// (0.9 *(20 - 1)) + 1 ==> 17.1
+ 1 ==> 18.1
//(int) 18.1 ==> 18
a = (int) ((Math.random() * (max -
min)) + min);
//printing the random number
System.out.print(a +" ");
//if the random number is 6
if(a == 6)
{
//count will be increased by
1
c++;
}
//initially high = 0, if high <
random number
if(high < a)
{
//then the random number is stored
in high
//largest number is stored at the
end
high = a;
}
}
System.out.println();
//if c is greater than or equal to
2
if(c >= 2)
{
//printing the line
System.out.println("HURRAY!! DOUBLE
SIXES GENERATED");
}
//else
else
{
//printing this line
System.out.println("SORRY!! DOUBLE
SIXES NOT ENCOUNTERED");
}
//printing largest random
number
System.out.println("Largest random
number is: "+high);
System.out.println("The
multiplication table is as follows:");
//initially j = 1
//for loop will iterate till j
becomes 10
for(int j = 1; j<= 10;
j++)
{
//printing 1 to 10
System.out.print(j+"\t");
}
//initially k = 2
//for loop will iterate till k
becomes largest random number
for(int k = 2; k<= high;
k++)
{
//jumping to next line
System.out.println();
//printing the number
System.out.print(k+"\t");
//initially j = 2
//for loop will iterate till j
becomes 10
for(int j = 2; j<= 10;
j++)
{
//printing the multiplication of
two numbers
System.out.print((k*(j))+"\t");
}
}
System.out.println();
System.out.print("/**************************Bahamas Vacation
Report END **********************\\");
}
}
//code ended here
Output 1 :
Output 2:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!