Improving is Learning

After a bit of thinking about the class I wrote in my previous post, I figured I should make the class have more features, and learn a few things while doing so.
The interface tells the story, I added methods to get and set the state variables and calculate the power rating. Also, the class can be instantiated with the initial values for the state variables.


//fileName: Resistor.h

@interface Resistor : NSObject
{
	double resistance, voltageAcross, currentThrough;
}

- (id) initWithResistance:(double)r
				  voltage:(double)v
				  current:(double)i;
- (void)setVoltageAcross:(double)v;
- (void)setCurrentThrough:(double)i;
- (double)getResistance;
- (double)getVoltageAcross;
- (double)getCurrentThrough;
- (double)getPowerRating;
@end

As I did in the previous post, I return self which is equivalent to this in Java or C#. It (rather obviously) refers to the class it’s invoked in.


//fileName: Resistor.m

#import "Resistor.h"

@implementation Resistor
- (id) initWithResistance:(double)r
				  voltage:(double)v
				  current:(double)i
{

	resistance = r;
	voltageAcross = v;
	currentThrough = i;
	
	return self;
}

- (void) setVoltageAcross:(double)v
{
	voltageAcross = v;
}

- (void) setCurrentThrough:(double)i
{
	currentThrough = i;
}

- (double) getResistance
{
	return resistance;
}

- (double) getVoltageAcross
{
	return voltageAcross;
}

- (double) getCurrentThrough
{
	return currentThrough;
}

- (double) getPowerRating {
	double p;
	
	if (voltageAcross == 0.0)
		p = currentThrough * currentThrough * resistance;
	else if (currentThrough == 0)	
		p = (voltageAcross * voltageAcross) / resistance;
	else
		p = voltageAcross * currentThrough;
	
	return p;
}
@end

Instead of including the print statement in the resistor class, I manipulated the main method to print out the information of the Resistor.

One thing to mentions is on line 14. Since the classes have an alloc method, which allocates memory there must be a way to deallocate the memory, and what do you know, dealloc does indeed deallocate the memory allotted for the instance.


//fileName: main.m

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

int main (int argc, char *argv[]) {
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	Resistor * aResistor = [[Resistor alloc] initWithResistance:10 voltage:25 current:1];
	
	NSLog(@"Resistance: %f, Voltage: %f, Current: %f, Power Consumed: %f",
		  [aResistor getResistance], [aResistor getVoltageAcross], [aResistor getCurrentThrough], [aResistor getPowerRating]);	
	
	[aResistor dealloc];
	
	[pool drain];
	return 0;
}

Another thing (other than the compiler directive) that’s really cool is how messages are passed to objects. [[Resistor alloc] initWithResistance:10 voltage:25 current:1]; calls the initWithResistance metod and passes arguments, which I understand could have also been done by [[Resistor alloc] initWithResistance:10:25:1];. But it makes the code more readable and neater to use names for the arguments (called, selectors) being passed.


run
[Switching to process 8053]
Running…
2009-12-06 21:02:24.335 Untitled[8053:a0f] Resistance: 10.000000, Voltage: 25.000000, Current: 1.000000, Power Consumed: 25.000000

Debugger stopped.
Program exited with status value:0.

I think that’s pretty much it for the Resistor class. I am going to try out something new next.

Advertisements

2 Comments on “Improving is Learning”

  1. Timothy says:

    Hi there!

    In the post, you wrote:

    …self which is equivalent to this in Java or C#. It (rather obviously) refers to the class it’s invoked in.

    Actually, in Java “this” refers to the particular object to which a running object method is associated. It doesn’t refer to the class (E.g. Dog), but an instance (E.g. Rover). That is one of the reasons a static method in Java such as main can’t refer to “this” (which “this”?!). “this” in main will cause a compiler error. (A constructor is not an object method, but “this” is used to refer to the constructed object anyway.)

    • Red Kid says:

      You’re completely right, of course. My use of “equivalent” is wrong. I should have said that they are similar at most.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s