Question

In: Computer Science

Create & name a file with the following format: LastNameFirstNameUnit5.java. Example: The instructor would create a...

Create & name a file with the following format:

LastNameFirstNameUnit5.java.

Example: The instructor would create a file with the following name: TonsmannGuillermoUnit5.java

Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter.
Only submit the .java file needed to make the program run. Do not submit the .class file or any other file. Comments REQUIRED; flow charts & pseudocode NOT REQUIRED.

5%

Style Components
Include properly formatted prologue, comments, indenting, and other style elements as shown in Chapter 2 starting page 64 and Appendix 5 page 881-892.

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
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 3 78 75 26 0
    2 69 84 15       1 15
    3 40 97 57       2 17
    4 14 90 76       6 6
    5 9 59 50 6 5

The minimum generated random number is: 3
The maximum generated random number is: 97
The total of generated random numbers is: 543
The average of generated numbers is: 54.30

Sample 2
Please enter the maximum random number to be use: 50
Please enter the number of rounds: 10

Round   Rand #1 Rand #2 Abs(-)       /     Mod
    1 23 45 22       1 22
    2 33 46 13       1 13
    3 2 36 34      18 0
    4 29 42 13 1      13
    5 3 29 26 9 2
    6 1 27 26      27 0
    7 8 45 37 5 5
    8 33 49 16 1      16
    9 11 31 20 2 9
   10 21 47 26 2 5

The minimum generated random number is: 1
The maximum generated random number is: 49
The total of generated random numbers is: 561
The average of generated numbers is: 28.05

Solutions

Expert Solution

 

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!!


Related Solutions

In JAVA Write a brief program that writes your name to a file in text format...
In JAVA Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called  Dog that contains instance data that represent the dog's name and age.   define the Dog constructor to accept and initialize instance data.   create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age).   Include a toString...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A...
THIS IS JAVA PROGRAMMING 1. Create a class named Name that contains the following: • A private String to represent the first name. • A private String to represent the last name. • A public constructor that accepts two values and assigns them to the above properties. • Public methods named getProperty (e.g. getFirstName) to return the value of the property. • Public methods named setProperty ( e.g. setFirstName)to assign values to each property by using a single argument passed...
Create a Word file containing the following: The full name of the “test subject” The test...
Create a Word file containing the following: The full name of the “test subject” The test subject is a person, not yourself, who can say the names of the products. The best test subject is one who is adamant about being an expert who can distinguish brand A from B. You cannot be the test subject because you generate the sequence, and the test subject cannot see it for an unbiased test. Pets and babies are not allowed as they...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
(C++) Create a data file and name it "input.txt". manually save 10 integers into the file....
(C++) Create a data file and name it "input.txt". manually save 10 integers into the file. Write a program to read the data and calculate the average of events and odds, separately. Print out the average values.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT