CSE-1020: Introduction to Computer Science I
York University
Midterm Labtest
ApproxPi
2:30pm Wednesday 21 October 2009
(Lab-04)
  Welcome to the Labtest Environment

During the test, you will not be able to access your regular home directory, access, receive, or send e-mail, print, or access the Internet.

At the end of your test session, this machine will be converted back to a standard, unrestricted Prism Lab machine. At this time, any files written by you during the test, except those that have been submitted, will not be recoverable. Make sure you submit your code before the end of the test . (Instructions to submit your code are below). If you do not submit your work on time, you will receive a grade of zero. (There are no exceptions).

Time Limit

You have about 80 minutes to complete this test (regular lab time). The computers will automatically shutdown at the end of the lab (e.g., 3:50pm), warning you several minutes in advance. No additional time will be provided, so you must submit your work prior to this; otherwise, there will be no way to recover it (it will be lost), and you will receive a grade of zero (no exceptions).

You are encouraged to submit often during the test. Newer submissions overwrite older ones.

Submitting Your Work

When you submit a file, you must include at the top of the file your name (surname, given name) and your Prism lab login. These should be placed in a comment so that the file will compile. Note that files which do not compile will receive a large penalty when marked, no matter how small the error that prevented compiling.

Submit the class ApproxPi.java before the time deadline. Here is the command to submit your work:

% submit 1020 midtermW2.30 ApproxPi.java

(Further details regarding the submit command can also be obtained by typing man submit.)

Once again, you are encouraged to submit regularly. Newer submissions simply overwrite older ones.

Unlike eCheck assignments, there is partial credit possible. If you are able to finish all aspects of the program (for example, say, input validation was requested but you did not do this), you should still check it in.

Useful APIs

Here are the common APIs that you may access:

 
  The Task

Write a Java application called ApproxPi.java.

The infinite sum

4 * ( 1/1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... )

converges to the value of π. It is not practical to compute an infinite sum on a computer, but you can compute a partial sum using the first N terms of the infinite sum.

Part 1: Write a program that asks the user for the number of terms N to use when computing the partial sum shown above. Your program should use Fraction objects from type.lib to compute the partial sum using N terms, and then output the sum as shown in the following sample output (text in red is typed in by the user):


% java ApproxPi
How many terms do you want to sum? 1
The approximate value of pi is
4/1 = 4.0
The value of Math.PI is
3.141592653589793
%
% java ApproxPi
How many terms do you want to sum? 20
The approximate value of pi is
516197940314096/166966608033225 = 3.0916238066678385
The value of Math.PI is
3.141592653589793
%
% java ApproxPi
How many terms do you want to sum? 0
The approximate value of pi is
0/1 = 0.0
The value of Math.PI is
3.141592653589793
%

Get Part 1 working first. Once you have, extend your program for Part 2. You need to do both for full credit, but you will receive partial marks if you only succeed in finishing Part 1.

Part 2: Add some code that performs friendly validation on the user input value for the number of terms N. Your code should ensure that the number of terms is always in the range 0 to 20. Your program should produce the following output: (text in red is typed in by the user)


% java ApproxPi
How many terms do you want to sum? -100
Number of terms must be from 0-20... try again: 100
Number of terms must be from 0-20... try again: -1
Number of terms must be from 0-20... try again: 21
Number of terms must be from 0-20... try again: 10
The approximate value of pi is
44257352/14549535 = 3.0418396189294024
The value of Math.PI is
3.141592653589793

Coding

You should use good programming practices as described in the textbook, and your code should conform to the style guide in the textbook. You should comment at least minimally, as by the guidelines.

You should not use any features of Java not covered in the first five chapters of the text. There will be some deduction if you do.