6 caculate

icon picker
arithmetic

input process and output a result which involves an arithmetic calculation

program requirement

Health and fitness apps are good examples of programs that”crunch number” to calculate a result. Many people like to track the distance they cover in a day.
This program should ask the user to enter and store number values for the walking distance and the running distance they cover. The program should then calculate the total by adding the walking and running distances together. This total should be displayed for the user.

learning point

an arithmetic expression or calculation is written as a kind of formula including numeric values or variables and arithmetic operators
the arithmetic operators are
add (+), subtract (-), multiply (*) and divide(/), there are others that will be covered another time
the program evaluates or “works out” the value or result of the expression in a similar way to how a student would evaluate an expression in mathematics
the integer result that is produced by the evaluation is then assigned to another integer variable
checking that the output is very important now and you will need to know what the expected output will be to ensure the program has the correct result

expected output

What is your walking distance? 1234
What is your running distance? 2345
You're total distance is 3579
understanding the output
the user has entered two integer numbers which the program has used to calculate a total
the total is displayed for the user to read

introducing the code

// INPUT
1 System.out.print("What is your walking distance? ");
2 walk = keyboard.nextInt();
3 System.out.print("What is your running distance? ");
4 run = keyboard.nextInt();
// PROCESS
5 totalDistance = walk + run;
// OUTPUT
6 System.out.println("You're total distance is " + totalDistance);
understanding the code
line 5 includes the expression walk + run which the program evaluates to produce a result
the current content of the two variables (input by the user) are added together
+ is the addition operator (since it is placed between two integer values)
the result of evaluating the arithmetic expression is an integer value
the value that results from the addition is assigned to the variable totalDistance
it is important that totalDistance is also an integer variable since it has to store the result which is an integer

the program code

Loading…

PRIMM

predict
Read the code and assume the user has walked 1500 (metres) and run 2500 (metres).
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
what happen if you changes the calculation to this?
totalDistance = run + walk;

modify
Here is the full dialog that the program should create with the user. Read, copy and re-use the code you already have for entering the total distance.
what new variables will you need?
what will the calculation look like, what operator will you need
What is your walking distance? 1234
What is your running distance? 2345
You're total distance is 3579
What is your stride length (m)? 0.75
How many stpes did you take? 3456
You're total distance is 2592 metres

make

Another fitness app measure is the number of flights of stairs climbed (or the equivalent). A user may use such an app to plan how to set and keep a target. For example to climb the equivalent of 100 flights a month a user may need to track how many flights still have to be climbed and how many days are left.
Can you create an Input-Process-Output sequence to calculate the number of flights still left to climb and the average per day to reach the target. The output dialog would look like this.
What is stair flight climbed so far this month? 35
What is your target for climbing stair flights? 100
How many days left for training? 10
The number of flights to climb is: 65
The number of flights to climb each training day is: 6.5
Think carefully about how you would do the calculation and what types of data and what variables would be involved.

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.