0% found this document useful (0 votes)
26 views

CS 152 Numeric Types, Math Operators and Assignment Statements

This document provides an overview of topics covered in a CS 152 Computer Programming Fundamentals course including numeric types, math operators, and assignment statements. It lists reading assignments for the first two weeks covering introductions to computers, programming, and Java as well as selections, if/else statements, and random numbers. It also includes the course instructor's contact information and lab schedules.

Uploaded by

CK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

CS 152 Numeric Types, Math Operators and Assignment Statements

This document provides an overview of topics covered in a CS 152 Computer Programming Fundamentals course including numeric types, math operators, and assignment statements. It lists reading assignments for the first two weeks covering introductions to computers, programming, and Java as well as selections, if/else statements, and random numbers. It also includes the course instructor's contact information and lab schedules.

Uploaded by

CK
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

CS 152 Computer Programming

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

CS-152 1 Thu 1400-1520 ESCP-109 Rashid


CS-152 2 Thu 1230-1350 ESCP-110 Andrew
CS-152 3 Tue 1400-1515 ESCP-109 Tommy
CS-152 4 Tue 0930-1050 ESCP-109 Rafael
CS-152 5 Fri 1200-1320 ESCP-109 Rashid
CS-152 6 Wed 1200-1320 ESCP-109 Javier
CS-152 7 Tue 1230-1350 ESCP-110 Jacob
CS-152 8 Thu 0930-1045 ESCP-109 Jordan

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

float: about 8 significant figures


public static void main(String[] args)
{
float a = 0.1f;
System.out.println(10 + a);
System.out.println(1000 + a);
System.out.println(1000000 + a);
System.out.println(10000000 + a);
}
3 significant figures
10.1 5 significant figures
1000.1
1000000.1 8 significant figures
1.0E7
= 10000000.0
6

3
Java's 8 Primitive Types
byte: 8-bit, [-128, 127].

short: 16-bit, [-32,768, 32,767] .

int: 32-bit, [-2,147,483,648, 2,147,483,647].

long: 64-bit, [-9,223,372,036,854,775,808,


9,223,372,036,854,775,807].
float: 32-bit,[1.4x10-45, 3.4028235x1038]

double: 64-bit, [4.9x10-324, 1.7976931348623157x10308]

boolean: Only two possible values: true and false.

char: 16-bit, ['\u0000' (0), '\uffff' (65,535) ].


7

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

A Variable’s Value is a Function of Time


1. public class Hello
2. { public static void main(String[] args)
3. {
4. int a = 2; //At this statement, b is undefined.
5. int b = 3;
6. a = a + b; //read a: 2, read b: 3, write a: 5
7. b = a + b; //read a: 5, read b: 3, write b: 8
8. System.out.println(a);
9. System.out.println(b);
10. }
11.}
Output: 5
8
10

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) }

Quiz: A Variable of type int


1. public class Hello
2. { public static void main(String[] args)
3. {
4. int z = 2;
5. z = z + 2;
6. z = (z + 3) * 2;
7. System.out.println(z);
8. }
9. }

When run, what characters are displayed in the console?


a) z b) 2
c) 4 d) 10 e) 14
12

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

Warning: Local Variable Never Read


1. public class Hello
2. { public static void main(String[] args)
3. {
4. int a = 2;
5. int b = 3;
6. a = a * 2;
7. System.out.println(a);
8. }
9. }

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. }

Order of some operators: Output: 5 + 6 + 3 * 2


First: ( ) parenthesis 17
Second: *, / multiplication, division
28
Third: +, - addition and subtraction
15

Quiz: Order of Operations


1. public class Hello
2. { public static void main(String[] args)
3. {
4. int x = 5;
5. int y = 10;
6. y = y - x / 2;
7. System.out.println(y);
8. }
9. }

When run, what characters are displayed in the console?


a) 8 b) 7.5
c) 3 d) 2.5 e) 2
16

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.

Double, float and int


1. public static void main(String[] args)
2. { System.out.println(1.0 / 3.0);
3. System.out.println(1.0f / 3.0f);
4. System.out.println(1000.0f / 3.0f);
5. System.out.println(3.0*(1.0f / 3.0f));
6. System.out.println(3.0*(1 / 3));
7. }

Type Size Range Significant Output:


Figures
0.3333333333333333
int 4 bytes ±2 billion exact integers
0.33333334
float 4 bytes ±3.4 x 1038 about 7
333.33334
double 8 bytes ±1.8 x 10308 about 15 1.0000000298023224
18
0.0

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. }

When run, what characters are displayed in the console?


a) 10 b) 9
c) 9.5 d) 13 e) 13.5
19

Math Expressions to Java


3 2
The mathematics expression:  A Java expression is
5 3 only part of a Java
statement.
An equitant expression in Java is:
3.0 / 5.0 + 2.0 / 3.0 An expression does not
end with a semicolon.
This Java expression can be used in a Java program:
1. public class Hello
2. {
3. public static void main(String[] args)
4. {
5. System.out.println(
6. 3.0 / 5.0 + 2.0 / 3.0);
7. }
20
8. }

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)

1. public class Hello


2. {
3. public static void main(String[] args)
4. {
The semicolon
5. System.out.println( goes at the end
6. (3.0+4.0+3.0) / (6.0–1.0) + of the Java
7. 8.0/(1.0+3.0)); statement, not
8. } at the end of
each line of
9. } code.
21 Output: 4.0

Modulus Operator: %
The modulus, n % m, is the integer reminder when integer
n is divided by integer m.

System.out.println("1 % 3 = " + 1 % 3); 1


System.out.println("2 % 3 = " + 2 % 3); 2
System.out.println("3 % 3 = " + 3 % 3); 0
System.out.println("4 % 3 = " + 4 % 3); 1
System.out.println("5 % 3 = " + 5 % 3); 2
System.out.println("6 % 3 = " + 6 % 3); 0
System.out.println("7 % 3 = " + 7 % 3); 1

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

You might also like