Computer Science 1: CPS109 With Prof. Kosta Derpanis

Download as pdf or txt
Download as pdf or txt
You are on page 1of 246

CPS109

COMPUTER SCIENCE 1
with Prof. Kosta Derpanis

Fundamental Data Types

Part 1
CPS109

COMPUTER SCIENCE 1
with Prof. Kosta Derpanis

Fundamental Data Types

Part 1
Administration
tinyurl.com/RU-N
ONO

Administration
m / C P S 1 0 9 - F 2 0 1 7
tinyurl.co
o o k
x t b
Te
, B i g Java : L ate O bj ec t s
H o rs t m a nn
LECTURE TOPICS
LECTURE TOPICS
Data types
LECTURE TOPICS
Data types
Variables
LECTURE TOPICS
Data types
Variables
Arithmetic expressions
Binary Representation
Binary Representation
Internal
Represen
tation

1 0 1 0 1 1 1 1
Internal
Represen
tation

bit (0 or 1)

1 0 1 0 1 1 1 1
Internal
Represen
tation

bit (0 or 1)

1 0 1 0 1 1 1 1
byte (group of 8 bits)
10100011
00001000
11001100
10001100
10001101
11001101
00101010
10001100
2052 10100011
2053 00001000
2054 11001100
2055 10001100
Byte address 2056 10001101
2057 11001101
2058 00101010
2059 10001100
2052 10100011
2053 00001000
2054 11001100
2055 10001100
Byte address 2056 10001101
2057 11001101
2058 00101010
2059 10001100
Binary
System

Decimal 0 1 2 3 4 5
Binary
System

Decimal 0 1 2 3 4 5

Binary
Binary
System

Decimal 0 1 2 3 4 5

Binary 0
Binary
System

Decimal 0 1 2 3 4 5

Binary 0 1
Binary
System

Decimal 0 1 2 3 4 5

Binary 0 1 10
Binary
System

Decimal 0 1 2 3 4 5

Binary 0 1 10 11
Binary
System

Decimal 0 1 2 3 4 5

Binary 0 1 10 11 100
Binary
System

Decimal 0 1 2 3 4 5

Binary 0 1 10 11 100 101


Hexadec
imal
System

Decimal 0 1 2 3 4 5 6 7 8 9
Hexadec
imal
System

Decimal 0 1 2 3 4 5 6 7 8 9

Hexadecimal
Hexadec
imal
System

Decimal 0 1 2 3 4 5 6 7 8 9

Hexadecimal 0
Hexadec
imal
System

Decimal 0 1 2 3 4 5 6 7 8 9

Hexadecimal 0 1
Hexadec
imal
System

Decimal 0 1 2 3 4 5 6 7 8 9

Hexadecimal 0 1 2
Hexadec
imal
System

Decimal 0 1 2 3 4 5 6 7 8 9

Hexadecimal 0 1 2 3 4 5 6 7 8 9
Hexadec
imal
System

Decimal 0 1 2 3 4 5 6 7 8 9 10

exadecimal 0 1 2 3 4 5 6 7 8 9 A
Hexadec
imal
System

ecimal 0 1 2 3 4 5 6 7 8 9 10 11

ecimal 0 1 2 3 4 5 6 7 8 9 A B
Hexadec
imal
System

3 4 5 6 7 8 9 10 11 12 13 14 15

3 4 5 6 7 8 9 A B C D E F
Hexadec
imal
System

5 6 7 8 9 10 11 12 13 14 15 16

5 6 7 8 9 A B C D E F 10
Integers

5 5 1 9 3 2
10
Integers

105 104 103 102 101 100

5 5 1 9 3 2
10
105 104 103 102 101 100

5 5 1 9 3 2
10
105 104 103 102 101 100

5 5 1 9 3 2
10
105 104 103 102 101 100

5 5 1 9 3 2
10
2 x 100 = 2
105 104 103 102 101 100

5 5 1 9 3 2
10
2 x 100 = 2
105 104 103 102 101 100

5 5 1 9 3 2
10
2 x 100 = 2
3 x 101 = 30
105 104 103 102 101 100

5 5 1 9 3 2
10
2 x 100 = 2
3 x 101 = 30
105 104 103 102 101 100

