Question

In: Computer Science

****java*** Create an application that generates 20 random numbers between 1 and 100 and writes them...

****java*** Create an application that generates 20 random numbers between 1 and 100 and writes them to a text file on separate lines. Then the program should read the numbers from the file, calculate the average of the numbers, and display this to the screen.

Solutions

Expert Solution

import java.io.File;

import java.io.FileWriter;

import java.io.IOException;

import java.util.Random;

import java.util.Scanner;

class random_numbers {

    public static void main(String[] args) throws IOException {

        Random rn = new Random();

        // writes them to a text file on separate lines.

        FileWriter output = new FileWriter("input.txt");

        // generates 20 random numbers between 1 and 100

        for (int i = 0; i < 20; i++)

            output.write(Integer.toString(rn.nextInt(100) + 1) + "\n");

        // close file

        output.close();

        // Then the program should read the numbers from the file,

        File file = new File("input.txt");

        Scanner input = new Scanner(file);

        // calculate the average of the numbers,

        double avg = 0;

        for (int i = 0; i < 20; i++) {

            avg += input.nextInt();

        }

        avg /= 20;

        // close file

        input.close();

        // display this to the screen.

        System.out.println("Average is: " + avg);

    }

}

.

Output:

.


Related Solutions

Write a Java program that creates an array with 20 random numbers between 1 and 100,...
Write a Java program that creates an array with 20 random numbers between 1 and 100, and passes the array to functions in order to print the array, print the array in reverse order, find the maximum element of the array, and find the minimum element of the array. The prototype of the methods: public static void printArray(int arr[]) public static void printArrayReverse(int arr[]) public static int searchMax(int arr[]) public static int searchMin(int arr[]) Sample output: Random Array: [17 67...
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
Create a program that generates random number between 0 and 50 and puts them into a...
Create a program that generates random number between 0 and 50 and puts them into a linked list. When it generates 49, instead of adding it to the list, it prints the list, freeing each node and then exits. Submit your .c file
Create a JAVA lottery game application.  Generate four random numbers, each between 0 and 9 (inclusive).  Allow the...
Create a JAVA lottery game application.  Generate four random numbers, each between 0 and 9 (inclusive).  Allow the user to guess four numbers.  Compare each of the user’s guesses to the four random numbers and display a message that includes the user’s guess, the randomly determined four-digit number, and the amount of points the user has won as follows: No matches 0 points Any one digit matching 5 points Any two digits matching 100 points Any three digits matching 2,000 points All four...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them array1 and array 2) Add the two 3Darrays together and then get the average. Save the average on a separate 3D array.(lets name it array3) Then add array1 and array3 then get the average Save the average on a separate 3Darray(lets name it array 4) Then add array2 and array3 then get the average Save the average on a separate 3Darray (lets name it...
To generate 100 random numbers between 1-100 in a randomData.txt file To read the 100 random...
To generate 100 random numbers between 1-100 in a randomData.txt file To read the 100 random numbers from randomData.txt and store them in an array Print the data in the array Find the smallest and the largest of the random numbers and their array position Insert an element of value100 in the 51th position of the array Delete all the elements of the array having values between 50-80 and print the residual array Sort the data in the final array(residual)...
Write a JAVA program that generates three random numbers from 1 to 6, simulating a role...
Write a JAVA program that generates three random numbers from 1 to 6, simulating a role of three dice. It will then add, subtract and multiply these two numbers. It will also take the first number to the power of the second and that result to the power of the third. Display the results for each calculation. Please note that the sample run is based on randomly generated numbers so your results will be different. Sample run: 6 + 2...
Create a C++ program that generates a random number from 1 to 100 using the rand()...
Create a C++ program that generates a random number from 1 to 100 using the rand() function. Give the user a total 5 chances to guess the number. Stop the program and indicate the user guessed the correct number and should be applauded. Also, please find out which guess was the closest to the actual number. For example, if the rand() produces 57 and the user guessed 10, 80, 52, 33 and 44 in their respective turns then print out...
Write a program that generates a random number between 1 and 100 and asks the user...
Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Your program should also keep a...
In Excel, the RAND function generates random numbers between 0 and 1 compute the expectation and...
In Excel, the RAND function generates random numbers between 0 and 1 compute the expectation and Standard deviation. Hints a = 0 , b = 1 P(x1<= X <= x2) = F(x2) - F(x1)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT