Command |
Explanation |
Examples |
println |
displays the result of a
calculation and/or some text on the screen |
println(3*3+4*4) ==> 25 |
+ - * / |
add , subtract ,
multiply , divide |
4*5 - 6 / 2
==> 17 |
Math.pow(b , e) |
calculates the power b
^ e |
Math.pow(3,4) ==> 81 |
parentheses ( ) |
use parentheses to control
the order of operations |
3*3 + 4*4 ==> 25 3*(3 + 4)*4 ==> 84 |
Find out how numbers are stored in binary in a computer's memory.
Calculate the maximum value that can be stored
in 32 bits.
Hence determine
the maximum factorial value
that can be correctly
calculated by this program, since Java stores integer values in 32 bits.
Describe what happens when the calculation gets too big, e.g. overflows.
Division is a difficult arithmetic operation, so people make
frequent mistakes.
The computer also makes mistakes, but for a different reason.
Question : Why (and when) does division
give incorrect answers?
Investigation
Find out what the computer prints for each of the following
calculations -
write the results in your notebook.
Explanation
Java treats integers
(whole numbers) differently than floating
point (decimal) numbers.
When dividing integers,
Java does it like people would divide up a bunch of coconuts.
7 / 3 ==> divide 7 coconuts among 3 people. We could give
2 coconuts to each person,
but there would still be 1 coconut left over. It's pretty
difficult to divide a coconut into
pieces, so we don't know what to do. The extra probably
doesn't go anywhere, just gets left out.
So 7 / 3 ==> 2 . That is how Java does integer
division.
When dividing decimals,
Java does it correctly, giving a correct decimal answer.
So 7.0 / 3.0 ==> 2.3333333 .
And parentheses are necessary if we want addition to be done
before division.
Java follows the normal math rules for order of operations.
So explain why 1/2 + 1/3 + 1/6 prints 0.
Explanations
For explanations, consult the Processing Help Reference, or
consult a Java textbook.