Question

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.

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

Generate 1000 random nos and write to a file , then read the file and calculate max (largest ),min (smallest ) and average of all 1000 nos.

By using two method.

I). WRITING BY FORMATER AND READING BY SCANNER

II). WRITING BY BUFFER WRITER AND READING BY BUFFER READER

I). USING FORMATER/SCANNER

1. FILE WRITE USING  Formatter.

import java.io.FileOutputStream;
import java.util.Formatter;
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/* GENERATE 1000 RANDOM INTEGER VALUES AND WRITE TO FILE USING FORMATTER CLASS */
class RandomWrite
{
   public static void main(String a[])
   {
       try
       {
           /* WRITE TO FILE */
           Formatter fmtFile;
           fmtFile = new Formatter(new FileOutputStream("random.txt")); // FILE OPEN OR CREATED USING FORMATTER

           for (int i = 1; i <= 1000; i++)       // ITERATE TO GENERATE 1000 RANDOM NOS
               fmtFile.format("%d\n", getRandomNo()); // WRITE RANDOM NOS TO FILE

           fmtFile.close(); // CLOSE Formatter

       }
       catch (IOException e) {
               System.out.println(e);
       }
   } // MAIN METHOD END
   //CALCULATE RANDOM INTEGERE VALUE
   public static int getRandomNo()
   {
           Random rand = new Random();
           return rand.nextInt(1000); // GENARATE RANDOM NOS IN RANGE OF 1000 , WE CAN CHANGE THIS VALUE
   }
}

2. FILE READ USING  SCANNER

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
/* READ 1000 RANDOM INTEGER FROM FILE USING SCANNER AND CALCULATE MAX,MIN , AVERAGE */
class RandomRead
{
   public static void main(String a[])
   {
       try
       {
           int sum=0;
           int max=0;
           int min=Integer.MAX_VALUE;
           //READING FROM FILE
           Scanner sc = new Scanner(new File("random.txt")); //FILE OPEN USING SCANNER
           while (sc.hasNext())
           {
               String str = sc.nextLine(); // READING FILE LINE BY LINE , EACH LINE CONTAINS A INTEGER VALUE
               sum=sum+ Integer.parseInt(str); // SUMMATION OF VALUES

               if ( Integer.parseInt(str) > max)
                   max=Integer.parseInt(str);   // FIND MAXIMUM OF VALUES

               if ( Integer.parseInt(str) < min)
                   min=Integer.parseInt(str); // FIND MAXIMUM OF VALUES
           }

           System.out.println("Maximum : " + max);
           System.out.println("Minimum : "+min);
           System.out.println("Average : "+sum/1000.0);
           sc.close(); // CLOSE SCANNER
       }
       catch (IOException e) {System.out.println(e);
       }
   } // MAIN METHOD END
}

SCREEN SHOT OF CODE FOR WRITING AND READING

OUT PUT

II). USING BUFFER READER/WRITER

1. FILE WRITE USING  BUFFER WRITER.

import java.util.Scanner;
import java.io.IOException;
import java.util.Random;
import java.io.BufferedWriter;
import java.io.FileWriter;
/* GENERATE 1000 RANDOM INTEGER VALUES AND WRITE TO FILE USING BUFFER WRITER */
class RandomWrite
{
   public static void main(String a[])
   {
       try
       {
           /* WRITE TO FILE */
           FileWriter fw = new FileWriter("random.txt"); // file created as random.txt
           BufferedWriter WriteFileBuffer = new BufferedWriter(fw);

           for (int i = 1; i <= 1000; i++)       // ITERATE TO GENERATE 1000 RANDOM NOS
           {
               // CONVERTED INTEGER TO STRING BEFORE WRITING TO FILE, AS BUFFER WRITER DOES NOT INTEGER VALUE
               String randNo=String.valueOf(getRandomNo());
               WriteFileBuffer.write(randNo); // WRITE RANDOM NOS TO FILE
               WriteFileBuffer.newLine(); // NEW LINE AFTER EACH INTEGER VALUE
           }
           WriteFileBuffer.close(); // CLOSE BufferedWriter
       }
       catch (IOException e) {
               System.out.println(e);
       }
   } // MAIN METHOD END
   //CALCULATE RANDOM INTEGERE VALUE
   public static int getRandomNo()
   {
           Random rand = new Random();
           return rand.nextInt(1000); // GENARATE RANDOM NOS IN RANGE OF 1000 , WE CAN CHANGE THIS VALUE
   }
} // CLASS END

2. FILE WRITE USING  BUFFER READER .

