Name: ________________________________________________________________________________________
Objective: For loops
Quiz:
Write the following for loop as a while loop. Make sure you fix the bug.
String foo = myScanner.readString();
String fie = ""
for (int i = 0; i <= foo.length(); ++i) {
char c = foo.charAt(i);
if (c == 'X') {
break;
} else {
fie += c;
}
}
Instructions
You will create an application that uses Leibniz's method for estimating Pi, which is based on the following series:
pi/4 = 1 - 1/3 + 1/5 - 1/7 + ...
- Create a new project called Pi
- Down import the files from Lab 15 Pi
- Modify the imported application as followings
- As the user for an integer.
- Your program will then use the integer as the number of iterations in the sequence that you will calculate to do your estimate. In other words, if the user enters 3, you will calculate 1 - 1/3 + 1/5. If the user enters 7 you will calculate 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/3.
Design Considerations
- The sequence might be easier to define if you recognize that 1 can be written as 1/1.
- You can assume the user will enter positive integer; However, there's no maximum value.
Grading
| Feature |
Points |
Out of |
Notes |
| Input |
|
1 |
|
| Loop |
|
3 |
|
| Calculation |
|
4 |
|
| Quiz |
|
2 |
|
| Total |
|
10 |
|
| |
|
|
|
Notes
Comments (0)
You don't have permission to comment on this page.