Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Random;
- // generate random integers and report their frequencies of occurrence
- public class CountRandoms
- {
- public static void main (String[] args) {
- // random integers will be generated in the range [1, biggest]
- int biggest = 10;
- // how many random values to generate
- int howMany = 1000;
- // filename to which to write report
- String rptFile = "countRandomsRpt.txt";
- // generate the vales and write the report
- saveReport(
- getCountsReport(
- getFrequency(
- biggest,
- getRandoms( biggest, howMany )
- )
- ),
- rptFile
- );
- }
- // generate howMany random integers in the range [1, biggest]
- public static int[] getRandoms( int biggest, int howMany ) {
- // allocate array to hold random integers
- int[] r = new int[ howMany ];
- // create new Random object
- Random rand = new Random();
- // load new, random integer in range [1, biggest] to array
- for (int i = 0; i < howMany; i++) {
- r[ i ] = 1 + rand.nextInt( biggest );
- }
- return r;
- }
- // count the frequency of occurrence of each integer in the
- // range [1, biggest] in the array r
- public static int[] getFrequency( int biggest, int[] r ) {
- // allocate array to hold frequency count for each integer
- // in the range [1, biggest]
- int[] freq = new int[ biggest ];
- for (int i = 0; i < r.length; i++) {
- freq[ r[ i ] - 1 ]++;
- }
- return freq;
- }
- // compose a report of freq(uency) counts
- public static String getCountsReport( int[] freq ) {
- // total up the frequencies and identify the number of digits
- // in the largest frequency
- int maxFWidth = 0;
- int howMany = 0;
- for (int i = 0; i < freq.length; i++) {
- howMany += freq[ i ];
- maxFWidth = Math.max(maxFWidth, freq[ i ]);
- }
- maxFWidth = ("" + maxFWidth).length();
- // number of digits in biggest value
- int maxNWidth = ("" + freq.length).length();
- // collect report output
- StringBuffer rpt = new StringBuffer();
- // add report headings
- rpt.append(
- "Random Number Frequency Counts Report\n" +
- "-------------------------------------\n" +
- "For " + howMany +" random integer in the range [1, " +
- freq.length + "]:\n\n"
- );
- // add frequency detail for each number
- for (int i = 0; i < freq.length; i++) {
- rpt.append(
- " " + padLeft( maxNWidth, i + 1) + " occurred " +
- padLeft( maxFWidth, freq[ i ] ) + " times\n"
- );
- }
- return rpt.toString();
- }
- // write report string rpt to file named rptFile
- public static void saveReport( String rpt, String rptFile ) {
- // allocate report file and stream for writing
- File outfile = new File( rptFile );
- FileOutputStream out = null;
- try { out = new FileOutputStream( outfile ); }
- catch ( FileNotFoundException e ) {
- // probably should do something more useful
- System.out.println( "Error!!! File Not Found\n" + e.toString() );
- }
- try { out.write( rpt.getBytes() ); }
- catch ( IOException e ){
- // probably should do something more useful
- System.out.println(
- "reportCounts() failed trying to write a record to " +
- rptFile + "\n" + e.toString()
- );
- }
- try { out.close(); }
- catch (IOException e) {
- // probably should do something more useful
- System.out.println(
- "reportCounts() failed trying to close " +
- rptFile + "\n" + e.toString()
- );
- }
- }
- // convert an int to string of given width, padded with blanks on
- // the left-hand side
- public static String padLeft( int width, int n) {
- String p = "" + n;
- while ( width > p.length() ) {
- p = " " + p;
- }
- return p;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement