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. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
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...
Step 1: Create a new Java project in NetBeans called “PartnerLab1” Create a new Java Class...
Step 1: Create a new Java project in NetBeans called “PartnerLab1” Create a new Java Class in that project called "BouncyHouse" Create a new Java Class in that project called “Person” Step 2: Implement a properly encapsulated "BouncyHouse" class that has the following attributes: Weight limit Total current weight of all occupants in the bouncy house (Note: all weights in this assignment can be represented as whole numbers) …and methods to perform the following tasks: Set the weight limit Set...
Java Question-- I have a program with 2 separate methods-- 1 method that assigns random values...
Java Question-- I have a program with 2 separate methods-- 1 method that assigns random values to an array and the other that uses the insertion method to sort the array. And of course I have a main method. I need to call both of these methods in the main method. I have successfully called the random assigned method from the main method, but I am confused on how to call the second sort method. Here is what I have--...
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
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...
In Java please 10.9 Lab 6 In BlueJ, create a project called Lab6 Create a class...
In Java please 10.9 Lab 6 In BlueJ, create a project called Lab6 Create a class called LineSegment –Note: test changes as you go Copy the code for LineSegment given below into the class. Take a few minutes to understand what the class does. Besides the mutators and accessors, it has a method called print, that prints the end points of the line segment in the form: (x, y) to (x, y) You will have to write two methods in...
Create a JAVA project named IA01 that does the following: Display a randomly-determined even number to...
Create a JAVA project named IA01 that does the following: Display a randomly-determined even number to the user The number should be between 2 and 10 inclusive Prompt the user to enter the value of half that number You can assume the user will enter valid numbers, not letters or gibberish They only get one chance to answer per number Tell the user if they are right or wrong Ask the user if they want to try another even number...