CISC 181 Introduction to Computer Science, Fall, 1998

Lab 2 - program using conditional and repetition statements and
an applet using basic graphics

These lab exercises may be started before lab is held on monday afternoon. In fact it is a good idea to start on these ahead of time, so as to arrive at lab with any questions that have arisen, to arrive generally ready to make good use of the lab time.
  1. Set up your java and course environment.
  2. This exercise is to do some modifications to the program developed in class. This is to be done and handed in at the end of lab on Monday, Sept 21. Modify either the file 181/Evens.java developed by section 81 or the file 181/ex3_16.java developed by section 80.
  3. This exercise is to be completed by the following lab, on Monday, Sept 28. Write an applet which illustrates a MonteCarlo approximation of Pi.

    Since the area of a circle of radius r is pi*r^2 and the area of a square exactly containing that circle is (2r)^2, the ratio of the two areas is pi/4. If a point is picked at random (random x and y coordinates) within the square, the probability that it is in the circle is pi/4.

    The Monte Carlo method is to pick a large succession of random points and count how many are in the circle. The expectation is that (number of points in circle)/(total number of points) approximates pi/4.

    The project is to write an applet that displays a circle in a square, then adds random points to the picture one by one. The display should also contain the current total number of random points that have been put in the picture and the current approximation to pi. Use color appropriately. In particular, use a different color for points within the circle and for points outside (but in the square).