Solving first- and second-degree degree polynomials I was writing a bit of code and I was just wondering if I was doing it the right way. I know all the syntax is right and It all works perfectly. I was just wondering if I'm doing it the right way. Should I have another infrastructure, am I following the MVC correctly (there is no V yet, it's mostly just a model). Is this program extensible and enhanceable enough?

It's a very simple program that finds the zeroes of either a first degree polynomial

$$ a x + b = 0 $$

or second degree polynomial.

$$ a x^2 + b x + c = 0 $$

First.h:

#import &lt;Foundation/Foundation.h&gt;

@interface First : NSObject
@property double a, b, x;

- (id) initWithAValue: (double) aValue andBValue: (double) bValue;
- (double) calculate;
@end


First.m:

#import "First.h"

@implementation First
-(id)initWithAValue:(double)aValue andBValue:(double)bValue
{
    self = [super init];
    if (self) {
        self.a = aValue;
        self.b = bValue;

    }
    return self;
}
- (double) calculate {
    self.x = (self.b - 2 * self.b)/self.a;
    return self.x;

}
@end


Second.h:

#import &lt;Foundation/Foundation.h&gt;

@interface Second : NSObject
@property double a,b,c,x1,x2,d;
- (id) initWithAValue: (double) aValue andBValue: (double) bValue andCValue: (double) cValue;
- (double) calculateX1;
- (double) calculateX2;
- (double) calculateD;
@end


Second.m:

#import "Second.h"

@implementation Second
- (id)initWithAValue:(double)aValue andBValue:(double)bValue andCValue:(double)cValue
{
    self = [super init];
    if (self){
        //TODO: check if a = 0
        self.a = aValue;
        self.b = bValue;
        self.c = cValue;
    }
    return self;
}
-(double)calculateD
{
    //TODO: Check if d &lt; 0
    self.d = (self.b * self.b)-(4 * self.a * self.c);
    return self.d;
}
-(double)calculateX1
{
    self.x1 = ((self.b - 2 * self.b) + sqrt([self calculateD]))/ (2 * self.a);
    return self.x1;
}
-(double)calculateX2
{
    self.x2 = ((self.b - 2 * self.b) - sqrt([self calculateD]))/ (2 * self.a);
    return self.x2;
}

@end


Main.m:

#import &lt;Foundation/Foundation.h&gt;
#import "First.h"
#import "Second.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        //This would be replaced by a user interface
        double firstA = 1;
        double firstB = 1;
        double secondA = 1;
        double secondB = 1;
        double secondC = 1;

        First *firstDegree = [[First alloc]initWithAValue:firstA andBValue:firstB];
        NSLog(@"The value of x for your first degree equation is: %.2f",[firstDegree calculate]);

        Second *secondDegree = [[Second alloc]initWithAValue:secondA andBValue:secondB andCValue:secondC];
        NSLog(@"D is: %.2f", [secondDegree calculateD]);
        NSLog(@"The values of x for your second degree equation are: %.2f and: %.2f",[secondDegree calculateX1],[secondDegree calculateX2]);
    }
    return 0;
}
As you wrote,


//TODO: check if a = 0



First degree polynomials are just a degenerate case of second degree polynomials, where the leading coefficient is 0.  Perhaps you should design your interfaces to reflect that.

I'm not sure that all those calculate… methods are appropriate.  Part of the point of object-oriented programming is to give the illusion of "smart data".  The object knows when it needs to recalculate; the caller just asks for poly.x and the answer is just there.

In first.m,


- (double) calculate {
    self.x = (self.b - 2 * self.b)/self.a;
    return self.x;

}



could just be

- (double) calculate {
    return self.x = -self.b / self.a;
}


In Objective C, it is not necessary to check if the receiver of a message is nil.  Therefore,


if (self) {
    self.a = aValue;
    self.b = bValue;

}



does not need to be in an if block.