Basic Object-C Help


#1

Hello, I am new here an unsure if this the right place to be posting but I am in need of help coding some basic Obective-C
I am new to it and am taking a class at my local university where I have been assigned this.

Develop a class named Box (or ABox) if necessary.

A Box object should have three iVars:
• Sender’s Name: NSString
• length: float type (in inches, such as 33.123)
• width: float type (in inches, such as 12.2345)
• height: float type (in inches, such as 8.50)

Each of these ivars should have matching property declarations and subsequently synthesized. Do not use any hardcoded getters or setters.

At the least, it should have two instance methods. These are:

  1. computeVolume
    • Should not receive any parameters
    • Should compute the volume of a box object and return float. By the way, the volume is computed as length * width * height (in cubic inches).
  2. estimateCategory
    • Should not receive any parameters
    • Should return “Large” if the volume is more than or equal to 8000 cubic inch
    • Should return “Medium” if the volume is more than or equal to 1000 cubic inch and less than 8000 cubic inches.
    • Should return “Small” if the volume is less than 1000 cubic inches.

It should also override the inherited description method that will return a formatted string with the values of iVars. (Either see our OOP example 4 or 5, or see brandontreb.com/objective-c-tuto … on-method/, or see Chapter2/Textbook1)

The consumer of the class will:
• Create one or more boxes (one at a time) and display the string (on the Log) received from the description method.
• Should receive the returned values from the two other methods (one at a time) and will display strings like “The Volume of the box is: 9999.99” with two digits after decimal or the “Estimated category of the box is Large (or whatever the category).

Test the above class extensively all probable scenarios and document the results with appropriate screen shots. The displayed information (on the Log) should be easily understandable.


I don’t even know where to start. I would greatly appreciate any help.
Feel free to code it yourself if you are up to it.


#2

My code so far

// ABox.h
@interface ABox : NSObject

@property (nonatomic, copy) NSString *name;
@property float length;
@property float width;
@property float height;

  • (float) computeVolume;
  • (NSString *) estimateCategory;

@end

// ABox.m
@implementation ABox
@synthesize name, length, width, height;

  • (float) computeVolume {
    return self.length * self.width * self.height;
    }
  • (NSString *) estimateCategory {
    float vol = [self computeVolume];
    if(vol >= 8000)
    return @“Large”;
    else if((vol < 8000) && (vol > 1000))
    return @“Medium”;
    else
    return @“Small”;
    }

#3

You have done well so far, but you need code for a driver and the code for the description method.

Here is something for you to tinker with:
main.m - the driver:

//  main.m

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

int main (int argc, const char * argv[])
{
    @autoreleasepool {
            
        MyBox *box = [[[MyBox alloc] init] autorelease];
        
        box.length = M_PI;
        box.width = M_PI;
        box.height = M_PI;
        NSLog (@"%@", box);
        
        box = [MyBox randomBox];
        NSLog (@"%@", box);
        
        box = [MyBox randomBox];
        NSLog (@"%@", box);
    }
    fflush (NULL);
    return 0;
}

MyBox.h:

//  MyBox.h

#import <Foundation/Foundation.h>

@interface MyBox : NSObject

@property (nonatomic, copy) NSString *name;

@property float length;
@property float width;
@property float height;

- (float) computeVolume;
- (NSString *) estimateCategory;

+ (MyBox *)randomBox;

@end

MyBox.m:

//  MyBox.m

#import "MyBox.h"

@implementation MyBox

@synthesize name, length, width, height;

static const char *_names [] = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Pluto"};

+ (MyBox *)randomBox
{
    MyBox *randomBox = [[[self class] alloc] init];
    
    randomBox.length = M_PI * (arc4random_uniform (arc4random_uniform (30)) + 1);
    randomBox.height = M_PI * (arc4random_uniform (arc4random_uniform (50)) + 1);
    randomBox.width  = M_PI * (arc4random_uniform (arc4random_uniform (70)) + 1);
    
    NSUInteger index = arc4random_uniform (sizeof (_names) / sizeof (_names [0]));
    NSString *randomName = [NSString stringWithUTF8String:_names [index]];
    randomBox.name = randomName;

    return [randomBox autorelease];
}

- (id)init
{
    self = [super init];
    if (self)
    {
        
    }
    return self;
}

- (float) computeVolume
{
    return self.length * self.width * self.height;
}

- (NSString *) estimateCategory
{
    float vol = [self computeVolume];
    if(vol >= 8000)
        return @"Large";
    else if((vol < 8000) && (vol > 1000))
        return @"Medium";
    else
        return @"Small";
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"Name: %@ Estimated Volume: %.2f Category: %@", [self name], [self computeVolume], [self estimateCategory]];
}
@end

If you don’t see all three lines on the output, run the program from the command line.

Sample Output:

> ./MyBox 
2012-09-13 14:24:18.523 MyBox[47984:303] Name: Triple J Estimated Volume: 31.01 Category: Small
2012-09-13 14:24:18.525 MyBox[47984:303] Name: Venus Estimated Volume: 14883.01 Category: Large
2012-09-13 14:24:18.526 MyBox[47984:303] Name: Mars Estimated Volume: 71314.44 Category: Large

> ./MyBox
2012-09-13 14:24:42.741 MyBox[47987:303] Name: Triple J Estimated Volume: 31.01 Category: Small
2012-09-13 14:24:42.742 MyBox[47987:303] Name: Jupiter Estimated Volume: 6139.24 Category: Medium
2012-09-13 14:24:42.743 MyBox[47987:303] Name: Uranus Estimated Volume: 147341.81 Category: Large

> ./MyBox
2012-09-13 14:24:44.755 MyBox[47989:303] Name: Triple J Estimated Volume: 31.01 Category: Small
2012-09-13 14:24:44.757 MyBox[47989:303] Name: Uranus Estimated Volume: 4340.88 Category: Medium
2012-09-13 14:24:44.757 MyBox[47989:303] Name: Mercury Estimated Volume: 18603.77 Category: Large

#4

Hey thanks a lot, however, I keep receiving a ‘autorelease’ is unavailable: not available in automatic reference counting mode
ARC forbids explicit message send of ‘autorelease’.

I am unsure what this means or how to fix it.