Solve pythagoras theorem in Objective-C - objective-c

I am working on a programm which lets you calculate the sides in a triangle using the Pythagoras method. I recently posted my first question about this. How can I code it so that you can also work out B or A. Here is my code so far:
PythagorasCalc.m
#import "PythagorasCalc.h"
#implementation PythagorasCalc
- (double)calculatePythagorasValue {
return sqrt(A*A+B*B);
}
//Access Code
- (double)A {
return A;
}
- (void)setA: (double)value {
if(A != value) {
A = value;
}
}
- (double)B {
return B;
}
- (void)setB: (double)value {
if(B != value) {
B = value;
}
}
- (double)C {
return C;
}
- (void)setC: (double)value {
if(C != value) {
C = value;
}
}
#end
PythagorasCalc.h
#import <Cocoa/Cocoa.h>
#interface PythagorasCalc : NSObject {
#private
int A;
int B;
int C;
}
#property (nonatomic) double A;
#property (nonatomic) double B;
#property (nonatomic) double C;
- (double)calculatePythagorasValue;
#end
AppDelegate.m
#import "PythagorasCalculatorAppDelegate.h"
#implementation PythagorasCalculatorAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
calculator = [[PythagorasCalc alloc] init];
}
- (IBAction)calculateClicked:(id)sender {
double A = _ATextField.doubleValue;
double B = _BTextField.doubleValue;
double C = _CTextField.doubleValue;
calculator.A = A;
calculator.B = B;
calculator.C = C;
_CTextField.doubleValue = [calculator calculatePythagorasValue];
}
#end
AppDelegate.h
#import <Cocoa/Cocoa.h>
#import "PythagorasCalc.h"
#interface PythagorasCalculatorAppDelegate : NSObject <NSApplicationDelegate> {
//Public Calculator Object
PythagorasCalc *calculator;
}
#property (assign) IBOutlet NSWindow *window;
- (IBAction)calculateClicked:(id)sender;
#property (weak) IBOutlet NSTextField *ATextField;
#property (weak) IBOutlet NSTextField *BTextField;
#property (weak) IBOutlet NSTextField *CTextField;
#end

Related

How to convert this code from Swift to Objective C

private var distance: Double {
return fabs(max - min)
}
How to define this variable in Objective C language.
In your .m file:
#interface MyClass ()
#property (nonatomic, assign, readonly) double distance;
#end
#implementation MyClass
-(double)distance {
return fabs(max - min);
}
#end

How to unit test instance method - Objective C

I am looking to structure my unit tests in the correct way and would like to know how to approach testing a class method while passing an object into the method
So the method in question (basic example)
MyClient.m:
#import "MyClient.h"
#interface MyClient()
#property (nonatomic, strong) MyMedia *myMedia;
#end
- (void)setMedia:(MyMedia *)media {
self.MyMedia = media;
int var;
if (media && media.isLive) {
var = 1;
}
else {
var = 2;
}
}
}
So in this example i would create two tests, 1 that tests var is 2 if media.isLive and the other to check var is 2 if not.
Hopefully this makes sense
Thanks
Your code is not really testable because everything you would need to check is private. Whilst there are ways to 'hack' into a class using the runtime and stuff to test things, you really want to just utilise the publically access interface of your class. So I've re-written your code like this:
MyClient.h
#interface MyClient:NSObject
#property (nonatomic, strong) MyMedia *myMedia;
#property (nonatomic, assign, readonly) int var;
#end
MyClient.m
#import "MyClient.h"
#implementation MyClient
- (void)setMedia:(MyMedia *)media {
_MyMedia = media;
_var = media && media.isLive ? 1 : 2;
}
#end
Now lets do some tests using XCTest.
MyClientTests.m
#import XCTest;
#import "MyClient.h"
#interface MyClienTests:XCTestCase
#end
#implementation MyClientTests {
MyClient *_myCLient;
MyMedia *_media;
}
-(void) setUp {
_myClient = [[MyClient alloc] init];
_media = [[MyMedia alloc] init];
}
-(void) testMyClientMediaIsAlive {
_media.alive = YES;
_myClient.media = _media;
XCTAssertEqual(1, _myClient.var);
}
-(void) testMyClientMediaIsNotAlive {
_myClient.media = _media;
XCTAssertEqual(2, _myClient.var);
}
-(void) testMyClientNoMedia {
XCTAssertEqual(2, _myClient.var);
}
#end

