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

Imagine a pizzeria in which the pizza chef is careless when tossing olives onto

ID: 663345 • Letter: I

Question

Imagine a pizzeria in which the pizza chef is careless when tossing olives onto a pizza base. In fact he throws them completely randomly in the square area containing the pizza. One can actually get a simple approximation to r by counting the proportion of the olives that hit the pizza. ? A pizza of diameter 1 has an area of pi/4, while the area of the entire square containing the pizza is 1. Therefore the proportion of olives hitting the pizza multiplied by 4 is an approximation to r. In the example, 20 out of 28 olives have hit the pizza and so this gives the (rather poor) approximate value of 4 * 20 / 28 = 2.857 Write a method, public static double pi(int olives) that simulates the random throwing of olives on to a pizza base in order to approximate the value of r. Your method should use an object from the library class java.util.Random to generate a sequence of random olive positions inside a suitable square, and keep track of how many land on the pizza. The client will specify the number of olives required, and you should return the corresponding approximation. Your method may mmt refer to Math. PI You may wish to use the following method from the class java.util.Random: public double nextDouble() Returns the next pseudorandom, uniformly distributed double value between 0.0 and 1.0 from this random number generator's sequence.

Explanation / Answer

//////////////////////////////////////////////////////////////////////

PizzaOlives.java

///////////////////////////////////////////////////////////////////

import java.util.Random;
import java.util.Scanner;


public class PizzaOlives {

public static double pi(int olives) // method pi() that simulate the random throwing of olives on to a pizza

//base in order to approximate the value of pi
{

double []xc=new double[olives];
double []yc=new double[olives];
Random r=new Random(); // object of random class
  
double radius=0.5;

// generating points
for(int i=0;i<olives;i++)
{
xc[i]=r.nextDouble();
yc[i]=r.nextDouble();
}
  
// let center of the pizza (0.5,0.5)
int count=0;

for (int i=0;i<olives;i++)
{
// whether olives hit the pizza
// using x^2+y^2<=r^2 (using Equation of circle)
// a point lie within the circle
// equation of circle with center at (h,k) is (x-h)^2+(y-k)^2=r^2;
if(((xc[i]-radius)*(xc[i]-radius)+(yc[i]-radius)*(yc[i]-radius))<=((radius*radius)))
{
count++; // counting the number of olives hit the pizza
}
  
}
double temp=(double)count/olives;
// return the approximate value of pi
return (4*temp);
  
}
public static void main(String[] args) {
  
Scanner sc=new Scanner(System.in);
// reading input
int olives=sc.nextInt();

// Calculating and printing approximation of value of pi
System.out.println(pi(olives));
  
}
}

Outputs:

////////////////////////////

olives

value of pi

///////////////////////////

output1.

1000
3.152

/////////////////////////////////

output2.

80
3.15

/////////////////////////////////

output3.

28
3.857142857142857