Greetings,
I've been puzzling over a programming issue for a little while and
wanted to get some help with it. Here's the issue: I want to have a
common base class called Matrix1x3 to hold three numbers. When I
subclass from Matrix1x3 to form Vector, I want the three numbers to be
doubles or floats. When I subclass from Matrix1x3 to form
Orientation, I want the three numbers to be Angle-s (another class
which encapsulates the functionality of operating on three different
representations of a double-valued angle - degrees, radians, and
mils). In other words, I want to use 1x3 matrices in my program, but
one type is for vector operations and the other is for operations on
orientations of pitch, roll, and yaw. Maybe I can't even do this, I
don't know. I was wondering if there was a way to maybe have the base
class contain some raw variant type that could be refined based on
each subclass...help me out here...
I think it might be kind of "useless" if the base class did not
contain the raw data members, but it seems as if what I want to do
would preclude storing any common data in the class, so then I have to
ask myself what the purpose would be of even having the base class in
the first place.
Furthermore, I wondered to myself, if I had a base class that had a
method "transpose()" for example (being the name, after all, of a
basic matrix operation):
a) Would I be forced to implement (and use/invoke) "transpose()" in
the subclass, since it was extended from the Matrix1x3 class?
b) Would there be any way that I could just use or invoke
Matrix1x3::transpose() without having to "go through" the subclass?
Another example - Let's say both Matrix1x3 and Vector have an "add()"
method, which means they each know how to add themselves. If I
actually implemented the code in Matrix1x3's add(), could I just get
by with calling Matrix1x3::add() in Vector::add()? (I admit that
adding matrices is kind of trivial, but there could be more
complicated operations which could be shared by both classes.)
I guess what I am trying to do is figure out the appropriate way to
write OO-based math code, in essence.
I realize this problem could apply to both Java and C++ but since it
grew out of some Java programming I wanted to post it here. Thanks in
advance for any suggestions.
Mike
Patricia Shanahan - 05 Jul 2007 20:37 GMT
...
> a) Would I be forced to implement (and use/invoke) "transpose()" in
> the subclass, since it was extended from the Matrix1x3 class?
> b) Would there be any way that I could just use or invoke
> Matrix1x3::transpose() without having to "go through" the subclass?
...
Here's another approach. Consider thinking of these things as multiple
views. You can have a single data object, with multiple getXXXView
methods, each returning an object implementing interface XXXView.
For example getMatrixView().transpose() would return the transposed form
of the matrix representation. The transpose operation could actually be
implemented in an abstract base class for Matrix.
Whether a particular view interface contains transpose() depends on
whether transposition makes sense for that view of the data.
Patricia
Roedy Green - 05 Jul 2007 23:44 GMT
>When I subclass from Matrix1x3 to form
>Orientation,
Consider defining a common interface and have two completely
independent classes implement it. This is presuming the
implementations share little logic.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Zerex71 - 06 Jul 2007 15:15 GMT
On Jul 5, 6:44 pm, Roedy Green <see_webs...@mindprod.com.invalid>
wrote:
> >When I subclass from Matrix1x3 to form
> >Orientation,
[quoted text clipped - 5 lines]
> Roedy Green Canadian Mind Products
> The Java Glossaryhttp://mindprod.com
Actually, just so we're all clear on this - Orientation and Vector
would both be subclasses of Matrix1x3 in my initial proposed
implementation. Vector has three doubles; Orientation has three
Angles, and Angle consists of three doubles itself (which is only
coincidental because I've chosen to implement three representations
for angular measurement).
The other question I forgot to ask is, Riddle me this, Batpeople: If I
were to put some raw data types such as an array of three doubles:
protected double array[] = new double[3];
and if I want to refer to the members in Vector as x, y, and z (since
these will be Cartesian vectors),
a) Is such a thing even possible?
b) Do I just add extra members and ensure the underlying array is kept
in sync with the extra data members (probably a bad programming/design
idea)
In other words, referring to the data members as x,y, and z only
really makes sense in the context of working with Vectors, not
matrices, where I would use something like array[i] or array[i][j] for
a multidimensional array.
Also, regarding my variant question (thinking similar to VBasic), is
there a way to specify a generic type that I can refine? Would it be
as 'silly' as saying that Matrix1x3 contains three Objects, and then
overriding(?) or specifying the actual types in the subclass?
Mike