# Creating an Obj-C Program to Calculate Density

Discussion in 'Mac Programming' started by iMasterWeb, Sep 4, 2009.

1. ### iMasterWeb macrumors regular

Joined:
Mar 15, 2009
#1
Hi there, me again! So right now, I'm trying to create a program that will, based on user input, calculate the density (which includes mass and volume) of an object.

The problem is that there's something wrong with my code, and I'm not sure what...whenever I run the program it stops after I enter the mass and it puts some text in the console, and then asks for my password (entering my password doesn't fix the problem) It seems like a pretty basic program, so could someone help me out? Thsanks!

@interface:
Code:
```#import <Foundation/Foundation.h>

@interface Calculator_Methods : NSObject {
int volume;
int mass;
int density;
}

@property int volume, mass, density;

-(void) regObjCalc;
-(void) irregObjCalc;
-(void) densityCalc;

@end
```
@implementation:
Code:
```#import "Calculator Methods.h"

@implementation Calculator_Methods

@synthesize volume, mass, density;

//Calculates the mass of a regular object
-(void) regObjCalc
{
int length, height, width;
scanf("%i", mass);
scanf("%i", length);
NSLog(@"And height:");
scanf("%i", height);
NSLog(@"And width:");
scanf("%i", width);
volume = length * width * height;
}
//Calculates the mass of an irregular object
-(void) irregObjCalc
{
int startHeight, endHeight;
scanf("%i", mass);
NSLog(@"Please enter the starting height of the water:");
scanf("%i", startHeight);
NSLog(@"Please enter the ending height of the water:");
scanf("%i", endHeight);
volume = endHeight - startHeight;
}
-(void) densityCalc
{
density = mass / volume;
NSLog(@"Density = mass/volume\n Mass = %i\nVolume = %i\n Density = %i/%i\n%i/%i = %i\nDensity = %i g/mL", mass, volume, mass, volume, mass, volume, density, density);
}

@end```
main:
Code:
```#import "Calculator Methods.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

Calculator_Methods *myObject = [[Calculator_Methods alloc] init];
char usrInput;
NSLog(@"Please enter'a' to find the density of an irregular object and 'b' to ifnd the density of a regular object");
scanf("%c", &usrInput);
switch (usrInput) {
case 'a':
[myObject irregObjCalc];
break;
case 'b':
[myObject regObjCalc];
break;
}
[myObject densityCalc];
[pool drain];
return 0;
}```
(don't ask why I named the class Calculator_Methods...I was being a little literal )

Thanks again!

-iMaster

2. ### Avizzv92 macrumors regular

Joined:
Mar 23, 2008
#2
Perhaps a more effective way would be to make a method with parameters that take the given values mass and volume. And you could have a similar method to calculate volume and the other calculations.

Code:
```-(double)calculateDensityForMass: (double)mass andVolume: (double)volume
{
double denisty;
density = mass / volume;
return density;
}```

3. ### crackpip macrumors regular

Joined:
Jul 23, 2002
#3
Did you just upgrade to SL? Xcode asked for my admin password the first time I ran the debugger. No idea why, though.

On to your program. You are going to get some weird answers even if it worked because your variables should be floats not int's. And, you've got expressions with integer division. Also your scanf's take a pointer to the variable.

In your program, you might want to account for whatever units you are anticipating. Finally, when calculating the density of an irregularly shaped object, the change in height of water is not the volume. It's related, but you are missing a piece.

crackpip

4. ### iMasterWeb thread starter macrumors regular

Joined:
Mar 15, 2009
#4
Thanks crackpit! Got it working!

(P.S. when using water displacement, the volume of an irregularly shaped object IS found by subtracting start height from end height...at least according to some sites on the internet and my science teacher )

5. ### chown33 macrumors 604

Joined:
Aug 9, 2009
Location:
Sailing beyond the sunset
#5
Measuring the change in height only gives you units of length, and volume is measured in cubic units. You're neglecting the area of the water's surface, measured in square units. So change in height (units) times surface area (square units) gives cubic units. Use millimeters for units, for example.

Example. Submerge your hand in a swimming pool: measure the change in water height (very tiny, probably not even measurable). Submerge your hand in a 1 gal bucket half-filled with water: measure the change in height (definitely measurable).

6. ### crackpip macrumors regular

Joined:
Jul 23, 2002
#6
Chown33 explained what I was hinting at. What your teacher may be talking about is using a beaker or graduated cylinder, which are calibrated to measure volume. In this case, you aren't measuring a starting and ending height; you are measuring starting and ending volume. You can tell because the markings on the side will have a volume unit (e.g mL or cm^3).

crackpip

7. ### iMasterWeb thread starter macrumors regular

Joined:
Mar 15, 2009
#7
OOOOOOOOOOHHHHHHHH. I see what you're saying now...my bad. But for my purposes, the current code will suffice