• Save
OOP Chapter 4: Data Type and Expressions
Upcoming SlideShare
Loading in...5
×
 

OOP Chapter 4: Data Type and Expressions

on

  • 902 views

http://www.youtube.com/watch?v=Jfn1Mdd3gXE

http://www.youtube.com/watch?v=Jfn1Mdd3gXE

Statistics

Views

Total Views
902
Views on SlideShare
810
Embed Views
92

Actions

Likes
1
Downloads
0
Comments
0

1 Embed 92

http://www.atit.patumvan.com 92

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

OOP Chapter 4: Data Type and Expressions OOP Chapter 4: Data Type and Expressions Presentation Transcript

  • Object-Oriented Programming Language Chapter 4 : Data Types and Expressions Atit Patumvan Faculty of Management and Information Sciences Naresuna UniversityMonday, February 20, 12
  • 2 Contents • Data Types and Constants • Arithmetic Expressions • Assignment OperatorsAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 3 Data Type and Constant data_type identifier = value; identifier value data_type int number = 25; copy 25 number 25 intAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 4 Data Types in Objective-C Type Descriptions Size char A character of the local character set 1 bytes int An integer (whole numbers) 4 bytes float Floating point number 4 bytes short A short integer 2 bytes long A double short 4 bytes long long A double long 8 bytes double Double precision float 8 bytes BOOL id An object 4 bytesAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 5 Uses the basic Objective-C data typesProgram 4.101: #import <Foundation/Foundation.h>02:03: int main(int argc, char *argv[]) {04:! @autoreleasepool {05:! ! int integerVar = 100;06:! ! float floatingVar = 331.79;07:! ! double doubleVar = 8.44e-11;08:! ! char charVar = w;09:! !10:! ! NSLog(@"integerVar = %i", integerVar);11:! ! NSLog(@"floatVar = %f", floatingVar);12:! ! NSLog(@"doubleVar = %e", doubleVar);13:! ! NSLog(@"doubleVar = %g", doubleVar);14:! ! NSLog(@"charVar = %c", charVar);15: } integerVar = 100 floatVar = 331.790009 doubleVar = 8.440000e-11 doubleVar = 8.44e-11 charVar = wAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 6 Basic Data Types Type Constant Example NSLog chars char a’, ‘n’ %c short int - %hi, %hx, %ho unsigned short int - %hu, %hx, %ho int 12, -97, 0xFFE0, 0177 %i, %x, %o unsigned int 12u, 100U, 0xFFu %u, %x, %o long int 12L, -2001l, 0xffffL %li, %lx, %lo unsigned long int 12UL, 100ul, 0xffeeUL %lu, %lx, %lo long long int 0xe5e5e5e5LL, 500ll %lli, %llx, %llo unsigned long long 12ull, 0xffeeULL %llu, % llx, int float 12.43f, 3.1e-5f, 0x1.5p10, %llo %f, %e, %g, %a double 0x1p-1 12.34, 3.1e-5, 0x.1p3 %f, %e, %g, %a long double 12.34L, 3.1e-5l %Lf, %Le, %Lg id nil %pAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 7 Arithmetic ExpressionProgram 4.305:06: int a = 2; a - b = 9807:! ! int b = 2; b * c = 5008:! ! int c = 25; a / c = 409:! ! int d = 4; a + b * c = 15010:! ! int result; a * b + c * d = 30011:! !12:! ! result = a - b;!// subtraction13:! ! NSLog(@"a - b = %i", result);14:15:! ! result = b * c;!// multiplication16:! ! NSLog(@"b * c = %i", result);17:! !18:! ! result = a / c;!// division19:! ! NSLog(@"a / c = %i", result);20:21:! ! result = a + b * c;! // precedence22:! ! NSLog(@"a + b * c = %i", result);23:! !24:! ! NSLog(@"a * b + c * d = %i", a * b + c * d);25:!Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 8 Integer Arithmetic and Unary Minus OperatorProgram 4.305:06:! int a = 25;07:! ! int b = 2;08:! ! float c = 25.0;09:! ! float d = 2.0;10:! !11:! ! NSLog(@"6 + a / 5 * b = %i", 6 + a / 5 * b);12:! ! NSLog(@"a / b * b = %i", a / b * b);13:! ! NSLog(@"c / d * d = %f", c / d * d);14:! ! NSLog(@"-a = %i", -a);15:! 6 + a / 5 * b = 16 a / b * b = 24 c / d * d = 25.000000 -a = -25Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 9 The Modulus OperatorProgram 4.405:06:! int a = 25, b = 5, c = 10, d =7;07:!08:! NSLog(@"a %% b = %i", a % b);09:! NSLog(@"a %% c = %i", a % c);10:! NSLog(@"a %% d = %i", a % d);11:! NSLog(@"a / d * d + a %% d = %i", a / d * d + a % d);12:! a % b = 0 a % c = 5 a % d = 4 a / d * d + a % d = 25 a /d * d+ a % d = ((a / d) * d)+(a % d)Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 10 Integer and Floating-Point ConversionProgram 4.505:06:! float f1 = 123.125, f2;07:! int i1, i2 = -150;08:!09:! i1 = f1; // floating to integer convision10:! NSLog(@"%f assigned to an int produces %i", f1, i1);11:!12:! f1 = i2; // integer to floating convision13:! NSLog(@"%i assigned to a float produces %f", i2, f1);14:!15:! f1 = i2 / 100; // integer divided by integer16:! NSLog(@"%i divided by 100 produces %f", i2, f1);17:!18:! f2 = i2 /100.0; // integer divided by float19:! NSLog(@"%i divided by 100.0 produce %f", i2, f2);20:!21:! f2 = (float) i2 / 100; // type cast operator22:! NSLog(@"(float) %i divided by 100 produces %f", i2, f2); 123.125000 assigned to an int produces 123 -150 assigned to a float produces -150.000000 -150 divided by 100 produces -1.000000 -150 divided by 100.0 produce -1.500000 (float) -150 divided by 100 produces -1.500000Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 11 The Type Case Operator int i2 = 25; float f2 = (float) i2 / 5; // type cast operator int s1 = (int) 29.55 + (int) 21.99; int s2 = 29 + 21; float f3 = (float) 6 / (float) 4 float f4 = (float) 6 / 4 id myNumber; Fraction * myFraction; myFraction = (Fraction *) myNumber;Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 12 Assignment Operator count += 10; count = count +10; counter -= 5; counter = counter - 5; a /= b + c; a = a / (b + c);Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 13 Increment and Decrement Operator count++; count = count + 1; counter--; counter = counter - 1; a /= b + c; a = a / (b + c);Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 14 Desk CalculatorProgram 4.648:49:! int main(int argc, char *argv[]) {50:! ! @autoreleasepool {51:! ! ! Calculator * deskCalc = [[Calculator alloc] init];52:! ! ! [deskCalc setAccumulator: 100.0];53:! ! !54:! ! ! [deskCalc add: 200.];55:! ! ! [deskCalc divide: 15.0];56:! ! ! [deskCalc subtract: 10.0];57:! ! ! [deskCalc multiply: 5]; Calculator58:! ! !59:! ! ! NSLog(@"The result is % g", [deskCalc accumulator]);60:! ! ! accumulator:double61:! ! ! [deskCalc release];62:! ! }63:! } setAccumulator(double):void64:! clear:void accumulator:double add(double):void The result is 50 subtract(double):void multiply(double):void divide(double):voidAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 15 Calculator Class: Interface SectionProgram 4.602:03: @interface Calculator: NSObject04: {05:! double accumulator;06: }07:08: // accumulator methods09: -(void) setAccumulator: (double) value;10: -(void) clear;11: -(double) accumulator; Calculator12:13: // arithmetic methods14: -(void) add: (double) value; accumulator:double15: -(void) subtract: (double) value;16: -(void) multiply: (double) value;17: -(void) divide: (double) value; setAccumulator(double):void18: @end! clear:void accumulator:double add(double):void subtract(double):void multiply(double):void divide(double):voidAtit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12
  • 16 Calculator Class: Implementation SectionProgram 4.6 20: @implementation Calculator 21: 22: -(void) setAccumulator: (double) value{ 23: ! accumulator = value; 24: } 25: -(void) clear{ 26: ! accumulator = 0; 27: } 28: -(double) accumulator{ 29: ! return accumulator; Calculator 30: } 31: 32: -(void) add: (double) value{ accumulator:double 33: ! accumulator += value; 34: } 35: -(void) subtract: (double) value{ setAccumulator(double):void 36: ! accumulator -= value; clear:void 37: } 38: -(void) multiply: (double) value{ accumulator:double 39: ! accumulator *= value; add(double):void 40: } subtract(double):void 41: -(void) divide: (double) value{ 42: ! accumulator /= value; multiply(double):void 43: } divide(double):void 44: @end!Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming LanguageMonday, February 20, 12