judaco / Objective-C

0 stars 0 forks source link

Objective-C - Lesson 02 #1

Open judaco opened 7 years ago

judaco commented 7 years ago
#import <Foundation/Foundation.h>
#import "Calculator.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {

        Calculator *myCalculator = [[Calculator alloc] init];

        [myCalculator add: 2];
        [myCalculator add: 8];
        [myCalculator divide: 2];
        [myCalculator subtract: 2];
        [myCalculator multiply: 3];
        [myCalculator setMemory];
        [myCalculator add:[myCalculator getMemory]];
        double result = [myCalculator getAccumulator];
        NSLog(@"result = %g", result);

    }
    return 0;
}
judaco commented 7 years ago
#import <Foundation/Foundation.h>

//The Fraction class

@interface Calculator : NSObject

-(void) clear;
-(void) add: (double) x;
-(void) divide: (double) x;
-(void) multiply: (double) x;
-(void) subtract: (double) x;
-(double) getAccumulator;
-(void) setAccumulator: (double) x;
-(double) changeSign;
-(double) reciprocal;
-(double) square;

-(void) clearMemory;
-(void) setMemory;
-(double) getMemory;

@end
judaco commented 7 years ago
#import "Calculator.h"

@implementation Calculator
{
    double accumulator;
    double memory;
}
-(void) clear{
    accumulator = 0.0;
}

-(void) add: (double) x{
    accumulator += x;
}

-(void) divide: (double) x{
    if(x != 0){
        accumulator /= x;
    }
}

-(void) multiply: (double) x{
    accumulator *= x;
}

-(void) subtract:(double) x{
    accumulator -= x;
}
-(double) getAccumulator{
    return accumulator;
}
-(void) setAccumulator:(double)x{
    [self clear];
    [self add:x];
}

-(double) changeSign{
    [self multiply:-1];
    return accumulator;
}

-(double) reciprocal{
    accumulator = 1.0 / accumulator;
    return accumulator;
}
-(double) square{
    [self multiply:accumulator];
    return accumulator;
}
-(void) clearMemory{
    memory = 0.0;
}
-(void) setMemory{
    memory = accumulator;
}
-(double) getMemory{
    return memory;
}

@end
judaco commented 7 years ago
//
//  main.m
//  Lesson02
//
//  Created by hackeru on 15/03/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Fraction.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        /*NSLog(@"I hope you will\n like Objective-C");
         int sum, value1, value2;
         value1 = 10;
         value2 = 30;
         sum = value1 + value2;
         NSLog(@"The sum of %i and %i is %i", value1, value2, sum);

         //[ClassOrInstance method];
         //[receiver message];
         //int radius = [myCircle getRadius]

         int numerator = 1;
         int denominator = 3;
         NSLog(@"The fraction is %i/%i", numerator, denominator);

         */

        Fraction *myFraction;//for pointers we are putting a "*"
        myFraction = [Fraction alloc];//allocate a place in the memory
        myFraction = [myFraction init];//initialize the place in the memory = both of them it's like a "new" in Java

        [myFraction setNumerator:1];
        [myFraction setDenominator:3];

        Fraction *myFraction2 = [[Fraction alloc] init];//how to make it in one line
        [myFraction2 setNumerator:3];
        [myFraction2 setDenominator:7];

        myFraction2.numerator = 3;
        NSLog(@"numerator = %i", myFraction2.numerator);

        [myFraction2 print];
        [myFraction2 setTo:3 over:7];

        [myFraction2 print];

        int myInt = 5;
        float myFloat = 45.6f;
        //  \n - break line
        char myChar = 'T';
        double myDouble = 1.23e+4;

        NSLog(@"myInt is %i", myInt);
        NSLog(@"myFloat is %f", myFloat);
        NSLog(@"myDouble is %e", myDouble);//could add some 0 from right side
        NSLog(@"myDouble is %g", myDouble);
        NSLog(@"myChar is %c", myChar);

        long int myLong = 10000000000L;
        NSLog(@"myLong is %li", myLong);

        long long int myVeryLong = 10000000000000L;
        NSLog(@"myVeryLong is %lli", myVeryLong);

        short int myShort = 32000;
        NSLog(@"myShort is %hi", myShort);

        unsigned int myUnsigenedInt = 400000000 ;//not at the same max value of int as number
        NSLog(@"myUnsignedInt is %u", myUnsigenedInt);

        //id myObject;//can point on pointer, but on ANY type that I want

        int x = 5;
        int y = 6;
        float z = (float)(x+y) / (float)2;
        NSLog(@"z=%f", z);

        [myFraction setTo:2 over:3];
        [myFraction2 setTo:4 over:6];
        [myFraction2 add:myFraction];
        [myFraction print];
        [myFraction2 print];

        Fraction *juda = [myFraction2 clone];//another pointer
    }
    return 0;
}
//
//  Fraction.h
//  Lesson02
//
//  Created by hackeru on 15/03/2017.
//  Copyright © 2017 juda. All rights reserved.
//
#import <Foundation/Foundation.h>

@interface Fraction : NSObject //name of the class, after : - from which I have an inheritance

@property int numerator, denominator;

//methods and properties
//property - it looks like a field, but it's a method, which in the background there's a field

-(void) print;
/*
-(void) setNumerator: (int)n;//like setter in Java
-(void) setDenominator: (int)d;//(method) firstName: (type) name of param
-(int) numerator;
-(int) denominator;*/
-(void) add: (Fraction *)f;//Fraction is not a primitive, it's a class with pointers, and the function want to get a Fraction
-(double) convertToNum;
-(void) setTo: (int)n over: (int)d;//one way for 2 params
//-(void) setTo: (int)n : (int)d;//another way for 2 params
-(Fraction *) clone;//how to reback a pointer

@end
//
//  Fraction.m
//  Lesson02
//
//  Created by hackeru on 15/03/2017.
//  Copyright © 2017 juda. All rights reserved.
//

#import "Fraction.h"

static int count = 0;//a global variable, I can use it wherever I want

@implementation Fraction

@synthesize numerator, denominator;//create getters and setters, without their visibility in my code

/*{
    int numerator;//all the params in Objective-C are private
    int denominator;
}*/

-(void)print{
    NSLog(@"(%i,%i)", numerator, denominator);
    count++;
}
/*
-(void)setNumerator:(int)n{
    numerator = n;
}

-(void)setDenominator:(int)d{
    denominator = d;
}

-(int)numerator{
    return numerator;
}

-(int)denominator{
    return denominator;
}
*/
-(double)convertToNum{
    if (denominator != 0)
        return numerator / denominator;
    else
        return NAN;
}

-(void)setTo:(int)n over:(int)d{
    numerator = n;
    denominator = d;
}

-(void)add:(Fraction *)f{//adding a fraction to another fraction
    self.numerator = self.numerator * f.denominator + self.denominator * f.numerator;
    self.denominator = denominator * f.denominator;
}

-(Fraction *) clone{
    Fraction *frac = [[Fraction alloc] init];
    frac.numerator = self.numerator;
    frac.denominator = self.denominator;
    return frac;
}

@end