A don' t know exactly, I have a task : To develop a type representing complex number .
I know that in java i can create a class as type, but in C++ we can do:
Private Type Complex
re As Double
im As Double
End Type
can I do the same in java. Or type it's mean only Class ?
> How to develop a type ?
By designing it first. On top of that you don't go "ok, I want to develop a type T".
There should be a need for that type T. What do you want to 'say' to that
T thing; what do you want a T thing to return to you? What are its tasks
(responsibilities) and what can it rely on? What do you have to feed a T
in order to make a T survive? How should that T defend itself against
malicious attacks (read: incorrect invoking code)?
This is the fun part because you can design a T in a horizontal position
without having to use such a nasty computer. All you need is your brains
and a bit of scribbling paper.
kind regards,
Jos
> A don' t know exactly, I have a task : To develop a
> type representing complex number .
Ah, it means you need to write a class called Complex.
public class Complex {
private Double re;
private Double im;
// TODO: Methods here
}
> > A don' t know exactly, I have a task : To develop
> a
> > type representing complex number .
>
> Ah, it means you need to write a class called
> Complex.
>
> public class Complex {
>private Double re;
>private Double im;
>
>// TODO: Methods here
>}
I know your code is just an example, but I'd suggest primitives for re and im. That is, lowercase "d" on "double".
> I know your code is just an example, but I'd suggest
> primitives for re and im. That is, lowercase "d" on "double".
And then it would be fun to check whether or not it would be worthwhile
to make Complex extends Number. I say it isn't which says more about
the Number class than your design of a Complex class.
kind regards,
Jos
> > I know your code is just an example, but I'd suggest
> > primitives for re and im. That is, lowercase "d" on "double".
>
> Why?
I don't see a need to have them as objects. The re and im parts will always have a value (might be zero, but that's still a value). No need to handle "null" [yes, if you make it immutable, and set up the constructors correctly, you can avoid any "null"--but primitives and "zero" is easier]. Even with autoboxing, why store Double objects? You'll have to convert to primitives to do 'getMagnitude', 'getAngle', 'multiply', 'divide', 'add', 'subtract', or anything else, and would need to convert back to Double objects to create the new Complex object. So, storing as primitives seems more straightforward.
> I don't see a need to have them as objects.
I can think of several reasons to have the members as objects (e.g, BigInteger, with an option to represent the value as a BigDecimal), especially if accuracy is a concern. These reasons also encapsulate why the choice of double may not be a good idea for a particular situation.
~
> > I don't see a need to have them as objects.
>
> I can think of several reasons to have the members as
> objects (e.g, BigInteger, with an option to represent
> the value as a BigDecimal), especially if accuracy is
> a concern. These reasons also encapsulate why the
> choice of double may not be a good idea for a
> particular situation.
Okay, I'll accept that argument. For more scientific purposes, that argument sounds fair to me. For an implementation used to teach programming, I think double will suffice (maybe the professor could then have a discussion as to what you could do if you needed more accuracy for your computations).
> Okay, I'll accept that argument. For more scientific
> purposes, that argument sounds fair to me. For an
> implementation used to teach programming, I think
> double will suffice (maybe the professor could then
> have a discussion as to what you could do if you
> needed more accuracy for your computations).
Sounds good to me. Thanks for your amicable response.
Cheers~
> Sounds good to me. Thanks for your amicable response.
You're welcome. I see no reason to fight on something like this. You had a good point. "Double vs. double" doesn't make a difference accuracywise (as far as I know), but BigDecimal could make a difference. BigDecimal and Double obviously do make a difference accuracywise.
Cheers to you, too!
> Sounds good to me. Thanks for your amicable response.
<not so amicable nitpick>
That's why the Number class nilfisks big times. It ties any concrete
number to a primitive type and all the descendents thereof.
<not so amicable nitpick/>
kind regards,
Jos (< nitpicker when it comes to numbers ;-)
> <not so amicable nitpick>
> That's why the Number class nilfisks big times. It ties any concrete
> number to a primitive type and all the descendents thereof.
> <not so amicable nitpick/>
It all depends on the pedagogical target. I don't see a problem with using primitives for introductory instruction. As mentioned, I think there are practical reasons to *not* use them for more critical applications.
Nitpick noted.
yawmark (<-- original nitpicker)
~
> > No, I was first! *beep!* *beep!* *beep!*
>
> What ho, varlet, dost thou go cross-gartered?
>
> *beep!* *beepity!* *beep!* *beep!*
>
> Ha-HA!
Nonononono! How many times do I have to tell you that there are no
"beepeties" allowed in the gigue (nor the entree nor bouree) of the duel!
When will you ever learn, you barbarian.
Now, once again: *beep!* en garde *beep!*
kind regards,
Jos ;-)
> > Now, once again: *beep!* en garde *beep!*
>
> *beepity!* *beepity!* *beepity!* (<-- double-bladed)
>
> Ha-HA!
No grasshopper; you don't want me to alert the grand jury do you?
Beepeties are not allowed until the allegro of the duel. Remember that.
Now, for the third time: *beep!* (slowly now, slowly) *beep!* *beep!*
(yes, assume the position) *beep!* (advance now, but slowly) *beep!*
See? that's the way it's supposed to go; no plebeian beepeties and stuff ;-)
kind regards,
Jos