import java.io.IOException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
/* READ 1000 RANDOM INTEGER FROM FILE USING BUFER READER , THEN CALCULATE MAX,MIN , AVERAGE */
class RandomRead
{
   public static void main(String a[])
   {
       try
       {
           int sum=0;
           int max=0;
           int min=Integer.MAX_VALUE;
           //READING FROM FILE
           FileReader fr = new FileReader("random.txt");
           BufferedReader ReadFileBuffer = new BufferedReader(fr); //FILE OPEN USING BufferedReader
           String str;
           while ((str=ReadFileBuffer.readLine()) !=null) //READING FILE LINE BY LINE , EACH LINE CONTAINS A INTEGER VALUE
           {
               sum=sum+ Integer.parseInt(str); // SUMMATION OF VALUES
               if ( Integer.parseInt(str) > max)
                   max=Integer.parseInt(str);   // FIND MAXIMUM OF VALUES
               if ( Integer.parseInt(str) < min)
                   min=Integer.parseInt(str); // FIND MAXIMUM OF VALUES
           }
           System.out.println("Maximum : " + max);
           System.out.println("Minimum : "+ min);
           System.out.println("Average : "+ sum/1000.0);
           ReadFileBuffer.close(); // CLOSE SCANNER
       }
       catch (IOException e) {System.out.println(e);
       }
   } // MAIN METHOD END
}

SCREENSHOT OF CODE

1. WRITER

2. READER

SCREENSHOT OUTPUT

Add a comment
Know the answer?
Add Answer to:
A. Create a java program that generates 1000 random integers. Write the 1000 random integers to...
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
  • /*Write a Java program that generates 1000 random integers, * calculates the average and prints two...

    /*Write a Java program that generates 1000 random integers, * calculates the average and prints two counts, one for the * numbers lower than the average and one for the numbers larger than the average. */ Use java thanks

  • 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>...

  • C++ language Write a program that 1. generates a random integer between 100 and 1000, call...

    C++ language Write a program that 1. generates a random integer between 100 and 1000, call the number N 2. reads a file name from the keyboard 3. opens a file for output using the file name from step 2 4. generates N random numbers between 1 and 100 and writes them to the file from step 3 5. closes the file 6. opens the file from steps 3, and 4 for input 7. reads the numbers from the file...

  • Java Programming - Write a program that generates two random integers, both in the range 25...

    Java Programming - Write a program that generates two random integers, both in the range 25 to 75, inclusive. Use the Math class. Print both integers and then display the positive difference between the two integers, but use a selection. Do not use the absolute value method of the Math class.

  • 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.

  • C++ Manually create a file Numbers.txtand fill it with integers, one below the other. Write a...

    C++ Manually create a file Numbers.txtand fill it with integers, one below the other. Write a program that reads ANY sequence of integers (can be positive, 0 or negative) from this file. Using a looping construct, you will read numbers from the file till end of the file is reached. Calculate the largest and the smallest numbers among the data in the file. Your code must display both of those on the terminal screen along with total count of numbers...

  • Write a program in python that generates X random integers Num. Num is a random number...

    Write a program in python that generates X random integers Num. Num is a random number between 20 to 50. X is a random number between 10 to 15. Calculate and show the Smallest, Largest, Sum, and Average of those numbers. You are not allowed to use Python functions sample(), min(), max(), average(), sort(), sorted()!! HINTs: to find Smallest.... 1) X is a random number between 10 to 15... for example 11...this determines how many times the loop will happen...

  • Write a program in c++ that generates a 100 random numbers between 1 and 1000 and...

    Write a program in c++ that generates a 100 random numbers between 1 and 1000 and writes them to a data file called "randNum.dat". Then re-open the file, read the 100 numbers, print them to the screen, and find and print the minimum and maximum values. Specifications: If your output or input file fails to open correctly, print an error message that either states: Output file failed to open Input file failed to open depending on whether the output or...

  • Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called...

    Write a simple Java program with the following naming structure: Open Eclipse Create a workspace called hw1 Create a project called hw1 (make sure you select the “Use project folder as root for sources and class files”) Create a class called Hw1 in the hw1 package (make sure you check the box that auto creates the main method). Add a comment to the main that includes your name Write code that demonstrates the use of each of the following basic...

  • Write a java program that creates a file called numbers.txt that uses the PrintWriter class. Write...

    Write a java program that creates a file called numbers.txt that uses the PrintWriter class. Write the odd numbers 1 to 99 into the file. Close the file. Using Scanner, open the numbers.txt file and read in the numbers. Add them all up and print the total. Close the file.

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