LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-02-2004, 10:22 AM   #1
TexasDex
Member
 
Registered: Feb 2003
Location: The Attic. Nowhere near Texas.
Distribution: Gentoo, Kubuntu, formerly LFS, SuSE, and RedHat
Posts: 133

Rep: Reputation: 15
defining "+" operator for new datatype


I am learning to program in JAVA and to teach myself class creation I have made the uConvert class, which has several sub-classes Length, Time, and Mass.



I want to be able to add different length objects

Code:
Length yourHeight = new Length();
Length myHeight = new Length();
myHeight.define(163,"cm");
yourHeight.define(68,"in");
Length ourHeight;
so that I can add them like:
Code:
ourHeight = myHeight + yourHeight;
instead of having to use this somewhat awkward method
Code:
ourHeight = myHeight.plus(yourHeight);
It works, but it is not "slick" or programmer-friendly

I have searched JAVA books, googled it to death, asked my Comp Sci instructor, to no avail. It always gives me this error:
"+" operator is not defined for Length,Length

I am using the Java SDK 1.3.1_09 on Windows XP, 2K and Redhat 8 machines, and nothing works.
 
Old 04-02-2004, 11:22 AM   #2
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Sorry, there's no way to overload operators in Java like there is in C++. The java developers hard-coded the operators, and anything else has to be done like you're doing it, now.
 
Old 04-02-2004, 03:00 PM   #3
TexasDex
Member
 
Registered: Feb 2003
Location: The Attic. Nowhere near Texas.
Distribution: Gentoo, Kubuntu, formerly LFS, SuSE, and RedHat
Posts: 133

Original Poster
Rep: Reputation: 15
That sucks

Why on earth wouldn't they let you do that? Is there some specific reason for it?

Well thanks anyway.
 
Old 04-02-2004, 03:18 PM   #4
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Look up JFront on google. It's a java library that allows operator overloading. I've not tried it, but the example given looks easy enough.
 
Old 04-02-2004, 05:25 PM   #5
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
They did do that on purpose, yes. http://forum.java.sun.com/thread.jsp...rt=0&trange=15
 
Old 04-02-2004, 07:18 PM   #6
kooch
Member
 
Registered: Mar 2004
Location: Upstate NY
Distribution: Slackware/YDL
Posts: 77

Rep: Reputation: 15
really how tough of a concept is operator overloading?

not to sound haughty but some people just shouldn't program....

LFM vs. LFSP
 
Old 04-03-2004, 05:57 AM   #7
Komakino
Senior Member
 
Registered: Feb 2004
Location: Somerset, England
Distribution: Slackware 10.2, Slackware 10.0, Ubuntu 9.10
Posts: 1,938

Rep: Reputation: 55
Quote:
Originally posted by kooch
really how tough of a concept is operator overloading?

not to sound haughty but some people just shouldn't program....

LFM vs. LFSP
I don't see the relevance... no one here is saying operator overloading is a difficult concept, only querying why something that useful isn't included in Java.
 
Old 04-03-2004, 10:39 AM   #8
TexasDex
Member
 
Registered: Feb 2003
Location: The Attic. Nowhere near Texas.
Distribution: Gentoo, Kubuntu, formerly LFS, SuSE, and RedHat
Posts: 133

Original Poster
Rep: Reputation: 15
the relevance is that the JAVA designers thought that operator overloading was too difficult a concept for "the masses", and therefore left it out.

From what I can see, JFront is just a macro that "fixes" source code to change the code statements from the "a + b" syntax to the "a.operatorplus(b);" syntax. I'll look at it, but I really want my class to be reusable by other programmers (even if I never finish it) with the + operator. From what I've heard there is minor movement in the JAVA designers to include op. overloading but I'm not holding my breath.

BTW, when (if) I get the uConvert class and all it's sub-classes finished I hope to be able to multiply different types of units so that you can multiply different types of units and get different types of units
e.g.
Length * Length = Area
Area * Length = Volume

It would work something like this:
Code:
Length tracklength = new Length();
track.define(.25,"mi");

Time race1t = new Time();
Time.define(11.548,"sec");

Velocity myspeed = new Velocity();

// myspeed = tracklength / race1t  // doesn't work!  Damnit
myspeed = tracklength.div(race1t);

myspeed.outputType = 3; // type 3 = Miles/Hr
System.out.println(myspeed);
If I wanted to I could get really detailed and include stuff like Pascals, Joules, and even Moles.

Has anyone here done something like that, creating a comprehensive set of measurement datatypes? I will still continue to work on it if somebody already has, because this is primarily for my own education but it would be cool to contribute it to the community. BTW it will be under the GPL if I do bother to finish/release it.

Last edited by TexasDex; 04-03-2004 at 10:49 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Telling people to use "Google," to "RTFM," or "Use the search feature" Ausar General 77 03-21-2010 11:26 AM
"Xlib: extension "XFree86-DRI" missing on display ":0.0"." zaps Linux - Games 9 05-14-2007 03:07 PM
Perl question concerning the "next" operator joesbox Programming 3 03-27-2005 08:59 PM
C++ / Can't access class members through the "." operator lokee Programming 7 07-05-2004 12:06 PM
"Defining" Linux Distros OldBob Linux - General 3 12-01-2003 01:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration