Question

In: Computer Science

Write a well-commented java program that demonstrates the use of loops, and generation of random integers...

Write a well-commented java program that demonstrates the use of loops, and generation of random integers (Code will be written in Dr. Java)

  1. You are taking some time off from your paint business and currently are on vacation in Bahamas.
  2. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers).
  3. You cannot use arrays (even if you know what they are) to store these numbers.
  4. It then picks up the largest number among them and prints a multiplication table until that number.
  5. While your program is generating random numbers, it keeps track of the number of 6’s generated.
    1. If more than two 6’s are generated, your program should print “HURRAY!! DOUBLE SIXES GENERATED”.
    2. If two 6’s are not generated, it should print “SORRY!! DOUBLE SIXES NOT ENCOUNTERED”.

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 ***************\

Solutions

Expert Solution

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!


Related Solutions

Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods...
Assignment Purpose Write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht can olny sepll a wrod one way.”...
Assignment Purpose The purpose of this lab is to write a well-commented java program that demonstrates...
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 You are taking some time off from your paint business and currently are on vacation in Bahamas. You decide to write a Java program that generates 10 random numbers between 1 and 20 (all integers). You cannot use arrays (even if you know what they are) to store these numbers. It then picks up...
Write a java program that will ask the user to enter integers (use loops) until -100...
Write a java program that will ask the user to enter integers (use loops) until -100 is entered. The program will find and display the greatest, the smallest, the sum and the average of the entered numbers. also write a separate driver class which will be used to run and display the greatest, the smallest, the sum and the average of the entered numbers. Thanks!!
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods.(Need Comment, Write by Java Code) Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use and re-use of methods with input validation. Instructions It is quite interesting that most of us are likely to be able to read and comprehend words, even if the alphabets of these words are scrambled (two of them) given the fact that the first and last alphabets remain the same. For example, “I dn'ot gvie a dman for a man taht...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of two dimensional arrays, input validation, and methods. (Write by Java Code, Need Comment) Instructions A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: Seat Ticket Price 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10...
Assignment Purpose The purpose of this lab is to write a well commented java program that...
Assignment Purpose The purpose of this lab is to write a well commented java program that demonstrates the use of one dimensional arrays and methods. Instructions Write a method rotateArray that is passed to an array, x, of integers (minimum 7 numbers) and an integer rotation count, n. x is an array filled with randomly generated integers between 1 and 100. The method creates a new array with the items of x moved forward by n Elements that are rotated...
***This is done with Java programming*** Write a well-documented (commented) program, “ISBN,” that takes a 9-digit...
***This is done with Java programming*** Write a well-documented (commented) program, “ISBN,” that takes a 9-digit integer as a command-line argument, computes the checksum, and prints the ISBN number. You should use Java’s String data type to implement it. The International Standard Book Number (ISBN) is a 10-digit code that uniquely specifies a book. The rightmost digit is a checksum digit that can be uniquely determined from the other 9 digits, from the condition that d1 + 2d2 +3d3 +...
Write a program in Java that initializes an array with ten random integers and then print...
Write a program in Java that initializes an array with ten random integers and then print three lines of output, containing: Every element at an odd index Every odd element All elements in reverse order   The program should use three different methods to implement the functionalities above. Call the primary source file ArrayManipulator.java
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT