Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA PROGRAMMING: Attached is a simple program, RandomDoubles, to generate a giv

ID: 3691428 • Letter: J

Question

JAVA PROGRAMMING:

Attached is a simple program, RandomDoubles, to generate a given number of random numbers within a range. Rewrite the indicated method to use a stream.

import java.util.Scanner;

public class RandomDoubles {

   public static void main(String[] args) {
       Scanner kybd = new Scanner(System.in);
       System.out.println("Enter ceiling, floor, number of samples: ");
       double ceil = kybd.nextDouble();
       double floo = kybd.nextDouble();
       int numSamples = kybd.nextInt();
       if (floo >= ceil || ceil > 1 || floo > 1) {
           System.out.println("invalid ceiling, floor");
           System.exit(0);
       }
       double avg = calcDoubles(floo, ceil, numSamples);
       System.out.println("Average is " + avg);
   }

   private static double calcDoubles(double floo, double ceil, int numSamples) {
       // rewrite this method to use streams
       double[] samples = genDoubles(floo, ceil, numSamples);
       double tot = 0;
       for (int i = 0; i < samples.length; i++) {
           tot += samples[i];
       }
       double avg = tot / samples.length;
       return avg;
   }

   public static double[] genDoubles(double floor, double ceiling, int num) {
       int cnt = 0;
       double range = ceiling - floor;
       double[] rv = new double[num];
       while (cnt < num) {
           double v = Math.random();
           v = v * range + floor;
           rv[cnt++] = v;
       }
       return rv;
   }

}

Explanation / Answer

import java.io.File;
import java.util.Scanner;

public class RandomDoubles {
   public static void main(String[] args) {
       Scanner kybd = new Scanner(System.in);
       System.out
               .println("Enter file name contains ceiling, floor, number of samples: ");
       String fileName = kybd.next();

       double avg = calcDoubles(fileName);
       System.out.println("Average is " + avg);

   }

   private static double calcDoubles(String fileName) {
       // rewrite this method to use streams
       double floo = 0.0d;
       double ceil = 0.0d;
       int numSamples = 0;
       Scanner scanner = null;
       try {
           // read data from file
           scanner = new Scanner(new File(fileName));
           // read params from file
           if (scanner.hasNext()) {
               ceil = scanner.nextDouble();
               floo = scanner.nextDouble();
               numSamples = scanner.nextInt();
           }
       } catch (Exception e) {
           // TODO: handle exception
       }

       if (floo >= ceil || ceil > 1 || floo > 1) {
           System.out.println("invalid ceiling, floor");
           System.exit(0);
       }
       double[] samples = genDoubles(floo, ceil, numSamples);
       double tot = 0;
       for (int i = 0; i < samples.length; i++) {
           tot += samples[i];
       }
       double avg = tot / samples.length;
       return avg;
   }

   public static double[] genDoubles(double floor, double ceiling, int num) {
       int cnt = 0;
       double range = ceiling - floor;
       double[] rv = new double[num];
       while (cnt < num) {
           double v = Math.random();
           v = v * range + floor;
           rv[cnt++] = v;
       }
       return rv;
   }
}

sample.txt

0.3 0.1 2

OUTPUT:

Enter file name contains ceiling, floor, number of samples:
sample.txt
Average is 0.13858940138259845