CSE-1020: Introduction to Computer Science I
York University
Midterm Lab Test: GeometricSeries
2:30pm Monday 26 October 2009
(Lab-01)
  Welcome to the Lab-test 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.

The lab tests are closed-book and no aids are allowed.

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.)

Instructions

You have the lab period, 80 minutes, to complete this lab test. The computers will automatically shutdown at the end of the time (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. Note that newer submissions overwrite older ones.

Your program will be marked for good style, as well as for running correctly (producing correct output in the correct format).

Submitting Your Work

When you submit a file, you should 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 GeometricSeries.java before the time deadline. Here is the command to submit your work:

% submit 1020 midtermM2.30 GeometricSeries.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 submit it.

Useful APIs

Here are the common APIs that you may access:

 
  The Task

Write a Java application called GeometricSeries.java.

Suppose that you have a fraction x/y where x and y are both positive and x < y (ie. the fraction is positive and has a numeric value less than 1). Then the infinite geometric series:

(x/y)0 + (x/y)1 + (x/y)2 + (x/y)3 + (x/y)4 + ...

is guaranteed to converge to a finite positive value.

Write a program that calculates an approximation to the infinite series by using only the first N terms of the series, where x, y, and N are all specified by the user. You should use the ToolBox class and the Fraction class to validate that the fraction x/y meets 3 conditions:

  1. x/y must be 0 or larger
  2. x/y must be less than 1
  3. y must be 5 or smaller

You should also validate the value of N. If the user asks for 0 or fewer terms, you should set N to 1. If the user asks for more than 25 terms you should set N to 25.

Your program should use the type.lib.Fraction class to represent each term (x/y)k of the sum (where 0 <= k <= N). Your program should print out each term of the series as a Fraction. Due to the limitations of the Fraction class, you should compute the sum as a double value instead of using the add method in Fraction.

At the end of the program you should output the sum of the series. See the sample output below for examples.

Sample Runs

Here are two sample runs of a correctly written program. Note that the text in red is text that the user types. The text in black is what the program types. The '%' represents the prompt from the command-line window (shell).

% java GeometricSeries
Enter a fraction that is greater than 0 and less than 1:
-1 1
Exception in thread "main" java.lang.RuntimeException: Fraction is less than 0
	at type.lib.ToolBox.crash(ToolBox.java:25)
	at GeometricSeries.main(GeometricSeries.java:20)
% java GeometricSeries
Enter a fraction that is greater than 0 and less than 1:
2 1
Exception in thread "main" java.lang.RuntimeException: Fraction is bigger than or equal to 1
	at type.lib.ToolBox.crash(ToolBox.java:25)
	at GeometricSeries.main(GeometricSeries.java:21)
% java GeometricSeries
Enter a fraction that is greater than 0 and less than 1:
1 8
Exception in thread "main" java.lang.RuntimeException: Denominator is too large
	at type.lib.ToolBox.crash(ToolBox.java:25)
	at GeometricSeries.main(GeometricSeries.java:22)
% java GeometricSeries
Enter a fraction that is greater than 0 and less than 1:
1 3
Enter the number of terms to sum:
10
 1 : 1/1
 2 : 1/3
 3 : 1/9
 4 : 1/27
 5 : 1/81
 6 : 1/243
 7 : 1/729
 8 : 1/2187
 9 : 1/6561
10 : 1/19683
The sum using 10 terms is 1.4999745973682874
% java GeometricSeries
Enter a fraction that is greater than 0 and less than 1:
1 3
Enter the number of terms to sum:
-5
 1 : 1/1
The sum using 1 terms is 1.0
% java GeometricSeries
Enter a fraction that is greater than 0 and less than 1:
4 5
Enter the number of terms to sum:
50
 1 : 1/1
 2 : 4/5
 3 : 16/25
 4 : 64/125
 5 : 256/625
 6 : 1024/3125
 7 : 4096/15625
 8 : 16384/78125
 9 : 65536/390625
10 : 262144/1953125
11 : 1048576/9765625
12 : 4194304/48828125
13 : 16777216/244140625
14 : 67108864/1220703125
15 : 268435456/6103515625
16 : 1073741824/30517578125
17 : 4294967296/152587890625
18 : 17179869184/762939453125
19 : 68719476736/3814697265625
20 : 274877906944/19073486328125
21 : 1099511627776/95367431640625
22 : 4398046511104/476837158203125
23 : 17592186044416/2384185791015625
24 : 70368744177664/11920928955078125
25 : 281474976710656/59604644775390625
The sum using 25 terms is 4.981110534068523
%

Coding

You should use good programming practises 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.