|
Understanding Java Logical Control and PtPlot
You have to write a Java program calculating the sum following sum:
1-1/4+1/9-1/16+...
- start by creating a class Sum [you will need to create a file Sum.java
for it]
- import ptolemy.plot*.
- in your main method:
- create double variable sum, initialize it with 0
- create a double variable ds, initialize it with 1 (this will be your sum
term)
- create a double variable i, initialize it with 1 (this will be your loop
counter)
- create two boolean variables b1, b2, initialize them with false
- define Plot variable myPlot
- start a do loop, in each iteration of do loop:
- increase sum: sum=sum+ds;
- change ds for next iteration: ds=ds/(abs(ds)*i*i);
- add a point (i,sum) to the first (0) set of myPlot
- add a point (i,ds) to the second (1) set of myPlot
- if b1 is false and absolute value of ds is smaller than 10-4,
assign true to b1
- if b1 is true and b2 is false, print (on screen) the current number of iterations, and
sum value; also assign true to b2
- increase i by 1
- check whether the absolute value of ds is larger than 10-5
- exit loop when absolute value of ds becomes smaller than 10-5
- print the total number of iterations (on screen), add this output as a
comment to your code
- display myPlot, print it (on paper)
- submit: code (along with the output it prints), and the printed graph(s)
top
|