Java I CS 142 Week 03-04 Assignment.docx

advertisement
Java I CS 142 Week 03-04 Assignment
Your job will be to:
1. Classname= “Riemann”
2. As always, the first line of code should have
your name in a comment.
3. Write a Java program to approximate
π‘₯2
(− )
π‘₯β„Žπ‘– 1
∫π‘₯π‘™π‘œ √2πœ‹ 𝑒 2 𝑑π‘₯
using left-Riemann
integration.
OK, this sounds “really hard” – and in a
sense it is. But… calculations like this were
one of the main reasons for the invention of computers, and this
is actually a relatively easy calculation to program. It is calculated
by ∑π‘₯<π‘₯β„Žπ‘–
π‘₯=π‘₯π‘™π‘œ (𝑓(π‘₯) ∗ π‘‘π‘’π‘™π‘‘π‘Žπ‘₯ ) that is, it is the sum of the areas of a
whole bunch of rectangles (actually, “nIntervals” is how many
there are). Each rectangle is “deltaX” wide, and f(x) high. You
may want to check out this url:
http://en.wikipedia.org/wiki/Riemann_sum
By the way, for those of you in or past Calculus II, you can’t
integrate this function in terms of elementary functions – it can’t
be done! For those of you familiar with statistics, this is the NormalCDF function on your
calculator (with default =0 and =1).
4. It should perform all calculations using “double” variables, although nIntervals should be an
int. You will find it convenient to calculate a variable with a name like “deltaX” from xhi,
xlo, and nIntervals. βˆ†π‘₯ =
π‘₯β„Žπ‘– − π‘₯π‘™π‘œ
π‘›πΌπ‘›π‘‘π‘’π‘Ÿπ‘£π‘Žπ‘™π‘ 
5. The program should have four methods: main, getInput, f, and produceOutput.
Variables like xlo, xhi, nIntervals, and deltaX, (but theSum should be in main
only) should be declared in the scope of the Riemann class (probably before main), and thus
are “visible” to all of the methods in the class. Note: when declaring these variables at the class
level, they must be declared “static”: “static double xlo;”, etc.
Methods:
a. The main method should
i. invoke getInput
ii. perform the calculation using the method (function) f (this will involve a loop
to sum up all the areas.)
iii. display the output using produceOutput(theSum).
b. The “getInput” method should explain to the user what the program is to do, and
prompt for the three values: xlo, xhi, and nIntervals, using JOptionPane. You
should use post-test loops to ensure:
Java I CS 142 Week 03-04 Assignment
p. 1 of 2
i. xhi > xlo
ii. 1 < nIntervals <106
c. You will have a method named “f” returning type double in your program. It should
have one double input argument, and implement the function:
𝑓(π‘₯) =
1
𝑒
(−
π‘₯2
)
2
√2πœ‹
d. The “produceOutput(theSum)” method should display the result of the
calculation to the user. You should pass the computed sum into the method as an
argument.
6. Submit your .java file to the OIS dropbox. Click on the “OIS” icon next to CS142 in “My Classes”
in the portal. For full credit, it is due at noon, on Friday, February 3rd. It may be submitted up
to one week late for reduced credit.
7. You MUST use the Eclipse debugger while developing this program.
Notes: you may want to “comment out” the input section during debugging so you don’t have to
enter numbers each time you run. You may want to initially write a really simple “f” function, and
change it to the Gaussian function when you get the basic integration working.
Some extra credit opportunities:
1. Use the Eclipse Window builder rather than using JOptionPane. If you do this, there should be
three input fields on the window, a “Calculate” button, and a label field which will display
answers. You may “borrow” code from another student who has already figured out how to use
the Window builder for the previous assignment, and modify it. If you use Window builder, you
should still use post-test loops to validate input as described above. The code corresponding to
the main program should be in the event handler for the button.
2. Use Simpson’s method to get a more accurate value. If you do so, display both left-Riemann and
Simpson’s results. This is called “Composite Simpson’s rule” in
http://en.wikipedia.org/wiki/Simpson%27s_rule
3. Note to get extra credit, you must claim it three ways: in the Title and Description fields of the
Dropbox, and the program should display (Console or GUI) the claim of extra credit.
Java I CS 142 Week 03-04 Assignment
p. 2 of 2
Download