Question

This is Java compute_pi (n) //n is the number of “darts” inside=0 for i = 1..n...

This is Java

  compute_pi (n) //n is the number of “darts”
    inside=0
    for i = 1..n
      x=random()
      y=random()
      if sqrt(x*x+y*y)<=1:
        inside++
    pi=4*inside/n
    return pi

Create a new file called ComputePi.java, and implement the above pseudocode to estimate the value of pi. Test the code with increasingly-large values of n. You should not need to recompile the code to do this: use arguments at the commandline to pass in n, and extract the argument from args in main. (If you are unfamiliar with arguments in java, se

Homework Answers

Answer #1
import java.util.Random;

public class ComputePi {

    public static double computePi(int n) {
        Random r = new Random();
        double inside = 0, x, y;
        for (int i = 0; i < n; ++i) {
            x = r.nextDouble();
            y = r.nextDouble();
            if (x * x + y * y < 1) {
                inside++;
            }
        }
        return (4 * inside) / n;
    }

    public static void main(String[] args) {
        System.out.println("calculated value of PI = " + computePi(10000000));
    }
}

Know the answer?
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
Similar Questions
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input...
Java Code: Console IO Practice Exercise The purpose of this exercise is to practice console input and output with the Java console. Setup: Create a class called ConsolePractice with a main method. Create a static field in your class that stores a scanner object. You will use this scanner for all user input. private static Scanner scanner = new Scanner(System.in); Part A: Create a static method called “divide”. Your method should do the following: Accept two integers as parameters, a...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
Question 1 - Debugging Java Problem Description: Commonly attributed to Grace Hopper in the 1940s (when...
Question 1 - Debugging Java Problem Description: Commonly attributed to Grace Hopper in the 1940s (when a moth found in a computer relay was fouling outputs), debugging is the practice of removing errors from code. It is a systematic procedure of examining the output, drawing a hypothesis for the cause of the error, then either implementing a correction or otherwise validating or falsifying the original error hypothesis. We are hunting and correcting errors in a controlled, systematic fashion. Most IDEs...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
USING MATLAB Code for pseudo Random Input N M=0 For i=1 to N X=rand# Y=rand# If...
USING MATLAB Code for pseudo Random Input N M=0 For i=1 to N X=rand# Y=rand# If X^2+ y^2 <= 1, then M=M+1 End the loop Print M/N (This is the probability) Print M/N (this is approximately = Py
IN JAVA!! You may be working with a programming language that has arrays, but not nodes....
IN JAVA!! You may be working with a programming language that has arrays, but not nodes. In this case you will need to save your BST in a two dimensional array. In this lab you will write a program to create a BST modelled as a two-dimensional array. The output from your program will be a two-dimensional array.   THEN: practice creating another array-based BST using integers of your choice. Once you have figured out your algorithm you will be able...
Java code Problem 1. Create a Point class to hold x and y values for a...
Java code Problem 1. Create a Point class to hold x and y values for a point. Create methods show(), add() and subtract() to display the Point x and y values, and add and subtract point coordinates. Tip: Keep x and y separate in the calculation. Create another class Shape, which will form the basis of a set of shapes. The Shape class will contain default functions to calculate area and circumference of the shape, and provide the coordinates (Points)...
##4. What will the following program display? ##def main(): ## x = 1 ## y =...
##4. What will the following program display? ##def main(): ## x = 1 ## y = 3.4 ## print(x, y) ## first printing ## change_us(x, y) ## print(x, y) ##second printing ## ##def change_us(a, b): ## a = 0 ## b = 0 ## print(a, b) ## ##main() ## ##Yes, yes, main() displays ##1 3.4 ##0 0 ##1 3.4 ## The question is: why x and y are still the same while the second printing of (x,y)? ## It seems...
In this Java programming assignment, you will practice using selection statements to determine whether a given...
In this Java programming assignment, you will practice using selection statements to determine whether a given year in the past or future qualifies as a “Leap Year”. I. Design a class called LeapYear in a file called LeapYear.java. This class will hold the main method and the class method that we will write in this assignment. II. Write an empty public static void main(String[] args) method. This method should appear inside the curly braces of the LeapYear class. III. Write...
i need to fix this java code. it not working on Repl.it website online compiler. error...
i need to fix this java code. it not working on Repl.it website online compiler. error -----------------------------------------------  javac -classpath .:/run_dir/junit-4.12.jar:target/dependency/* -d . Main.java  java -classpath .:/run_dir/junit-4.12.jar:target/dependency/* Main Exception in thread "main" java.io.FileNotFoundException: C:\\Users\\Carter Collins\\Downloads\\input.txt (No such file or directory) at java.base/java.io.FileInputStream.open0(Native Method) at java.base/java.io.FileInputStream.open(FileInputStream.java:219) at java.base/java.io.FileInputStream.(FileInputStream.java:157) at java.base/java.util.Scanner.(Scanner.java:639) at Main.main(Main.java:18) exit status 1 ------------------------------------------------ codes:: import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws Exception { Scanner scan = new Scanner(System.in); Scanner sc...