SlideShare a Scribd company logo
Object-Oriented Programming Language
                           Chapter 4 : Data Types and Expressions


                                          Atit Patumvan
                          Faculty of Management and Information Sciences
                                        Naresuna University




Monday, February 20, 12
2



                                                                                     Contents


             •        Data Types and Constants

             •        Arithmetic Expressions

             •        Assignment Operators




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University              Object-Oriented Programming Language
Monday, February 20, 12
3



                                                              Data Type and Constant


            data_type identifier = value;
                  identifier

                                                                                     value

                                                                                      data_type
              int number = 25;                                                                 copy   25
                     number
                                                                                      25
                                                                                             int
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                     Object-Oriented Programming Language
Monday, 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 bytes

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                        Object-Oriented Programming Language
Monday, February 20, 12
5



                                     Uses the basic Objective-C data types

Program 4.1
01: #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 = w


Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University   Object-Oriented Programming Language
Monday, 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                     %p
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                Object-Oriented Programming Language
Monday, February 20, 12
7



                                                                 Arithmetic Expression

Program 4.3
05:
06:             int      a = 2;                                                      a   -   b   =   98
07:!      !     int      b = 2;                                                      b   *   c   =   50
08:!      !     int      c = 25;                                                     a   /   c   =   4
09:!      !     int      d = 4;                                                      a   +   b   *   c = 150
10:!      !     int      result;                                                     a   *   b   +   c * d = 300
11:!      !
12:!      ! result = a - b;!// subtraction
13:!      ! NSLog(@"a - b = %i", result);
14:
15:!      !     result = b * c;!// multiplication
16:!      !     NSLog(@"b * c = %i", result);
17:!      !
18:!      !     result = a / c;!// division
19:!      !     NSLog(@"a / c = %i", result);
20:
21:!      ! result = a + b * c;! // precedence
22:!      ! 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 Language
Monday, February 20, 12
8



                        Integer Arithmetic and Unary Minus Operator

Program 4.3
05:
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 = -25




Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University     Object-Oriented Programming Language
Monday, February 20, 12
9



                                                                 The Modulus Operator

Program 4.4
05:
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 Language
Monday, February 20, 12
10



                                    Integer and Floating-Point Conversion

Program 4.5
05:
06:!      float f1 = 123.125, f2;
07:!      int i1, i2 = -150;
08:!
09:!      i1 = f1; // floating to integer convision
10:!      NSLog(@"%f assigned to an int produces %i", f1, i1);
11:!
12:!      f1 = i2; // integer to floating convision
13:!      NSLog(@"%i assigned to a float produces %f", i2, f1);
14:!
15:!      f1 = i2 / 100; // integer divided by integer
16:!      NSLog(@"%i divided by 100 produces %f", i2, f1);
17:!
18:!      f2 = i2 /100.0; // integer divided by float
19:!      NSLog(@"%i divided by 100.0 produce %f", i2, f2);
20:!
21:!      f2 = (float) i2 / 100; // type cast operator
22:!      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.500000
Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                                   Object-Oriented Programming Language
Monday, 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 Language
Monday, 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 Language
Monday, 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 Language
Monday, February 20, 12
14



                                                                             Desk Calculator

Program 4.6
48:
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];                                                                  Calculator
58:!      ! !
59:!      ! ! NSLog(@"The result is % g", [deskCalc accumulator]);
60:!      ! !                                                                                  accumulator:double
61:!      ! ! [deskCalc release];
62:!      ! }
63:!      }                                                                                    setAccumulator(double):void
64:!                                                                                           clear:void
                                                                                               accumulator:double
                                                                                               add(double):void
  The result is                     50                                                         subtract(double):void
                                                                                               multiply(double):void
                                                                                               divide(double):void

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University                        Object-Oriented Programming Language
Monday, February 20, 12
15



                                           Calculator Class: Interface Section