Expected method body

*.m file*
#import "BNREmployee.h"
#interface BNREmployee ()
#property (weak) IBOutlet NSWindow *window;
#end
#implementation BNREmployee
- (double)yearsOfEmployment
// **Error expected method body**-(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Do I have a non-nil hireDate?
if (self.hireDate) {
// NSTimeInterval is the same as double
NSDate *now = [NSDate date];
NSTimeInterval secs = [now timeIntervalSinceDate:self.hireDate];
return secs / 31557600.0; // Seconds per year
} else {
return 0;
}
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
#end
*.h file*
#import "BNRPerson.h"
#interface BNREmployee : BNRPerson
#property (nonatomic) unsigned int employeeID;
#property (nonatomic) unsigned int officeAlarmCode;
#property (nonatomic) NSDate *hireDate;
- (double)yearsOfEmployment;
#end
How can i declare "- (double)yearsOfEmploymentsince" since it is declared in another file but while still keeping the "-(void)applicationDidFinishLaunching:(NSNotification *)aNotification {" as the main header???
Your question is a little unclear especially without your .h file to see what this class is a subclass of, but this line: - (double)yearsOfEmployment needs an opening {, that's the "expected method body" problem.

strange objective C bug, properties not case sensitive?

I have two properties "M" and "m", not the best coding style I know but bear with me. Assignment to these properties in the init method does not function properly. Here's the code in it's entirety:
#import "AppDelegate.h"
#interface Foo : NSObject
#property (nonatomic, assign) int M;
#property (nonatomic, assign) int m;
- (id)initWithM:(int)M m:(int)m;
#end
#implementation Foo
- (id)initWithM:(int)M m:(int)m {
if((self = [super init])) {
self.M = M;
printf("M = %d %d\n", M, self.M);
self.m = m;
printf("M = %d %d\n", M, self.M);
printf("m = %d %d\n", m, self.m);
}
return self;
}
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
Foo *f = [[Foo alloc] initWithM:2 m:1];
}
#end
And here is the output from the printf's:
M = 2 2
M = 2 1
m = 1 0
If I change "M" to "BAR" and "m" to "bar" it works as I would expect. Is there an explanation for this other than being a compiler bug?
Thanks.
#property int M;
#property int m;
both create
- (void)setM:(int)
If you really wanted to have both an m and an M property (which you definitely shouldn't) you can use
#property int M;
#property (setter = setLowerCaseM:, getter = lowerCaseM)int m;

Getting error NSInternalInconsistencyException "Argument must be non-nil"

I run this code:
- (void)unitButtonButtonTapped:(id)sender {
[_label setString:#"Last button: Unembossed square"];
MilitaryUnits *target = nil;
target = [Peasants militaryUnits];
target.position = ccp(100, 450);
[self addChild:target];
}
And I get this error:
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Argument must be non-nil'
These are my .h and .m class files
#import "cocos2d.h"
#interface MilitaryUnits : CCSprite {
int _experience;
int _number_of_units;
int _stamina;
int _armor_level;
int _weapon_levell;
}
#property (nonatomic, assign) int experience;
#property (nonatomic, assign) int number_of_units;
#property (nonatomic, assign) int stamina;
#property (nonatomic, assign) int armor_level;
#property (nonatomic, assign) int weapon_levell;
#end
#interface Peasants : MilitaryUnits{
}
+(id)militaryUnits;
#end
#import "MilitaryUnits.h"
#implementation MilitaryUnits
#synthesize number_of_units = _number_of_units;
#synthesize stamina = _stamina;
#synthesize experience = _experience;
#synthesize armor_level = _armor_level;
#synthesize weapon_levell = _weapon_levell;
#end
#implementation Peasants
+ (id)militaryUnits {
Peasants *militaryUnits = nil;
if ((militaryUnits = [[[super alloc] initWithFile:#"Target.png"] autorelease])) {
}
return militaryUnits;
}
#end
Note, I'm using cocos 2d
looks to me like your sprite is nil, ie the file "Target.png" is not found. Make certain the file name has the same case (in finder) as you spelled out in your code, and that the file is included in the target's membership in Xcode.
Also
+ (id)militaryUnits {
Peasants *militaryUnits;
if ((militaryUnits = [[[super alloc] initWithFile:#"Target.png"] autorelease])) {
return militaryUnis;
} else {
CCLOGERROR(#"your favorite whine style for errors like file not found");
return nil;
}
}