* for multiplication + for addition - for subtraction / for division (beware of integer division, as explained below)
% for modulus (the remainder from integer division) = for assignment (stroring the result of operation at right in
variable at left; think of it like an arrow to the left)
+= adds a value to a variable. Similarly, to subtract from or multiply or divide
by a value, or take the modulus of a value, use: -= or *= or /= or
%=
++ increments a variable. So x++; is equivalent to x += 1;
and x = x + 1;
-- decrements a variable
x = Math.pow(2, 3);
(double)x
int num_chars, num_words;
double avg_length;
String input;
input = JOptionPane.showInputDialog("How many characters are in the document?");
num_chars = Integer.parseInt(input);
input = JOptionPane.showInputDialog("and how many words are there?");
num_words = Integer.parseInt(input);
avg_length = (double)num_chars / num_words;
JOptionPane.showMessageDialog(null, "The average word length is " + avg_length + " characters.");
/* Sample Output:
How many characters are in the document? 100
and how many words are there? 28
The average word length is 3.57143 characters.
*/
// without the type cast, the average word length calculated would have been 3
Busses.java: Solution to above in-class exercise