Program 4.6
02:
03:      @interface Calculator: NSObject
04:      {
05:!       double accumulator;
06:      }
07:
08:      // accumulator methods
09:      -(void) setAccumulator: (double) value;
10:      -(void) clear;
11:      -(double) accumulator;                                                              Calculator
12:
13:      // arithmetic methods
14:      -(void) add: (double) value;                                                accumulator:double
15:      -(void) subtract: (double) value;
16:      -(void) multiply: (double) value;
17:      -(void) divide: (double) value;                                             setAccumulator(double):void
18:      @end!                                                                       clear:void
                                                                                     accumulator:double
                                                                                     add(double):void
                                                                                     subtract(double):void
                                                                                     multiply(double):void
                                                                                     divide(double):void

Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University              Object-Oriented Programming Language
Monday, February 20, 12
16



                              Calculator Class: Implementation Section

Program 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 Language
Monday, February 20, 12

More Related Content

PDF
OOP Chapter 3: Classes, Objects and Methods
PDF
OOP Chapter 7 : More on Classes
PDF
OOP: Chapter 2: Programming in Objective-C
PDF
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
PDF
Python Application: Visual Approach of Hopfield Discrete Method for Hiragana ...
PDF
Writing Usable APIs in Practice by Giovanni Asproni
ODT
Object Oriented Approach Within Siebel Boundaries
PDF
Design and implementation of a java based virtual laboratory for data communi...
OOP Chapter 3: Classes, Objects and Methods
OOP Chapter 7 : More on Classes
OOP: Chapter 2: Programming in Objective-C
Chapter 9 : Polymorphism, Dynamic Typing, and Dynamic Binding
Python Application: Visual Approach of Hopfield Discrete Method for Hiragana ...
Writing Usable APIs in Practice by Giovanni Asproni
Object Oriented Approach Within Siebel Boundaries
Design and implementation of a java based virtual laboratory for data communi...

What's hot (20)

PDF
Review_Cibe Sridharan
PPTX
On the Semantics of Real-Time Domain Specific Modeling Languages
PDF
Producing simulation sequences by use of a Java-based Framework
DOCX
Case study how pointer plays very important role in data structure
PDF
Extractive Summarization with Very Deep Pretrained Language Model
PPTX
Event-driven Model Transformations in Domain-specific Modeling Languages
PDF
turecko-150426_pse_01
PDF
Data Structures and Algorithms
PPT
pointer, structure ,union and intro to file handling
DOC
Unit 8
PDF
EXTRACTIVE SUMMARIZATION WITH VERY DEEP PRETRAINED LANGUAGE MODEL
PPT
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
PPT
pointer, structure ,union and intro to file handling
PPT
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
PPT
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
PDF
IRJET - Speech to Speech Translation using Encoder Decoder Architecture
PDF
ENSEMBLE MODEL FOR CHUNKING
PDF
IRJET - Analysis of Paraphrase Detection using NLP Techniques
PDF
Dynamic framed slotted aloha algorithms using fast tag estimation
PDF
Review_Cibe Sridharan
On the Semantics of Real-Time Domain Specific Modeling Languages
Producing simulation sequences by use of a Java-based Framework
Case study how pointer plays very important role in data structure
Extractive Summarization with Very Deep Pretrained Language Model
Event-driven Model Transformations in Domain-specific Modeling Languages
turecko-150426_pse_01
Data Structures and Algorithms
pointer, structure ,union and intro to file handling
Unit 8
EXTRACTIVE SUMMARIZATION WITH VERY DEEP PRETRAINED LANGUAGE MODEL
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
IRJET - Speech to Speech Translation using Encoder Decoder Architecture
ENSEMBLE MODEL FOR CHUNKING
IRJET - Analysis of Paraphrase Detection using NLP Techniques
Dynamic framed slotted aloha algorithms using fast tag estimation
Ad

Similar to OOP Chapter 4: Data Type and Expressions (20)

