Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to handle the lack of operator overload ?

Reply
Thread Tools

How to handle the lack of operator overload ?

 
 
Mr Smith
Guest
Posts: n/a
 
      02-09-2005
Hello,

In a table, i have various object. Right now, i'm writing the Double object code. But in Java (AFAIK) you can't overload operator, so, what is the best on a dummy operation: [i, j] += [i, j + 1]

1/
set(i, j, new Double(((Double)get(i, j)).doubleValue() + ((Double)get(i, j + 1)).doubleValue()))

Simply awful to read, it's just an addition here...


2/
setdouble(i, j, getdouble(i, j) + getdouble(i, j + 1))

Could be ok, but it needs to put the casts in a lot of getters/setters, heavy work.


3/
set(i, j, Op.addDouble(get(i, j), get(i, j + 1)))
with
static addDouble(Object a, Object b) {
double c = ((Double)a).doubleValue();
double d = ((Double)b).doubleValue();
return new Double(c + d);
}


What is you rpersonnal choice? What are the pros and cons?

Thanx for your advices
 
Reply With Quote
 
 
 
 
Andrea Desole
Guest
Posts: n/a
 
      02-09-2005
Mr Smith wrote:
> Hello,
>
> In a table, i have various object. Right now, i'm writing the Double object code. But in Java (AFAIK) you can't overload operator, so, what is the best on a dummy operation: [i, j] += [i, j + 1]
>
> 1/
> set(i, j, new Double(((Double)get(i, j)).doubleValue() + ((Double)get(i, j + 1)).doubleValue()))
>
> Simply awful to read, it's just an addition here...


agreed

>
>
> 2/
> setdouble(i, j, getdouble(i, j) + getdouble(i, j + 1))
>
> Could be ok, but it needs to put the casts in a lot of getters/setters, heavy work.
>
>
> 3/
> set(i, j, Op.addDouble(get(i, j), get(i, j + 1)))
> with
> static addDouble(Object a, Object b) {
> double c = ((Double)a).doubleValue();
> double d = ((Double)b).doubleValue();
> return new Double(c + d);
> }
>
>
> What is you rpersonnal choice? What are the pros and cons?


I would go for number 2, because it makes clear that it's a table of
doubles. I think most of the times the getters and setters where you
have to cast are not that many. If it's not the case maybe you can
implement a couple of extra setters and getters in your table to do the
conversion for you; that would still keep the type safety.
I don't really like 3, it allows clients of your class to pass whatever
they want without even thinking about it.
 
Reply With Quote
 
 
 
 
Mr Smith
Guest
Posts: n/a
 
      02-09-2005
On Wed, 09 Feb 2005 14:59:39 +0100
Andrea Desole <(E-Mail Removed)_SPAM_PLEASE.nl> wrote:

>> [...]

> I would go for number 2, because it makes clear that it's a table of
> doubles. I think most of the times the getters and setters where you


it's not a table of Double, it's a table with some column of Double

> have to cast are not that many. If it's not the case maybe you can
> implement a couple of extra setters and getters in your table to do the
> conversion for you; that would still keep the type safety.
> I don't really like 3, it allows clients of your class to pass whatever
> they want without even thinking about it.


thx for your comments
 
Reply With Quote
 
Andrea Desole
Guest
Posts: n/a
 
      02-09-2005
Mr Smith wrote:
>
> it's not a table of Double, it's a table with some column of Double


you have a table with mixed objects? Mmmm, that sounds dangerous. I
would try to restrict it somehow.
 
Reply With Quote
 
Mr Smith
Guest
Posts: n/a
 
      02-09-2005
On Wed, 09 Feb 2005 16:32:38 +0100
Andrea Desole <(E-Mail Removed)_SPAM_PLEASE.nl> wrote:

> Mr Smith wrote:
> >
> > it's not a table of Double, it's a table with some column of Double

>
> you have a table with mixed objects? Mmmm, that sounds dangerous. I
> would try to restrict it somehow.


well it's not my choice my boss want a database/spreadsheet with mixed column type
 
Reply With Quote
 
Dotty
Guest
Posts: n/a
 
      02-09-2005

"Mr Smith" <(E-Mail Removed)> wrote in message
news:20050209172946.7d59cd74@recif...
> On Wed, 09 Feb 2005 16:32:38 +0100
> Andrea Desole <(E-Mail Removed)_SPAM_PLEASE.nl> wrote:
>
> > Mr Smith wrote:
> > >
> > > it's not a table of Double, it's a table with some column of Double

> >
> > you have a table with mixed objects? Mmmm, that sounds dangerous. I
> > would try to restrict it somehow.

>
> well it's not my choice my boss want a database/spreadsheet with mixed

column type

I do this, just use "Object[][] data;"
then put a different type in each column. Works well with database.
Then pass this array to the JTable constructor.

int k = 0; // count records
while (rec.next()) {
for (int i = 0; i < numCols; i++) {
data[k][i] = rec.getObject(i + 1);
}
k++;
}


 
Reply With Quote
 
Steve Horsley
Guest
Posts: n/a
 
      02-09-2005
Mr Smith wrote:
> On Wed, 09 Feb 2005 14:59:39 +0100
> Andrea Desole <(E-Mail Removed)_SPAM_PLEASE.nl> wrote:
>
>
>>>[...]

>>
>>I would go for number 2, because it makes clear that it's a table of
>>doubles. I think most of the times the getters and setters where you

>
>
> it's not a table of Double, it's a table with some column of Double
>
>
>>have to cast are not that many. If it's not the case maybe you can
>>implement a couple of extra setters and getters in your table to do the
>>conversion for you; that would still keep the type safety.
>>I don't really like 3, it allows clients of your class to pass whatever
>>they want without even thinking about it.

>
>
> thx for your comments


That sounds like a List of SomeClass where each instance of SomeClass
represents one row from the spreadsheet to me. So that each line in the
spreadsheet represents one item of some sort. It may well simplify
things to deal with a one-dimentional list of some more complex object
instead.

Steve
 
Reply With Quote
 
Mr Smith
Guest
Posts: n/a
 
      02-10-2005
On Wed, 09 Feb 2005 22:41:06 +0000
Steve Horsley <(E-Mail Removed)> wrote:

> > [...]

>
> That sounds like a List of SomeClass where each instance of SomeClass
> represents one row from the spreadsheet to me. So that each line in the
> spreadsheet represents one item of some sort. It may well simplify
> things to deal with a one-dimentional list of some more complex object
> instead.


Sounds like a good idea, but i don't know if at some point we will not be limited by this approach. We do data analysis, and i'm pretty sure that in the future the column will be used as complex objects. So i'd rather not choose to see rows or columns as complex object, to keep it simple.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
exponentiation operator (lack of) carlos@colorado.edu C Programming 67 01-04-2006 05:27 AM
T::operator int () const ambiguous with T::operator Handle () const? Tim Clacy C++ 15 05-30-2005 02:14 AM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM
overload operator - Matthew Monopole C++ 1 08-08-2003 09:39 AM



Advertisments