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.
create a file in java where you can store the first name, last name and the...
create a file in java where you can store the first name, last name and the grade of the student. this will repeats until the answer of the question "are you done?" is.equals (y). then write another program that you can read from this file the student's information . I have dome most of it but my program stores only 1 student's information. WRITE IN FILE public static void main(String[] args) { System.out.println("Enter the file name"); Scanner keyboard=new Scanner(System.in); String...
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...
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...
1. Create a1. Create a new java file named Name.java that should have Name class based...
1. Create a1. Create a new java file named Name.java that should have Name class based on new java file named Name.java that should have Name class based on the following UML Name - first: String - last: String + Name () + Name (String firstName, String lastName) + getName () : String + setName (String firstName, String lastName) : void + getFirst () : String + setFirst (String firstName) : void + getLast () : String + setLast (String...
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...
File has a format Name and number, the number represents power. The name and the (integer)...
File has a format Name and number, the number represents power. The name and the (integer) power are separated by some amount of space. Importantly, any line that begins with a hash '#' are comments and they need to be ingored. Write a program that reads from that file, and prints out only the name of the hero with the strongest power. That name should be capitalized (not uppercase, but capitalized, as in 'Galadriel') Here is the heroes.txt # DC...
File IO Java question • Ask the user to specify the file name to open for...
File IO Java question • Ask the user to specify the file name to open for reading • Get the number of data M (M<= N) he wants to read from file • Read M numbers from the file and store them in an array • Compute the average and display the numbers and average.
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...
Create a Java class file for an Account class. In the File menu select New File......
Create a Java class file for an Account 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 Account. For Package: select csci1011.lab8. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab8; /** * * @author Your Name */ public class Account { } Implement the Account...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT