HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Solving first- and second-degree degree polynomials

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
degreesolvingpolynomialsfirstsecondand

Problem

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 

@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 

@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 < 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


Solution

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.

Code Snippets

//TODO: check if a = 0
- (double) calculate {
    self.x = (self.b - 2 * self.b)/self.a;
    return self.x;

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

}

Context

StackExchange Code Review Q#67876, answer score: 2

Revisions (0)

No revisions yet.