PPT
Mesics lecture 3 c – constants and variables
PDF
2 1. variables & data types
PPT
Datatypes
PPT
FP 201 Unit 2 - Part 2
PPSX
C language (Collected By Dushmanta)
PPS
C programming session 01
PPTX
COM1407: Variables and Data Types
PPTX
Data types
PDF
Data types
PPTX
Chapter 6 data types
PPTX
Lecture 2 introduction to Programming languages C.pptx
PPTX
Constant, variables, data types
PDF
03 expressions.ppt
PDF
C Programming Assignment
PPT
Data Types in C
PPT
C the basic concepts
PPT
C language Unit 2 Slides, UPTU C language
PDF
Java Reference
PDF
Learning VB.NET Programming Concepts
PPTX
structured Programming Unit-2-Basic-Elements-of-C.pptx
Mesics lecture 3 c – constants and variables
2 1. variables & data types
Datatypes
FP 201 Unit 2 - Part 2
C language (Collected By Dushmanta)
C programming session 01
COM1407: Variables and Data Types
Data types
Data types
Chapter 6 data types
Lecture 2 introduction to Programming languages C.pptx
Constant, variables, data types
03 expressions.ppt
C Programming Assignment
Data Types in C
C the basic concepts
C language Unit 2 Slides, UPTU C language
Java Reference
Learning VB.NET Programming Concepts
structured Programming Unit-2-Basic-Elements-of-C.pptx
Ad

More from Atit Patumvan (20)

PDF
Iot for smart agriculture
PDF
An Overview of eZee Burrp! (Philus Limited)
PDF
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
PDF
Chapter 1 mathmatics tools
PDF
Chapter 1 mathmatics tools
PDF
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
PDF
Chapter 0 introduction to theory of computation
PPTX
Media literacy
PDF
Chapter 01 mathmatics tools (slide)
PDF
การบริหารเชิงคุณภาพ ชุดที่ 8
PDF
การบริหารเชิงคุณภาพ ชุดที่ 7
PDF
การบริหารเชิงคุณภาพ ชุดที่ 6
PDF
Computer Programming Chapter 5 : Methods
PDF
Computer Programming Chapter 4 : Loops
PDF
Introduction to Java EE (J2EE)
PDF
การบริหารเชิงคุณภาพ ชุดที่ 5
PDF
การบริหารเชิงคุณภาพ ชุดที่ 4
PDF
การบริหารเชิงคุณภาพ ชุดที่ 3
KEY
การบริหารเชิงคุณภาพ ชุดที่ 2
PDF
Computer Programming: Chapter 1
Iot for smart agriculture
An Overview of eZee Burrp! (Philus Limited)
แบบฝึกหัดวิชา Theory of Computation ชุดที่ 1 เซ็ต
Chapter 1 mathmatics tools
Chapter 1 mathmatics tools
รายงานการประเมินคุณภาพภายใน ปีงบประมาณ 2556
Chapter 0 introduction to theory of computation
Media literacy
Chapter 01 mathmatics tools (slide)
การบริหารเชิงคุณภาพ ชุดที่ 8
การบริหารเชิงคุณภาพ ชุดที่ 7
การบริหารเชิงคุณภาพ ชุดที่ 6
Computer Programming Chapter 5 : Methods
Computer Programming Chapter 4 : Loops
Introduction to Java EE (J2EE)
การบริหารเชิงคุณภาพ ชุดที่ 5
การบริหารเชิงคุณภาพ ชุดที่ 4
การบริหารเชิงคุณภาพ ชุดที่ 3
การบริหารเชิงคุณภาพ ชุดที่ 2
Computer Programming: Chapter 1

Recently uploaded (20)

PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
01-Introduction-to-Information-Management.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Computing-Curriculum for Schools in Ghana
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
master seminar digital applications in india
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
01-Introduction-to-Information-Management.pdf
Anesthesia in Laparoscopic Surgery in India
VCE English Exam - Section C Student Revision Booklet
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Microbial disease of the cardiovascular and lymphatic systems
GDM (1) (1).pptx small presentation for students
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Computing-Curriculum for Schools in Ghana
2.FourierTransform-ShortQuestionswithAnswers.pdf
Pharma ospi slides which help in ospi learning
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
master seminar digital applications in india
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Cell Structure & Organelles in detailed.
A systematic review of self-coping strategies used by university students to ...
FourierSeries-QuestionsWithAnswers(Part-A).pdf

