Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4...

47
Bicycl e Ca r Types pedal( ) pushAccelera tor()
  • date post

    22-Dec-2015
  • Category

    Documents

  • view

    220
  • download

    0

Transcript of Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4...

Page 1: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

BicycleCar

Types

pedal()pushAccelerator()

Page 2: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Stringdoubleint

Programming Language Types

3 3.0 4.04 “three” “four”

= 7= “threefour”

+ +/

= 0

/

= 0.75

Page 3: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

BM

ISpr

eads

heet

AB

MIC

alcu

lato

rTyping Objects

Page 4: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Object Types

Primitive types Classes

Kinds of Types

Types

ABMISpreadsheet

AnotherBMISpreadsheet

Interfaces

BMISpreadsheet

Stringdouble int

Type = Set of operations

Page 5: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Object Vs Primitive Types

• Instances of object types are objects.

• Instances of primitive types are primitive values.

• Primitive types used to construct object types.

• ~ Atoms Vs Molecules

• Different set of rules.

• In Smalltalk no difference.

• C++ even worse.

Page 6: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Abstract Value Vs Representation

2.2 02.2

0.22E+1

LBS_IN_KG

Page 7: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Invoking Abstract Operation

profit - earnings

-earnings

Math.round(bmi)

binary,infix

unary, prefix

Arbitray, method invocation

Page 8: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Types and Assignment

double height = 1.77;

int weight = 70;

int weight = 70.0;

double height = 2;

int weight = “seventy”;

Page 9: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Primitive Types

• Constants (Literals & Named Constants)

• Assignment Rules

• Operations with Invocation Syntax

Page 10: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Mathematical Integers

int Range & Constants

{- .. +}

int {-231 .. (231 – 1)}

0-2147483648

2147483647 2

02

Integer.MAX_VALUE

Integer.MIN_VALUE

-2

+2

32 bits

Page 11: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Mathematical Real Numbers

double Range & Constants

double

2.2

0.22E+1

0.22

Double.MAX_VALUE

Double.MIN_VALUE .2

64 bits

02.20 2. 2

2.2E0 22E-1

Standardmantissa

.22E1 .22E-1

exponent

xEy = x * 10y

Page 12: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Mathematical Integers

Other Integer Subsets

{- .. +}

byte {-27 .. (27 – 1)}

Byte.MAX_VALUEByte.MIN_VALUE

short {-215 .. (215 – 1)}

Short.MAX_VALUEShort.MIN_VALUE

long {-263 .. (263 – 1)}

Short.MAX_VALUEShort.MIN_VALUE

8 bits

16 bits

64 bits

Page 13: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Mathematical Real Numbers

float Size & Constants

float 32 bits

Float.MAX_VALUE

Float.MIN_VALUE .2

Page 14: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Mixed Assignment

long l =70;

int

Safe & Automatically Converted

int long

double d = 70;

int double

70.0;

long l

Page 15: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

int i = 70.6;

Cast

int double

Not Automatically Converted

(int)

cast

70;

float i = 70.6;(float)

float double

Page 16: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Assignment Rules

v e

Te Tv

v = e;

Te Tv

v = (Tv) e;

TvTe

!(Te Tv || Te Tv)

v = (Tv) e;

double d = 5;

int i = (int) 5.7; bool b = (bool) 5;

Narrower than

Wider than

Page 17: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Assignment Rules for Primitive Types

• If T1 narrower than T2 (Set of instances of T1 Set of instances of T2)

• Expression of type T1 can be assigned to Variable of type T2

• Expression of type T2 can be assigned to Variable of type T1 with cast.

Page 18: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Actual Parameter Assignment

double weight;

public void setWeight (double newWeight) {

weight = newWeight;

}

setWeight (70);

Page 19: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Actual Parameter Assignment

int intWeight;

public void setIntWeight (int newWeight) {

intWeight = newWeight;

}

setWeight (70.6);setWeight ((int)70.6);

Page 20: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Returning a Value

double weight;

public int getIntWeight () {

return weight;

}

Page 21: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Translated Into Assignment

double weight;

public int getIntWeight () {

return weight;

}

double weight;

public int getIntWeight () {

int getIntWeight = weight;

}

Internal Variable

Page 22: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Returning a Value

double weight;

public int getIntWeight () {

return(int) weight;

}

Page 23: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Primitive Types

• Constants (Literals & Named Constants)

• Assignment Rules

• Operations with Invocation Syntax

Page 24: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Int Arithmetic Operations

5/2 5 % 2

x == (x/y)*y x == (x / y) * y + (x % y)

5/2 2 5 % 2 1

Name Action Operands & ResultType(Signature)

+ add int, int int- subtract int, int int- negate int int* multiply int, int int/ int quotient int, int int% int remainder int, int int

Page 25: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

double Arithmetic Operations

5.0/2.0 5.0/2.0 2.5

Name Action Operands & Result Type(Signature)

+ add double, double double- subtract (unary) double- subtract double, double double* multiply double, double double/ divide double, double double

Page 26: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Overflow

Integer.MAX_VALUE + 1 Integer.MAX_VALUE

Integer.MIN_VALUE - 1 Integer.MIN_VALUE

Double.MAX_VALUE + 1 Double.MAX_VALUE

Double.MIN_VALUE - 1 Double.MIN_VALUE