5 5 1 9 3 2
10
2 x 100 = 2
3 x 101 = 30
9 x 102 = 900
105 104 103 102 101 100

5 5 1 9 3 2
10
2 x 100 = 2
3 x 101 = 30
9 x 102 = 900
1 x 103 = 1000
105 104 103 102 101 100

5 5 1 9 3 2
10
2 x 100 = 2
3 x 101 = 30
9 x 102 = 900
1 x 103 = 1000
5 x 104 = 50000
105 104 103 102 101 100

5 5 1 9 3 2
10
2 x 100 = 2
3 x 101 = 30
9 x 102 = 900
1 x 103 = 1000
5 x 104 = 50000
5 x 105 = 500000
105 104 103 102 101 100

5 5 1 9 3 2
10
2 x 100 = 2
3 x 101 = 30
9 x 102 = 900
1 x 103 = 1000
5 x 104 = 50000
5 x 105 = 500000

551932
Binary-to
-decimal

1 0 0 1 0 1
2
Binary-to
-decimal

25 24 23 22 21 20

1 0 0 1 0 1
2
25 24 23 22 21 20

1 0 0 1 0 1
2
25 24 23 22 21 20

1 0 0 1 0 1
2
25 24 23 22 21 20

1 0 0 1 0 1
2
1 x 20 = 1
25 24 23 22 21 20

1 0 0 1 0 1
2
1 x 20 = 1
25 24 23 22 21 20

1 0 0 1 0 1
2
1 x 20 = 1
0 x 21 = 0
25 24 23 22 21 20

1 0 0 1 0 1
2
1 x 20 = 1
0 x 21 = 0
25 24 23 22 21 20

1 0 0 1 0 1
2
1 x 20 = 1
0 x 21 = 0
1 x 22 = 4
25 24 23 22 21 20

1 0 0 1 0 1
2
1 x 20 = 1
0 x 21 = 0
1 x 22 = 4
0 x 23 = 0
25 24 23 22 21 20

1 0 0 1 0 1
2
1 x 20 = 1
0 x 21 = 0
1 x 22 = 4
0 x 23 = 0
0 x 24 = 0
25 24 23 22 21 20

1 0 0 1 0 1
2
1 x 20 = 1
0 x 21 = 0
1 x 22 = 4
0 x 23 = 0
0 x 24 = 0
1 x 25 = 32
25 24 23 22 21 20

1 0 0 1 0 1
2
1 x 20 = 1
0 x 21 = 0
1 x 22 = 4
0 x 23 = 0
0 x 24 = 0
1 x 25 = 32

37
Base-to-
decimal

N
X 1
i
General rule mi b
i=0

b 5 b 4 b 3 b 2 b 1 b 0

m5 m4 m3 m2 m1 m0
b
Data
Types

Define the set of values and operations


8
Primitive Types
Primitive
Types
Primitive
Types
Primitive
Types
Primitive
Types
Unicode
(Partial) T
able
Primitive
Types
Primitive
Types
Primitive
Types
Primitive
Types

y p es are N OT o bj e c t s
Primitive t
Primitive
Value Exa
m ples
Primitive
Value Exa
m ples
Integer
Primitive
Value Exa
m ples
Integer
0 -5 360 12000
Primitive
Value Exa
m ples
Integer

Floating-point
Primitive
Value Exa
m ples
Integer

Floating-point
0.15 -33.3 3.14159
Primitive
Value Exa
m ples
Integer

Floating-point

Character
Primitive
Value Exa
m ples
Integer

Floating-point

Character
‘a‘ ‘A‘ ‘#‘ ‘ ‘ ‘\n‘
Primitive
Value Exa
m ples
Integer

Floating-point

Character

Boolean
Primitive
Value Exa
m ples
Integer

Floating-point

Character

Boolean
true false
Variables
Variables

Named place to store data


Variables

Named place to store data

Reserves space in memory


Variables

Named place to store data

Reserves space in memory

Must be declared before using


Variable
Declaratio
n

type variableName
Variable
Declaratio
n

type variableName
int, char, double,
etc.
Variable
Declaratio
n