OOP Chapter 4: Data Type and Expressions

  • 1. Object-Oriented Programming Language Chapter 4 : Data Types and Expressions Atit Patumvan Faculty of Management and Information Sciences Naresuna University Monday, February 20, 12
  • 2. 2 Contents • Data Types and Constants • Arithmetic Expressions • Assignment Operators Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language Monday, February 20, 12
  • 3. 3 Data Type and Constant data_type identifier = value; identifier value data_type int number = 25; copy 25 number 25 int Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language Monday, February 20, 12
  • 4. 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 bytes Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language Monday, February 20, 12
  • 5. 5 Uses the basic Objective-C data types Program 4.1 01: #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 = w Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language Monday, February 20, 12
  • 6. 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 %p Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language Monday, February 20, 12
  • 7. 7 Arithmetic Expression Program 4.3 05: 06: int a = 2; a - b = 98 07:! ! int b = 2; b * c = 50 08:! ! int c = 25; a / c = 4 09:! ! int d = 4; a + b * c = 150 10:! ! int result; a * b + c * d = 300 11:! ! 12:! ! result = a - b;!// subtraction 13:! ! NSLog(@"a - b = %i", result); 14: 15:! ! result = b * c;!// multiplication 16:! ! NSLog(@"b * c = %i", result); 17:! ! 18:! ! result = a / c;!// division 19:! ! NSLog(@"a / c = %i", result); 20: 21:! ! result = a + b * c;! // precedence 22:! ! 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 Language Monday, February 20, 12
  • 8. 8 Integer Arithmetic and Unary Minus Operator Program 4.3 05: 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 = -25 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language Monday, February 20, 12
  • 9. 9 The Modulus Operator Program 4.4 05: 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 Language Monday, February 20, 12
  • 10. 10 Integer and Floating-Point Conversion Program 4.5 05: 06:! float f1 = 123.125, f2; 07:! int i1, i2 = -150; 08:! 09:! i1 = f1; // floating to integer convision 10:! NSLog(@"%f assigned to an int produces %i", f1, i1); 11:! 12:! f1 = i2; // integer to floating convision 13:! NSLog(@"%i assigned to a float produces %f", i2, f1); 14:! 15:! f1 = i2 / 100; // integer divided by integer 16:! NSLog(@"%i divided by 100 produces %f", i2, f1); 17:! 18:! f2 = i2 /100.0; // integer divided by float 19:! NSLog(@"%i divided by 100.0 produce %f", i2, f2); 20:! 21:! f2 = (float) i2 / 100; // type cast operator 22:! 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.500000 Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language Monday, February 20, 12
  • 11. 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 Language Monday, February 20, 12
  • 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 Language Monday, February 20, 12
  • 13. 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 Language Monday, February 20, 12
  • 14. 14 Desk Calculator Program 4.6 48: 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]; Calculator 58:! ! ! 59:! ! ! NSLog(@"The result is % g", [deskCalc accumulator]); 60:! ! ! accumulator:double 61:! ! ! [deskCalc release]; 62:! ! } 63:! } setAccumulator(double):void 64:! clear:void accumulator:double add(double):void The result is 50 subtract(double):void multiply(double):void divide(double):void Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language Monday, February 20, 12
  • 15. 15 Calculator Class: Interface Section Program 4.6 02: 03: @interface Calculator: NSObject 04: { 05:! double accumulator; 06: } 07: 08: // accumulator methods 09: -(void) setAccumulator: (double) value; 10: -(void) clear; 11: -(double) accumulator; Calculator 12: 13: // arithmetic methods 14: -(void) add: (double) value; accumulator:double 15: -(void) subtract: (double) value; 16: -(void) multiply: (double) value; 17: -(void) divide: (double) value; setAccumulator(double):void 18: @end! clear:void accumulator:double add(double):void subtract(double):void multiply(double):void divide(double):void Atit Patumvan, Faculty of Management and Information Sciences, Naresuan University Object-Oriented Programming Language Monday, February 20, 12
  • 16. 16 Calculator Class: Implementation Section Program 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 Language Monday, February 20, 12