(double) Integer.MIN_VALUE - 1.0 double)( Integer.MIN_VALUE - 1.0

Page 27: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Overflow

10/0 Integer.MAX_VALUE

-10/0 Integer.MIN_VALUE

10.0/0.0 Double.POSITIVE_INFINITY

-10.0/0.0 Double.NEGATIVE_INFINITY

0/0 0

0.0/0.0 Double.NaN

Page 28: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Int Overflow

Page 29: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Double Overflow

Page 30: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Mixed Operations

5/2.0

narrower type converted

5.0/2.0

int i = (int) (5/2.0) int i =(int) (5.0/2.0) int i = (int) 2.5

int i = 2

double d = 5/(int) 2.0 double d = 5/2 double d = 2

double d = 2.0

Page 31: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Strong Vs Weak Typing

“hello” - 1

int minus

Legal under Weak Typing

Illegal under Strong Typing

anything goes

strict type rules

Page 32: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Misc Math Operations

Operations (invoked on Math) Signatureabs() double double, int intacos(), asin(), atan(), cos(), sin(), tan() double doublepow () double, double doubleexp(), log() double doubleround() double longrandom(), pi() doublesqrt() double double

Math.pi() Math.power(5,3) 53

int i = Math.round(5.9)

Math.round(5.9) (long) 6

int i = 6int i = (int) Math.round(5.9)

(int) Math.round(Integer.MAX_VALUE + 1.0) Integer.MAX_VALUE

(int) 5.9 5

Page 33: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

boolean Constants

boolean

falsetrue

Page 34: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Relational Operations

5 == 5 true

5 == 4 false

5 >= 4 true

5 <= 4 false

5 != 5 false

5 != 4 true

Name Action Signature of intimplementation

Signature of doubleimplementation

== equal? int, int boolean double, double boolean!= not equal? int, int boolean double, double boolean> greater than? int, int boolean double, double boolean< less than? int, int boolean double, double boolean>= greater than or

equal?int, int boolean double, double boolean

<= less than or equal? int, int boolean double, double boolean

Page 35: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Boolean Vs Number Expressions

hoursWorked > MAX_HOURS

true if hoursWorked is greater than MAX_HOURS and false otherwise

boolean overWorked =

int earnings = hourlyWage*hoursWorked + BONUS

Page 36: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Boolean Property

Page 37: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Boolean Property Code (edit)

public boolean isOverWeight() {

return OVER_WEIGHT_BMI < getBMI();

}

Page 38: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Boolean Property Code

public final double HIGH_BMI == 25;

public boolean getOverWeight() {return getBMI() >

HIGH_BMI;}

Page 39: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Boolean Property Code

public final double HIGH_BMI == 25;

public boolean isOverWeight() {return getBMI() >

HIGH_BMI;}

Page 40: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Boolean Operations

!true false

!false true

true && true true

true && false false

Name(s) Action Signature! not boolean boolean&&, & and boolean, booleanboolean||, | or boolean, booleanboolean

false && true false

false && false false

true || true true

true || false true

false || true true

false || false false

Page 41: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Regular evaluation

Short-circuit evaluation

Short-Circuit Evaluation

false && (9654.34/323.13 > 32.34) false

true || (9654.34/323.13 > 32.34) true

Name(s) Action Signature! not boolean boolean&&, & and boolean, booleanboolean||, | or boolean, booleanboolean

Second operand not evaluated

false & (9654.34/323.13 > 32.34 false

true | (9654.34/323.13 > 32.34) true

Second operand evaluated

Page 42: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Short-Circuit Evaluation

false && (10/0) false

Name(s) Action Signature! not boolean boolean&&, & and boolean, booleanboolean||, | or boolean, booleanboolean

false & (10/0)

error in some languages

Page 43: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

false && (10 / 0)

Complex Expressions

Sub-expression

false && 10 / 0

Operator evaluation order?

Page 44: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Operator Precedence

! - (T)* / %+ -> < <= >=== !=&|&&||

false && 10 / 0false && 10 / 0

- 5 - 4 -5 - 4

!true && false !true && false

5 / 4 * 3 5/4 * 3

true || false == false || true

true || false == false || true

(int) 5 / 2.0 (int) 5 / 2.0

unary cast

Page 45: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Operator Precedence (edit)

! - (T)* / %+ -> < <= >=== !=&|&&||

false && (10 / 0)false && 10 / 0

- 5 - 4 (-5) - 4

!true && false ( !true) && false

5 / 4 * 3 (5/4) * 3

true || false == false || true

true || (false == false) || true

(int) 5 / 2.0 ((int) 5) / 2.0

unary cast

Page 46: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Operator Precedence

! - (T)* / %+ -> < <= >=== !=&|&&||

false && (10 / 0)false && 10 / 0

- 5 - 4 (-5) - 4

!true && false (!true) && false

5 / 4 * 3 (5/4) * 3

true || false == false || true

true || (false == false) || true

(int) 5 / 2.0 ((int) 5) / 2.0

unary cast

Page 47: Bicycle Car Types pedal() pushAccelerator(). Stringdoubleint Programming Language Types 3 3.04.0 4 “three”“four” = 7 = “threefour” + + / = 0 / = 0.75.

Printing Arbitrary Expressions

System.out.println (2) Output: 2

System.out.println (2.0) Output: 2.0

System.out.println ((int) 2.0) Output: 2

System.out.println (5 > 0) Output: true