type variableName
l e g a l i d ent if i e r
must be
Variable
Declaratio
n

type variableName
char initial; /* 16 bits allocated */
int count; /* 32 bits allocated */
double time; /* 64 bits allocated */
type
char initial; /* 16 bits allocated */
int count; /* 32 bits allocated */
double time; /* 64 bits allocated */
char initial; /* 16 bits allocated */
int count; /* 32 bits allocated */
variable n ame
double time; /* 64 bits allocated */
char initial; /* 16 bits allocated */
int count; /* 32 bits allocated */
double time; /* 64 bits allocated */
byte number;

memory

memory address
byte number;

memory

memory address
byte number;

memory
number

memory address 197508


=
=
assigns variable on left the value
on right side
=
assigns variable on left the value
on right side

O T compare values
Does N
=
assigns variable on left the value
on right side

N O T compare values
D o e s
Assignme
nt

initial = 'a';
count = 5;
rate = factor;
time = time + 1;
Assignme
nt

initial = 'a';
count = 5;
rate = factor;
time = time + 1;
Assignme
nt

initial = 'a';
count = 5;
rate = factor;
time = time + 1;
Assignme
nt

initial = 'a';
count = 5;
rate = factor;
time = time + 1;
Assignme
nt

initial = 'a';
count = 5;
rate = factor;
time = time + 1;
Assignme
nt

initial = 'a';
count = 5;
rate = factor;
time = time + 1;
byte number;

number = 7;

memory

memory address
byte number;

number = 7;

memory

memory address
byte number;

number = 7;

memory
number

memory address 197508


byte number;

number = 7;

memory
number

memory address 197508


byte number;

number = 7;

memory
number

memory address 197508


byte number;

number = 7;

memory
0 0 0 0 0 1 1 1

number
memory address 197508
char initial;

initial = 'a';

memory

memory address
char initial;

initial = 'a';

memory

memory address
char initial;

initial = 'a';

memory
initial

memory address
char initial;

initial = 'a';

memory
initial

memory address
Unicode
Table
char initial;

initial = 'a';

memory
initial

memory address
char initial;

initial = 'a';

memory
0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 1
initial
memory address
Multiple
Declaratio
ns

int num1, num2, num3;


Multiple
Declaratio
ns

int num1, num2 = 5, num3;


int num1, num2 = 5, num3;

num1 = num2 + num3;

Spot the p rob l e m ?


int num1, num2 = 5, num3;

num1 = num2 + num3;

Spot the p rob l e m ?


int num1, num2 = 5, num3;

num1 = num2 + num3;

Variables must be initi


alized prior
to usage
int num1, num2 = 5, num3;

num1 = num2 + num3;

Unresolved compilation problem:


The local variable num3 may not have been
initialized
public class SimpleAssignment
{
public static void main(String[] args)
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
What is th e o u t p u t ?
public class SimpleAssignment
{
public static void main(String[] args)
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
outputSystem.out.println(num1);
argument with no carr
System.out.print("num2 = i");
age return
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
What is th e o u t p u t ?
public class SimpleAssignment
{
public static void main(String[] args)
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
What is th e o u t p u t ?
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
Console
}
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
Console
}
java SimpleAssignment
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
Console
}
java SimpleAssignment

