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
the result of the addition is then assigned back to total
line 7 and 11 the most recently updated value of total is displayed
the sequence of input a number, add the number to total and display total is repeated for each of the 5 events

the program code

Loading…

PRIMM

predict
The program is unfinished and only handles the first three events, you will complete it at the modify task.
Read the code and assume the user enters the event scores as follow:
hurdles: 750, shot-put: 650, long jump: 800
Can you anticipate what would appear on the console when the program is run?
sometimes this is called the expected output

run
go ahead and run the program, enter the example values used above, then run the program again and enter your own test values
look at the results displayed by the program, these are called the actual output
check that the expected output is the same as the actual output

investigate
add this line to your code and run the program
System.out.println("Alternative calculation for total : " + (hurdles + shotput + longJump));
this is not a running total since it does one calculation with all the variables, however imagine if there were thousands of numbers to add
later you will see that the running total method can be organised with a loop to make a running total method that could work with very large lists of numbers in a way that the single calculation could not

modify
Here is the full list of events that the program should process. Read, copy and re-use the code you already have for entering the remaining two events: high jump and 800m run.
here are the full results
hurdles: 750, shot-put: 650, long jump: 800, high jump: 700, 800m: 750

make

Since you will discover a better way to a running total later we will skip the make activity in this task.

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.