Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

object arithmetic

Thread view: 
stocksami@earthlink.net - 05 May 2006 15:38 GMT
I am developing a java application that has some classes that contain
arrays of numerical data that have multiple dimensions.  I would like
to be able to add objects together or subtract one from another using
simple statements such as:

MyClass m = m1 + m2;

where m1 and m2 are objects of class MyClass.

How would I do that?  Would it be a lot simpler to just write a
constructor that has the 2 objects and the operator as arguments?

Thanks
Gordon Beaton - 05 May 2006 15:43 GMT
> I would like to be able to add objects together or subtract one from
> another using simple statements such as:
[quoted text clipped - 5 lines]
> How would I do that? Would it be a lot simpler to just write a
> constructor that has the 2 objects and the operator as arguments?

Since you can't overload operators in Java, you are left with these
kinds of solutions:

 MyClass m = m1.add(m2);

or:

 MyClass m = MyClass.add(m1,m2);

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

stocksami@earthlink.net - 05 May 2006 15:54 GMT
> > I would like to be able to add objects together or subtract one from
> > another using simple statements such as:
[quoted text clipped - 18 lines]
>
> --

Thanks,  I didn't know that operators couldn't be overloaded.

Clark
Matt Humphrey - 05 May 2006 15:47 GMT
>I am developing a java application that has some classes that contain
> arrays of numerical data that have multiple dimensions.  I would like
[quoted text clipped - 7 lines]
> How would I do that?  Would it be a lot simpler to just write a
> constructor that has the 2 objects and the operator as arguments?

You can do it via the constructor, but that implies that every operation
automatically creates new objects and it ties up the constructor logic doing
an implicit operation.  A cleaner and simpler solution is to do

MyClass temp = new MyClass ();
temp.set (m1);
temp.add (m2);

This means you only create new objects when you want to preserve the
original parameters.  I prefer this technique for long chains of operations
and it leaves you open to create all new kinds of operations. (Matrix /
Complex math is often done this way.)  You can combine the constructor and
set with something like

MyClass temp = new MyClass (m1);
or
MyClass temp = m1.copy ();

But the effect is largely the same.

Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/
stocksami@earthlink.net - 05 May 2006 16:34 GMT
> >I am developing a java application that has some classes that contain
> > arrays of numerical data that have multiple dimensions.  I would like
[quoted text clipped - 30 lines]
> Cheers,
> Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/

For this particular case I have an entire flatfile DB loaded into
memory and the enduser will be using Matlab to arbitrarily perform
calculations.  The original data has to remain unchanged.  Matlab has
the marvelous ability to access object data and method results in a
running java application.

I'll keep your comments in mind when I'm writing the calulation
intensive part of the application, though.  I'll be doing more of that
later.

Thanks,
Clark


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.