num1 = 5
num2 = 1
num3 = 1
public class SimpleAssignment
{
public static void main(String[] args)
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment
{
public static void main(String[] args)
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 1
{
public static void main(String[] args)
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 1
{ num2 = undefined
public static void main(String[] args)
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 1
{ num2 = undefined
public static void main(String[] args)
num3 = undefined
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 1
{ num2 = undefined
public static void main(String[] args)
num3 = undefined
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 1
{ num2 = 1
public static void main(String[] args)
num3 = undefined
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 1
{ num2 = 1
public static void main(String[] args)
num3 = undefined
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 1
{ num2 = 1
public static void main(String[] args)
num3 = 1
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 1
{ num2 = 1
public static void main(String[] args)
num3 = 1
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 5
{ num2 = 1
public static void main(String[] args)
num3 = 1
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 5
{ num2 = 1
public static void main(String[] args)
num3 = 1
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 5
{ num2 = 1
public static void main(String[] args)
num3 = 1
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 5
{ num2 = 1
public static void main(String[] args)
num3 = 1
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 5
{ num2 = 1
public static void main(String[] args)
num3 = 1
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 5
{ num2 = 1
public static void main(String[] args)
num3 = 1
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment num1 = 5
{ num2 = 1
public static void main(String[] args)
num3 = 1
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
public class SimpleAssignment
{
public static void main(String[] args)
{
int num1 = 1, num2, num3;

num2 = num1;
num3 = num2;
num1 = 5;

System.out.print("num1 = ");
System.out.println(num1);
System.out.print("num2 = ");
System.out.println(num2);
System.out.print("num3 = ");
System.out.println(num3);
}
}
Identifiers
identifier
names used for variables, methods and
classes
Identifier
Rules
Identifier
Rules
Must not be a reserved word
Identifier
Rules
Must not be a reserved word
abstract default if package synchronized
assert do implements private this
boolean double import protected throw
break else instanceof public throws
byte extends int return transient
case false interface short true
catch final long static try
char finally native strictfp void
class float new super volatile
const for null switch while
continue goto
HelloWor
ld.java

public class HelloWorld


{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}
HelloWor
ld.java

public class HelloWorld


{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}

Java rese r ved wo rd s


Identifier
Rules
Must not be a reserved word
abstract default if package synchronized
assert do implements private this
boolean double import protected throw
break else instanceof public throws
byte extends int return transient
case false interface short true
catch final long static try
char finally native strictfp void
class float new super volatile
const for null switch while
continue goto
Identifier
Rules
Must not be a reserved word

Must contain only letters, digits, underscores


or dollar sign
Identifier
Rules
Must not be a reserved word

Must contain only letters, digits, underscores


or dollar sign
dollar signs are legal b
ut should be avoided
Identifier
Rules
Must not be a reserved word

Must contain only letters, digits, underscores


or dollar sign
valid: _Abc8
Identifier
Rules
Must not be a reserved word

Must contain only letters, digits, underscores


or dollar sign
valid: _Abc8
invalid: Abc-8
Identifier
Rules
Must not be a reserved word

Must contain only letters, digits, underscores


or dollar sign

Must never begin with a digit


Identifier
Rules
Must not be a reserved word

Must contain only letters, digits, underscores


or dollar sign

Must never begin with a digit


valid: U238 and _765
Identifier
Rules
Must not be a reserved word

Must contain only letters, digits, underscores


or dollar sign

Must never begin with a digit


valid: U238 and _765
invalid: 7abc and 67_q
Identifier
Rules
Must not be a reserved word

Must contain only letters, digits, underscores


or dollar sign

Must never begin with a digit

CASE SENSITIVE
Abc _32 32X Price$ file?1

int abc87 45% return CHAR


Abc _32 32X Price$ file?1

int abc87 45% return CHAR

are va l i d i d ent if i ers ?


Which
Abc _32 32X Price$ file?1

int abc87 45% return CHAR

are va l i d i d ent if i ers ?


Which
int y;
int Y;
int yY;
int yy;
int YY;
int YYY;
int y;
int Y;
int yY;
int yy;
int YY;
int YYY;

i t h var i a b l e n a m es ?
Issue w
Java Nam
ing
Conventio
n

Variable and method names start with a


lowercase letter
Java Nam
ing
Conventio
n

Variable and method names start with a


lowercase letter

Class names start with an uppercase letter


Cam e l c as e
Cam e l c as e

class: HelloWorld
method: outputLine()
variable: listHead
Arithmetic expressions
Unary
Operators
Unary
Operators

Unary Plus (+)


+2 is 2
Unary
Operators

Unary Plus (+)


+2 is 2
Unary Minus (-)
(–(–2)) is 2
int x = 3;

System.out.print("+x = ");
System.out.println(+x);

System.out.print("-x = ");
System.out.println(-x);

System.out.print("x = ");
System.out.println(x);

Output
int x = 3;

System.out.print("+x = ");
System.out.println(+x);

System.out.print("-x = ");
System.out.println(-x);

System.out.print("x = ");
System.out.println(x);

Output
int x = 3;

System.out.print("+x = ");
System.out.println(+x);

System.out.print("-x = ");
System.out.println(-x);

System.out.print("x = ");
System.out.println(x);

Output
int x = 3;

System.out.print("+x = ");
System.out.println(+x);

System.out.print("-x = ");
System.out.println(-x);

System.out.print("x = ");
System.out.println(x);

Output
+x = 3
int x = 3;

System.out.print("+x = ");
System.out.println(+x);

System.out.print("-x = ");
System.out.println(-x);

System.out.print("x = ");
System.out.println(x);

Output
+x = 3
int x = 3;

System.out.print("+x = ");
System.out.println(+x);

System.out.print("-x = ");
System.out.println(-x);

System.out.print("x = ");
System.out.println(x);

Output
+x = 3
-x = -3
int x = 3;

System.out.print("+x = ");
System.out.println(+x);

System.out.print("-x = ");
System.out.println(-x);

System.out.print("x = ");
System.out.println(x);

Output
+x = 3
-x = -3
int x = 3;

System.out.print("+x = ");
System.out.println(+x);

System.out.print("-x = ");
System.out.println(-x);

System.out.print("x = ");
System.out.println(x);

Output
+x = 3
-x = -3
x = 3
int x = 3;

System.out.print("+x = ");
System.out.println(+x);

System.out.print("-x = ");
System.out.println(-x);

System.out.print("x = ");
System.out.println(x);

Output
+x = 3
-x = -3
x = 3
Binary
Operators
Binary
Operators
Addition (+)
3 + 4 or 55.1 + 43.58
Binary
Operators
Addition (+)
3 + 4 or 55.1 + 43.58

Subtraction (-)
50 - 20 or 45.3 - 0.78
Binary
Operators
Addition (+)
3 + 4 or 55.1 + 43.58

Subtraction (-)
50 - 20 or 45.3 - 0.78

Multiplication (*)
5 * 10 or 0.6 * 3.4
Addition (+)
3 + 4 or 55.1 + 43.58

Subtraction (-)
50 - 20 or 45.3 - 0.78

Multiplication (*)
5 * 10 or 0.6 * 3.4

Division (/)
50.0 / 2.0 or 45 / 2
Addition (+)
3 + 4 or 55.1 + 43.58

Subtraction (-)
50 - 20 or 45.3 - 0.78

Multiplication (*)
5 * 10 or 0.6 * 3.4

Division (/)
50.0 / 2.0 or 45 / 2

Remainder (%)
30 % 7 or 45 % 3 or 23 % 77
Arithmetic expression with integers and
floating-points is a floating-point
Arithmetic expression with integers and
floating-points is a floating-point

d a m i xe d ex pres s i o n
Calle
Mixed
Expressi
ons

System.out.println(30.0 + 7);

System.out.println(30 + 7);

Output
System.out.println(30.0 + 7);

System.out.println(30 + 7);

Output
System.out.println(30.0 + 7);

System.out.println(30 + 7);

Output
System.out.println(30.0 + 7);

System.out.println(30 + 7);

Output
37.0
System.out.println(30.0 + 7);

System.out.println(30 + 7);

Output
37.0
System.out.println(30.0 + 7);

System.out.println(30 + 7);

Output
37.0
37
Mixed
Expressi
ons

System.out.println(30.0 + 7);

System.out.println(30 + 7);

Output
37.0
37
Integer
Division
System.out.println(7.0 / 4.0);

System.out.println(7.0 / 4);

System.out.println(7 / 4);

Output
System.out.println(7.0 / 4.0);

System.out.println(7.0 / 4);

System.out.println(7 / 4);

Output
System.out.println(7.0 / 4.0);

System.out.println(7.0 / 4);

System.out.println(7 / 4);

Output
1.75
System.out.println(7.0 / 4.0);

System.out.println(7.0 / 4);

System.out.println(7 / 4);

Output
1.75
System.out.println(7.0 / 4.0);

System.out.println(7.0 / 4);

System.out.println(7 / 4);

Output
1.75
1.75
System.out.println(7.0 / 4.0);

System.out.println(7.0 / 4);

System.out.println(7 / 4);

Output
1.75
1.75
System.out.println(7.0 / 4.0);

System.out.println(7.0 / 4);

System.out.println(7 / 4);

Output
1.75
1.75
1
System.out.println(7.0 / 4.0);

System.out.println(7.0 / 4);

System.out.println(7 / 4);
result truncated

Output
1.75
1.75
1
System.out.println(7.0 / 4.0);

System.out.println(7.0 / 4);

System.out.println(7 / 4);

Output
1.75
1.75
1
Can you spot the ERRO
R ?
int midTerm1 = 63;
int midTerm2 = 84;
int finalExam = 83;

double average = (midTerm1 + midTerm2 + finalExam)/3;

System.out.println("Average mark: " + average);


int midTerm1 = 63;
int midTerm2 = 84;
int finalExam = 83;

double average = (midTerm1 + midTerm2 + finalExam)/3;

System.out.println("Average mark: " + average);


Possible SOLUTION
int midTerm1 = 63;
int midTerm2 = 84;
int finalExam = 83;

double average = (midTerm1 + midTerm2 + finalExam)/3.0;

System.out.println("Average mark: " + average);


Possible SOLUTION
int midTerm1 = 63;
int midTerm2 = 84;
int finalExam = 83;

double average = (midTerm1 + midTerm2 + finalExam)/3.0;

System.out.println("Average mark: " + average);


Another SOLUTION
int midTerm1 = 63;
int midTerm2 = 84;
int finalExam = 83;

double average =
( (double)(midTerm1 + midTerm2 + finalExam) )/3;

System.out.println("Average mark: " + average);


Another SOLUTION
int midTerm1 = 63;
int midTerm2 = 84;
int finalExam = 83;

double average =
( (double)(midTerm1 + midTerm2 + finalExam) )/3;
t y p ecast
System.out.println("Average mark: " + average);
Can you spot the ERRO
R?
double balance = 33.3;
int dollars = balance;
double balance = 33.3;
int dollars = balance;
compiler disallows floa
t ing-point to integer as
signment
double balance = 33.3;
int dollars = (int) balance;
typecast
How do we evaluate
compound expressions?
(a + b) / c + a / c - a + b / c * b
Order of
Operatio
ns
Order of
Operatio
ns

1. Parentheses: Evaluated first (inside-out)


Order of
Operatio
ns

1. Parentheses: Evaluated first (inside-out)

2. Operator precedence: unary, *, /, %, + and -


Order of
Operatio
ns

1. Parentheses: Evaluated first (inside-out)

2. Operator precedence: unary, *, /, %, + and -

3. Associativity: Binary operators evaluated left-


to-right, unary operators right-to-left
Evaluate the following expression:

z - (a + b / 2) + w * -y
Evaluate the following expression:

z - (a + b / 2) + w * -y

1. Parentheses
b/2 then add a
Evaluate the following expression:

z - (a + b / 2) + w * -y

1. Parentheses
b/2 then add a
Evaluate the following expression:

z - (a + b / 2) + w * -y

1. Parentheses
b/2 then add a
2. Unary
Evaluate the following expression:

z - (a + b / 2) + w * -y

1. Parentheses
b/2 then add a
2. Unary
Evaluate the following expression:

z - (a + b / 2) + w * -y

1. Parentheses
b/2 then add a
2. Unary
3. Multiplication
Evaluate the following expression:

z - (a + b / 2) + w * -y

1. Parentheses
b/2 then add a
2. Unary
3. Multiplication
Evaluate the following expression:

z - (a + b / 2) + w * -y

1. Parentheses
b/2 then add a
2. Unary
3. Multiplication
4. Subtraction
Evaluate the following expression:

z - (a + b / 2) + w * -y

1. Parentheses
b/2 then add a
2. Unary
3. Multiplication
4. Subtraction
Evaluate the following expression:

z - (a + b / 2) + w * -y

1. Parentheses
b/2 then add a
2. Unary
3. Multiplication
4. Subtraction
5. Combine
Evaluate the following expression:

z - (a + b / 2) + w * -y

1. Parentheses
b/2 then add a
2. Unary
3. Multiplication
4. Subtraction
5. Combine

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy