1 is left over from dividing 7/2

advertisement
Name: _________________________________________________
Variables Most Often Used
Data
Type
Character
Java
Keyword
char
Kind of Value
1 character - Unicode
Bytes of
Memory
2
integer
int
Integers
4
double
double
Decimal values to 15
decimal digit precision
8
not applicable
-2,147,483,648 to
2,147,483,647
(-231 to 231 - 1)
1.7e-308 to 1.73e308
positive and negative
boolean
boolean
1
true or false 1 or 0
Character
char
Boolean (Logical) values
True or False
Used to store one character.
Use ‘ ‘ single quotes around
the character
Range of Values
‘c’
‘A’
What data type would you use for the following? If you could use more than one list more than one
1. 248 ___________________________
2. 43925 _________________________
3.
28000 _________________________
4. ‘C’
__________________________
5. true ___________________________
6. 1982483546
____________________
7. 1.6785 ___________________________
8. 376.25 __________________________
OPERATORS IN JAVA
Basic operators:
Description
Parentheses
Operators
()
Associativity
L to R
* / %
L to R as they appear
multiplication, division, modulus
+ -
addition, subtraction
L to R as they appear
What is integer division?
An int is a whole number. When you divide with integers, the remainder is truncated (done away
with). You are left with just the whole number.
Example:
7/2 = 3
5/2 = 2
14 / 3 = 4
What is dividing by double?
When you divide by a double, the answer will be a double.
a double, the answer will be a double.
Example: 7/2 = 3
15/4 = 3
What is modulus?
remainder.
7.0/2 = 3.5
15.0/4 = 3.75
So when one number is an int and one is
7/2.0 = 3.5
Modulus is the remainder operator. You will divide and then the answer is the
7/2 = 3
3
2 7
7
-6____
1
7%2 = 1
answer is 1
(1 is left over from dividing 7/2)
Do not use a calculator. It is not the decimal number from
dividing.
Write the answer to the equations below:
32/ 3
32.0 / 3
14/5
14.0 / 5
18 / 4
18.0 / 4
1. What did you observe about dividing with integers?
2. What did you observe when dividing with a decimal (float value)
Modulus Operator:
Type these into the interactions pane and record the result.
Modulus is the % sign and it is the remainder from dividing.
integer answer.
32 % 3
14 % 5
18 % 4
18 % 4
12 % 5
Integer division will result in an
Project 2:
Description
Parentheses
multiplication, division, modulus
addition, subtraction
Operators
()
Associativity
L to R
* / %
L to R (equal precedence as they
appear left to right)
L to R (equal precedence as they
appear left to right)
+ -
Work these out (show your work. Then type them into the interactions pane to check your work.
Example:
1. 3 * 6 - 4 / 2
3* 6 = 18
4/2 = 2
18-2 = 16
2.
6/3*2
3.
((4+9) * 2+3) * 2) + 1
4.
2 + 3 * 2 + 15 / 5
5. 12 + 3 % 4 + 1
Variable Worksheet
Name: ____________________________________
How to create variable
Starts with Lower case
One word no spaces
Use camel case if put two words together
ex: camelCase
Can have a number, _ or $ in the variable name. Those are the only symbols.
These are valid:
camel_Case;
camelCase2;
camelCase$
Use the correct data type and correct a valid variable name for each of the following.
1. ________________________________ = 3; // number of people
2. ________________________________ = 32.32; // price of an item
3. ________________________________ = false; // value of a done flag
4. ________________________________ = ‘a’;
// key to press
5. ________________________________ = 2;
// class period number
6. ________________________________ = 30.0; // price of dinner
7. ________________________________ = true; // is done
8. ________________________________ = ‘x’; // character to press to stop
Initialize the value of each of the following variables. Be sure to use a valid value for the type.
1. int count = ____________________________________________________
2. double total = __________________________________________________
3. char stopKey = _________________________________________________
4. boolean stopFlag = ______________________________________________
5. double result = _________________________________________________
6. int numPets = ___________________________________________________
7. char upKey = ___________________________________________________
Download