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