Question

Please write in java program Write an application that generates 100 random integers between 1 and...

Please write in java program


Write an application that generates 100 random integers between 1 and 75 and writes them to a file named "file_activity.txt". Read the data back from the file and display the following:

  • The sum of the numbers in the file
  • The average of the numbers in the file
  • The numbers in the file in increasing order

To sort an array:
int[] arr = new int[20];
Arrays.sort(arr); -- this sorts an array

To sort an ArrayList:
ArrayList<Integer> aLst = new ArrayList<>();
Collections.sort(aLst);

Note: The array and ArrayList in the above examples are empty. Create your array/ArrayList, put the data from the file in the array/ArrayList, then sort it.



0 0
Add a comment Improve this question Transcribed image text
Answer #1

package datastructure;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class Test {
  
  
  
  
  
public static void main(String args[]) throws FileNotFoundException
  
{
  
  
   // create file object
File fp=new File("C:\\Users\\CHITTIMALLA\\Desktop\\file_activity.txt");
  
  
// to write the file
PrintWriter out= new PrintWriter(fp);
Random rand = new Random();
  
  

      
      
   for(int i=0;i<100;i++)  
   {
      
   
int num= rand.nextInt(75) + 1; // generates random integers between 1 to 75
out.println(num);
out.flush();


  
   }
  
   int[] arry=new int[100];
  
   Scanner sc= new Scanner(fp); // to read file
  
  
  
   for(int i=0;i<100;i++)  
   {
      
      
arry[i] =rand.nextInt(75) + 1; // read into array

  
   }
  
  
   // to find sum
   int sum=0;
   for(int i: arry)
       sum=sum+i;
  
   System.out.println("Sum is : "+sum);
  
   float avg=(float)sum/100;
   System.out.println("The average is : "+avg);
  
   // to sort array
   Arrays.sort(arry);
  
   System.out.println("The sorted numbers are : ");
  
   for(int i:arry)
       System.out.println(i);
  
}
  
}

FIle:

SampleRun:

Please upvote me

comment for any query

Add a comment
Know the answer?
Add Answer to:
Please write in java program Write an application that generates 100 random integers between 1 and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a JAVA application which generates 20 random numbers between 1 and 100 and writes them...

    Create a JAVA application which 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.

  • java 1. Write an application that generates n random integers in the range provided the user....

    java 1. Write an application that generates n random integers in the range provided the user. Use method genrate Even to generate even numbers only and generateOdd to generate odd numbers void generateOdd(int n, int r1, int r2); void generateEven(int n, int r1, int r2); Sample run: How many integers you have?5 What are your number ranges? 3 10 The generated even numbers are 6 4 8 10 6 The generated odd numbers are 37 9 35

  • //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList...

    //Java (Sort ArrayList) Write the following method that sorts an ArrayList: public static > void sort(ArrayList list) Write a test program that does the following operations: 4. Creates an empty ArrayList of Integer elements; 5. Generates 20 integer values, drawn at random between 0 and 9 (included), and adds them to the array list; 6. Calls the method sort and prints both the original list, and the sorted one.

  • A. Create a java program that generates 1000 random integers. Write the 1000 random integers to...

    A. Create a java program that generates 1000 random integers. Write the 1000 random integers to a file using the Formatter class. B. Create a second java program that opens the file with the 1000 integers using the Scanner class. Read in the integers and find and print out the largest, smallest and the average of all 1000 numbers. C. Repeat A from above but use the BufferedWriter class B. Repeat B from above but use the BufferedReader class.

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • 2. Write a C++ program, that generates a set of random integers and places them in...

    2. Write a C++ program, that generates a set of random integers and places them in an array. The number of values generated and their range are entered by the user. The program then continually prompts the user for one of the following character commands: a: display all the values I: display the values less than a given value g display the values greater than a given value e: display all odd values o: display all even values q: quit...

  • C++ programming please Write a program that will display random numbers in order from low to...

    C++ programming please Write a program that will display random numbers in order from low to high. 1. Declare an integer array of size 100. Ask the user how many numbers to work with (n). It can be less than 100. 2. Generate random numbers from 10 to 50. 3. Write the random numbers to an output file named random.dat. Close the file. 4. Open random.dat for input and read the data values into your array. Pass your array to...

  • Please write a Java program that does the following: Create an array of 100 integers. Store...

    Please write a Java program that does the following: Create an array of 100 integers. Store 100 random integers (between 1 and 100) in the array. Print out the elements of the array. Sort the array in ascending order. Print out the sorted array. Prompt the user to enter a number between 1 and 100. Search the array for that number and then display "Found" or "Not Found" message. Display each number from 1 to 100 and the number of...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in...

    JAVA~ 1. Write a static method named countOdd(my_array) that returns the number of odd integers in a given array. If there are no odd numbers in the array, the method should return 0. If the array is empty, the method should also return 0. For example: Test Result System.out.println(countOdd(new int[]{2, 3, 5, 6})); 2 System.out.println(countOdd(new int[]{2})); 0 System.out.println(countOdd(new int[]{})); 0 System.out.println(countOdd(new int[]{-7, 2, 3, 8, 6, 6, 75, 38, 3, 2})); 4 public static int ----------------------------------------------------------------------------------------------------------- 2. Write a static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT