In: Computer Science
Create & name a file with the following format: Example: The instructor would create a file
with the following name: TonsmannGuillermoUnit5.java |
5% |
|
Style Components |
5% |
|
LastNameFirstNameUnit5.java - Main Method Purpose: This program will generate a series of pairs of random numbers and it will produce a table containing these values and some calculations with them. At the end some statistics will be printed. · The program should define format strings to output results as shown in the sample at the bottom. Every row in the table must be printed using the printf command. · The program will get from the user the maximum random number that can be computed (an integer). Initially, the program will get this value from the user as a String variable. · The program will then use the Integer wrapper class to parse the integer out of the String above and store it inside an integer variable. · Then the program will ask the user how many rounds of numbers will be generated (an integer). It should also receive this number from the user as a String and use the Integer wrapper class to get the integer value into another integer variable. · Using a for-loop, output a table with the following characteristics: o Print a header of titles using a header format and printf (this should be before the loop, not inside the loop). o A pair of random numbers between zero and the maximum random number to be computed will be generated at the beginning of the for-loop (known as first and second). o There should be one line in the table per each of these pair of random values generated. o Each line should contain the following information: * A line number, also known as round. * A smallest random number that was generated in the round (the smallest between first and second). * A largest random number that was generated in the round (the largest between first and second). * The absolute difference of the first and second random numbers. This can be computed with the help of the the Math.abs method. * The integer division of the largest random value over the smallest random value. * The remainder of the integer division of the largest random value over the smallest random value (also known as the modulus operation). After all the rows of the table have been generated, the program should print 4 values: the smallest random number generated in all rounds, the largest random generated in all rounds, the total of adding all random numbers generated and their average. All these values should be preceded by appropriate labels as shown in the example below. Mimic the output in the sample below exactly. Numbers will change due to random and selection but format will be the same. Students should not use Arrays or any other advanced concept that was not reviewed in the class to solve this assignment. |
||
Sample Below are 2 different random examples, based on the given input. Your output will vary but the format will be the same. Sample 1 Round Rand #1 Rand #2
Abs(-)
/ Mod The minimum generated random number is: 3 |
||
Sample 2 Round Rand #1 Rand #2
Abs(-)
/ Mod The minimum generated random number is: 1 |
Explanation:
Code in JAVA is given below
Please read comments for better understanding of the code
Output is given at the end of the code
------------------------------------------------------------------------
Code in JAVA::
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Random;
public class TonsmannGullermoUnit5 {
public static void main(String[] args) throws Exception{
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
/**
* Following we created an String variable named value which will be used to store
* the maximum random value to be generated.
*
* Another String variable named round is declared to store the number of rounds
* */
String value,round;
/**
* Prompting user to enter the maximum random number
* */
System.out.print("Please enter the maximum random number to be use: ");
value=br.readLine();
/**
* Prompting user to enter the number of rounds
* */
System.out.print("Please enter the number of rounds: ");
round=br.readLine();
/**
* Converting value to integer and storing in variable named maxRandom
* Converting round to integer and storing in variable named totalRound
* */
int maxRandom=Integer.parseInt(value);
int totalRound=Integer.parseInt(round);
/**
* Three integer variables named sum,minimum,maximum are declared below
* */
int sum=0,minimum=maxRandom+1,maximum=-1;
Random rand = new Random();
/**
* Printing the header line
* */
System.out.printf("%5s%10s%10s%10s%10s%10s\n","Round","Rand #1","Rand #2","Abs(-)","/","Mod");
for(int i=1;i<=totalRound;i++){
int n1,n2,min=maxRandom+1,max=-1;
n1= 1 + rand.nextInt(maxRandom);
n2= 1 + rand.nextInt(maxRandom);
if(n1<n2){
min=n1;
max=n2;
}else{
min=n2;
max=n1;
}
if(min<minimum){
minimum=min;
}
if(max>maximum){
maximum=max;
}
sum=sum+n1+n2;
System.out.printf("%5d%10d%10d%10d%10d%10d\n",i,min,max,Math.abs(min-max),max/min,max%min);
}
System.out.printf("The minimum generated random number is: %d\n",minimum);
System.out.printf("The maximum generated random number is: %d\n",maximum);
System.out.printf("The total of generated random numbers is %d\n",sum);
double average=(double)sum/(2*totalRound);
System.out.printf("The average of generated numbers is: %.2f\n",average);
}
}
OUTPUT::
------------------------------------------------------------------------
TEST CASE 1:
Please enter the maximum random number to be use: 6
Please enter the number of rounds: 2
Round Rand #1 Rand #2 Abs(-) / Mod
1 6 6 0 1 0
2 1 2 1 2 0
The minimum generated random number is: 1
The maximum generated random number is: 6
The total of generated random numbers is 15
The average of generated numbers is: 3.75
------------------------------------------------------------------------
TEST CASE 2:
Please enter the maximum random number to be use: 100
Please enter the number of rounds: 10
Round Rand #1 Rand #2 Abs(-) / Mod
1 64 92 28 1 28
2 13 80 67 6 2
3 54 80 26 1 26
4 72 91 19 1 19
5 3 97 94 32 1
6 5 43 38 8 3
7 24 70 46 2 22
8 54 87 33 1 33
9 8 92 84 11 4
10 3 25 22 8 1
The minimum generated random number is: 3
The maximum generated random number is: 97
The total of generated random numbers is 1057
The average of generated numbers is: 52.85
------------------------------------------------------------------------
TEST CASE 3:
Please enter the maximum random number to be use: 100
Please enter the number of rounds: 5
Round Rand #1 Rand #2 Abs(-) / Mod
1 41 88 47 2 6
2 66 87 21 1 21
3 23 65 42 2 19
4 42 54 12 1 12
5 2 52 50 26 0
The minimum generated random number is: 2
The maximum generated random number is: 88
The total of generated random numbers is 520
The average of generated numbers is: 52.00
------------------------------------------------------------------------
Please provide the feedback!!
Thank You!!