Unit 4: Loops – Modify a Program
Type or open the source code named MathTutor.java in Code Listing 4-23 on p.254-255.
Add a loop to the program so that the user can determine if they want to try another math problem. After the user is told whether they answered the problem correctly, ask the user if they want to try again. Based on their answer, either repeat the code or quit. Also, add a farewell message at the end (after they choose to quit.)
Hints:
- You will be adding code, you should not alter any of the existing code.
- The portion of the program that should loop occurs from lines 23 – 47.
- Code Listing 4-6 shows an example of the user controlling whether or not to repeat
A working program might looks like this:What is the answer to the following problem:
45 + 32 = ?
77 [Enter]
Correct!
Would you like to try again (‘y’ or ‘n’)?
y [Enter]What is the answer to the following problem:
50 + 12 = ?
72 [Enter]
Sorry, wrong answer. The correct answer is 62.
Would you like to try again (‘y’ or ‘n’)?
y [Enter]
What is the answer to the following problem:
18 + 40 = ?
58 [Enter]
Correct!
Would you like to try again (‘y’ or ‘n’)?
n [Enter]
Thanks for using the Math Tutor!Unit 4: Loops – Programming Project
Chapter 4 Programming Project: Calories Burned
Write a program that calculates the calories burned while running over a given number of miles. While several factors determine how many calories are burned during a mile run, for this program, 85 calories are burned for every mile of running. The cumulative calories burned during a run can be calculated as:
Calories * Distance
For example, if a person runs 5 miles, the total calories burned is 400. After 3 hours, the person has burned 255 calories.
The program should ask for the number of miles ran. It should use a loop to display the total calories burned after each mile. For example, if the user ran 5 miles, the program should display a report similar to the following:
Miles run Calories Burned
1 85
2 170
3 255
4 340
5 425
Input Validation: Do not accept a value less than 0 or greater than 50
Note: You must use a loop to calculate the calories burned after each mile and generate a table similar to the one above to receive credit.
0 comments