ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Gentoo, Kubuntu, formerly LFS, SuSE, and RedHat
Posts: 133
Rep:
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.
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.
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.
Distribution: Gentoo, Kubuntu, formerly LFS, SuSE, and RedHat
Posts: 133
Original Poster
Rep:
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.