6 caculate

icon picker
updating a total

use input, process, output to calculate total points for an athletics event

program requirement

Event organisers for athletics events use technology to help process results data. In multi-discipline sports like the pentathlon there are scores for each event and these are added together to produce a total score. One method of calculating the total is to add on each score value one by one. After each addition the total is updated until the final addition reaches the final total.
This program should ask the user to enter and store number values for the score for each event. The program should update the total by adding on each event score in turn. After the last event score is added the final total should be displayed.

learning point

Running Total is a method of calculating a total by adding on a sequence of number values to repeatedly update the total. The total always starts at 0.
It is called running total because it is always changing as each number is added on
total is 0
numbers to add are: 2 + 3 + 4 + 5
add 2: total is 0 + 2, makes 2
add 3: total is 2 + 3, makes 5
add 4: total is 5 + 4, makes 9
add 5: total is 9 + 5, makes 14 - this is the final value for total
the algorithm for updating a total has three parts
set the total to 0 at the start
update total by adding on another number values, this is repeated for every new value
display the final value of total at the end

expected output

Please enter hurdles score: 748
After 1 event the total is : 748
Please enter shot put score: 685
After 2 events the total is : 1433
Please enter 800m score: 771
After 5 events the total is : 3567
You're final total is 3567
understanding the output
the user has entered 5 integer numbers, one for each event in the sport (maximum score is 1000 per event)
after each score is typed in the total is updated and displayed
after the last score is entered the addition produces the final value of the total

introducing the code

1 int hurdles = 0;
2 int run800m = 0;
3 int total = 0;
// input
4 System.out.print("Please enter hurdles score: ");
5 hurdles = Integer.parseInt(keyboard.nextLine());
// update and output
6 total = total + hurdles;
7 System.out.println("After 1 event the total is : " + total);
8 System.out.print("Please enter shot put score: ");
9 shotput = Integer.parseInt(keyboard.nextLine());
10 total = total + shotput;
11 System.out.println("After 2 events the total is : " + total);
understanding the code
line 1-3 the variables need for the scores each of the 5 events and the total are initialised to 0
line 4, 5 and 8, 9 the user is prompted for an event score, the score is stored in one of the variables
line 6 and 10 the total is updated by adding on the most recent score
notice that total appears on both sides of the = sign
the expression total + hurdles is evaluated to calculate the sum of the two numbers
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
) instead.