CS 152 Numeric Types, Math Operators and Assignment Statements
CS 152 Numeric Types, Math Operators and Assignment Statements
Fundamentals
Numeric Types, Math Operators
and Assignment Statements
Joel Castellanos
e-mail: [email protected]
Web: http://cs.unm.edu/~joel/
Office: Electrical and
Computer Engineering
building (ECE).
Room 233
8/28/2017
Reading
Week 1:
Ch 1: Intro to Computers
Programming, and Java (skip
1.11 and 1.12)
Ch 2: Elementary
Programming
Week 2:
Ch 3: Selections (if, if-else,
switch, and random
Numbers)
1
To See Changes, Reload Web Page
To speed opening of static web pages, your
browser will cache pages, images and other
web data.
However, if the page is not static (that is, it has
changed since you last viewed it) then you must
click "Reload" to overwrite your browser's local
cache with new data from the web.
Lab Schedule
2
Quiz: byte, short, int, long, float, ...
In Java, the keywords byte, short, int, long,
float, and double are:
a) primitive numeric types
b) object types
Section 2.9.1: Primitive Numeric Types
c) variable
byte: -128 totypes
127
short: -32768 to 32767
d) math
int: operators
~2 billion
long: 2 63
e) relational operators
float: about 8 significant figures with exponent to ~ 38
double: about 16 significant figures with exponent to ~ 300
3
Java's 8 Primitive Types
byte: 8-bit, [-128, 127].
Quiz:
Which is the best variable declaration for the
number of people in your family?:
a) boolean foo;
b) boolean familyMemberCount = 1;
c) int familyMemberCount = 1;
d) float familyMemberCount = 1;
e) double familyMemberCount = 1;
4
A Variable of type int
1. public class Hello
2. { public static void main(String[] args)
3. {
4. int x = 0; //Allocates memory Order of
5. System.out.println(x); statement
6. x = x + 2; execution
7. System.out.println(x);
8. x = (x + 1) * 3;
9. System.out.println(x);
10. }
11.}
x is a variable. Output: 0
2
9
3 is a literal. 9
5
Different Ways To Add
1) public class Hello
2) { public static void main(String[] args)
3) { int a = 7;
4) System.out.println(a); //7
5)
6) a = a + 1;
7) System.out.println(a); //8
8)
9) a += 1;
10) System.out.println(a); //9
11)
12) a++;
13) System.out.println(a); //10
14)
15) }
11 16) }
6
Find the Syntax Error
1. public class Hello
2. { public static void main(String[] args)
3. {
4. int z = 2;
5. int z = z + 2;
6. System.out.println(z);
7. }
8. }
Duplicate local variable z
int z = 2; int z = 2;
or int w = z + 2;
z = z + 2;
System.out.println(w);
13
Output: 4
14
7
Printing the Results of Expressions
1. public class Hello
2. {
3. public static void main(String[] args)
4. {
5. System.out.println("5 + 6 + 3 * 2");
6. System.out.println(5 + 6 + 3 * 2);
7. System.out.println((5 + 6 + 3) * 2);
8. }
9. }
8
Integer and Floating Point Division
1. public class Hello
2. {
3. public static void main(String[] args)
4. {
5. System.out.println(5 / 2);
6. System.out.println(5.0 / 2.0);
7. System.out.println(5.0 / 2);
8. System.out.println(5 / 2.0);
9. }
10.}
Output: 2
2.5
2.5 Java casts int 2 into double 2.0 then divides.
17 2.5 Java casts int 5 into double 5.0 then divides.
9
Quiz: Evaluating Expressions
1. public class Hello
2. {
3. public static void main(String[] args)
4. {
5. System.out.println(8 + 11 / 2);
6. }
7. }
10
Math Expressions to Java
3 4 3 8
The mathematics expression:
6 1 (1 3)
An equitant expression in Java is:
(3.0+4.0+3.0) / (6.0–1.0) + 8.0/(1.0+3.0)
Modulus Operator: %
The modulus, n % m, is the integer reminder when integer
n is divided by integer m.
22
11
Quiz: What 3 Numbers are Output?
public static void main(String[] args)
{
int n = 92;
int a = n / 25;
n = n % 25;
int b=n/ 10;
n = n % 10;
System.out.println(a);
System.out.println(b); a) 3.68, 9.2, 0
System.out.println(n); b) 3.68, 9.2, 92
} c) 3.68, 9.2, 15
d) 3, 9, 0
23
e) 3, 1, 7
12