Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiplying Non-Primitive Data Types

Status
Not open for further replies.

RetailBoy

Programmer
Dec 9, 2004
1
US
I am about to begin coding a program that multiplies 2 non-primitive data types. It needs to be able to multiply negatives, whole numbers, and fractions. I'm trying to think of the best method to do this. I read something that mentioned converting non-primitive data types to primitive but I'm not sure if that's the road I should be taking.

Any ideas would be very helpful.
 
You will find it impossible to do arithmetic on a non-numeric primitive or any kind of Object - it just doesn't make sense.

--------------------------------------------------
Free Database Connection Pooling Software
 
I think you should clarify what kinf of non-primitive data types are you talking about, but remeber that Java cannot extend operators.

Cheers.

Dian

 
arithmetic on a non-numeric primitive or any kind of Object
I beg to differ. A decent time class should have an add, subtract, multiply and devide. Same for a Complex class. As for non-numeric primitives, there are cases where you would want to add a value to a char -- be them rare with the exsiting parse* functions of the Integer, Float, etc classes.

Arithmetic makes sence on any thing that has units like perhaps a person's height in Feet, Inches, ect.

The language dosen't allow operator overloading, which means you can't add Objects using + ... unless it's the String class. You would have to write a function called add() function and it would be called like:
Code:
c = a.add(b);

Operator over loading is just syntactic sugar in the first place.
 
I think it would be clearer with getValue/setValue methods

a.setValue(a.getValue() + b)

But that's just an opinion.

Cheers.

Dian
 
jstreich : I would not call "c = a.add(b);" performing an arithmetic operation on an object - that is just a wrapper for what is probably the same as something like :

Integer c = new Integer(5).intValue() * new Integer(6).intValue();

--------------------------------------------------
Free Database Connection Pooling Software
 
A BigDecimal might be the best class to use.
Else you might get a lot of work, to implement
Code:
3 * 0.3
0.3 * 3
3 * 300
0.3 * 0.3
300000000000 * 300000000000
300000000000 * 0.3
, - mixed types with their specific problems.

seeking a job as java-programmer in Berlin:
 
I agree with jstreich -- just write an add() method to accept an object of your type and do your custom arithmetic.

While .NET supports operator overloading, I suspect that in a few years we'll see it on a list of "Top ten language features that you shouldn't use". Java got it right by not allowing it.

Chip H.


____________________________________________________________________
If you want to get the best response to a question, please read FAQ222-2244 first
 
jstreich : I would not call "c = a.add(b);" performing an arithmetic operation on an object - that is just a wrapper for what is probably the same as something like :

Integer c = new Integer(5).intValue() * new Integer(6).intValue();

Not quite...

For instance if we have a class Feet we may have a few diffferent arithmatics to perform:

int divide(Feet a)
//Feet divided by feet yeilds and int, not a length

Feeet divide(int a)
//Feet devided by an int is still in Feet
ect.

Also, keep in mind when adding times, you have other conversions your arithmatic might have to do...
Code:
Time add(Time a)
{
   Time ret = new time();
   ret.hours = hours + a.hours;
   ret.minutes = minutes + a.minutes;
   ret.seconds = seconds + a.seconds;
   if(ret.seconds >= 60)
   {
      ret.minutes = ret.seconds/60;
      ret.seconds = ret.seconds%60;
   }
   if(ret.minutes >= 60)
   {
       ret.hours = ret.minutes/60;
       ret.minutes = ret.minutes%60;
   }
   return ret;
}
is a bit more involved than a wrapper... This would be an add... If you want to get technical, an add of a primitive is just the ANDing and XORing of bits -- and Java does no real arithmatic... It's all perspective I guess.
 
jstreich - semantics - my point was that whenever you do "arithmetic" on an object, it involves doing arithmetic on primritive data types, ie the objects primitive data values - not the object itself - as your example has shown.

--------------------------------------------------
Free Database Connection Pooling Software
 
I agree sedj, I think there's no point on adding confusing methods when what you're really doing is retrieve a value, operate it and get it back to its place.

Cheers.

Dian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top