1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static void main(String[] args) {
SimpleReader input = new SimpleReader1L();
SimpleWriter output = new SimpleWriter1L();
output.print(\"Number of points: \");
int n = input.nextInteger();
int ptsInInterval = 0, ptsInSubinterval = 0;
Random rnd = new Random1L();
while (ptsInInterval < n) {
double x = rnd.nextDouble();
ptsInInterval++;
if (x < 0.5) {
ptsInSubinterval++;
}
}
double estimate = (100.0 * ptsInSubinterval) / ptsInInterval;
output.println(\"Estimate of percentage: \" + estimate + \"%\");
input.close();
output.close();
}
1. What does this program do?
2. What is the program output if the user enters 10000 when prompted for the number of points?
3. How would your answer to the above question change if the test in line 15 used <= rather than <?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); SimpleWriter output = new SimpleWriter1L(); output.print(\"Number of points: \"); int n = input.nextInteger(); int ptsInInterval = 0, ptsInSubinterval = 0; Random rnd = new Random1L(); while (ptsInInterval < n) { double x = rnd.nextDouble(); ptsInInterval++; if (x < 0.5) { ptsInSubinterval++; } } double estimate = (100.0 * ptsInSubinterval) / ptsInInterval; output.println(\"Estimate of percentage: \" + estimate + \"%\"); input.close(); output.close(); } 1. What does this program do? 2. What is the program output if the user enters 10000 when prompted for the number of points? 3. How would your answer to the above question change if the test in line 15 used <= rather than <? | 
Solution
Answer1)
 This program calculating the percentage of random values generated by random function whose value are less than 0.5 out of number of points entered by user.
Answer2)
 The output will vary each time you run the code because of random function.
 my output is: 49.64%
Answer3)
 It will count all the values whose value is equal to 0.5
 output after changing \'<\' to \'<=\' is: 49.64%
Since you have not provided the SimpleReader ,SimpleWriter and Random classes. so, i have changed the code like that:
import java.util.Random;
 import java.util.Scanner;
 public class Results {
    public static void main(String[] args) {
 //   SimpleReader input = new SimpleReader1L();
 //   SimpleWriter output = new SimpleWriter1L();
       
        Scanner sc = new Scanner(System.in);
      
    System.out.print(\"Number of points: \");
    int n = sc.nextInt();
      
    int ptsInInterval = 0, ptsInSubinterval = 0;
      
    Random rnd = new Random(1000);
      
    while (ptsInInterval < n) {
    double x = rnd.nextDouble();
    ptsInInterval++;
    if (x <= 0.5) {
    ptsInSubinterval++;
    }
    }
    double estimate = (100.0 * ptsInSubinterval) / ptsInInterval;
    System.out.println(\"Estimate of percentage: \" + estimate + \"%\");
   
    sc.close();
    }
    }
![1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); SimpleWrit 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); SimpleWrit](/WebImages/46/1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-1145506-1761615547-0.webp)
![1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); SimpleWrit 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); SimpleWrit](/WebImages/46/1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-1145506-1761615547-1.webp)
![1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); SimpleWrit 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); SimpleWrit](/WebImages/46/1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-1145506-1761615547-2.webp)
