Java I CS 142 Week 04 Assignment.docx

advertisement
Java I CS 142 Week 04 Assignment
You will write a program to integrate the function 2x2 + 7x + 15 between xland xr, using n divisions of the interval (xl,xr).
Wow! That sounds hard and calculus-ey. But it is actually a very simple application of loops.
Your program will begin by displaying your name, so I know where to put the grade). Then it should askg the user for the
numbers xl and xr. You can use console I/O (Scanner) or JOptionPane to get these numbers. They should both be
doubles. But here is a catch: xr must be greater than xl. If the user tries to enter, for example, xl=10 and xr=5, your
program should tell the user that xr must be greater than xl, and allow the user to re-enter both values. This is called a
validation loop.
When your program has good values for xl and xr, it should prompt the user for “How many divisions of the interval?”
This should be an integer number (int). It should not be smaller than 2, or larger than one million. Again, you will use a
validation loop to ensure that the number “n” is an OK number.
Now your program needs to calculate a “deltaX” = (xr-xl)/n. That will be the width of each rectangle, and your program
is going to add up a lot of rectangles of width “deltaX”. The height of each rectangle is going to be 2x2 + 7x + 15 for some
x in the interval, so let’s just use the “left x”. You will use a loop to add up the areas of the n rectangles.
Your program will loop a variable (“x”)
starting at xl and stopping at xr-deltaX,
incrementing by deltaX each time. Each
iteration of the loop will add the area of
another rectangle. Each area will of course
be f(x)*deltaX, and f(x) will be 2x2 + 7x + 15
.
Since you are adding up a lot of rectangles,
you will want to start with a value of 0 in
some variable, and add to it (using the +=
operator) each time.
Finally, your program will print out its input
values and the total sum (formatted to 5
digits after the decimal point). Your
program should be named Week04.java,
and you should submit it to the drop box
by the end of class next Tuesday. I will be
in California for a conference on Tuesday, so I will have a substitute (who doesn’t know Java ) with you in class next
Tuesday. Be nice to her.
Download