How to develop a type ?

How to develop a type ?
[30 byte] By [bueno.a] at [2007-11-26 14:28:51]
# 1

you mean a class?

public class AskBetterQuestions {

public AskBetterQuestions (){

}

}

mkoryaka at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 2

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 ?

bueno.a at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 3

> 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

JosAHa at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 4

> 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

}

aniseeda at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 5
Whoever set this task for you must have assumed that you knew that the two kinds of user-definable types in Java are classes and interfaces. And now you do.
DrLaszloJamfa at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 6

> > 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".

doremifasollatidoa at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 7
> How should that T defend itself against malicious attacks Should objects be allowed to carry concealed weapons?
DrLaszloJamfa at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 8
> in C++ we can do: > > Private Type Complex >re As Double > im As Double > End Typethat looks more like vb.
mkoryaka at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 9
> I know your code is just an example, but I'd suggest> primitives for re and im. That is, lowercase "d" on "double".Why?~
yawmarka at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 10

> 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

JosAHa at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 11
>Ah, it means you need to write a class called Complex.I and thought>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 !
bueno.a at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 12
>that looks more like vb.Ops.....You right !
bueno.a at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 13

> > 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.

doremifasollatidoa at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 14

> 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.

~

yawmarka at 2007-7-8 2:22:58 > top of Java-index,Java Essentials,Java Programming...
# 15

> > 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).

doremifasollatidoa at 2007-7-21 16:11:29 > top of Java-index,Java Essentials,Java Programming...
# 16

> 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~

yawmarka at 2007-7-21 16:11:29 > top of Java-index,Java Essentials,Java Programming...
# 17

> 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!

doremifasollatidoa at 2007-7-21 16:11:29 > top of Java-index,Java Essentials,Java Programming...
# 18

> 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 ;-)

JosAHa at 2007-7-21 16:11:29 > top of Java-index,Java Essentials,Java Programming...
# 19

> <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)

~

yawmarka at 2007-7-21 16:11:29 > top of Java-index,Java Essentials,Java Programming...
# 20
> Nitpick noted.> > yawmark (<-- original nitpicker)No, I was first! *beep!* *beep!* *beep!*kind regards,Jos (so there :-P ;-)
JosAHa at 2007-7-21 16:11:29 > top of Java-index,Java Essentials,Java Programming...
# 21
> No, I was first! *beep!* *beep!* *beep!*What ho, varlet, dost thou go cross-gartered?*beep!* *beepity!* *beep!* *beep!*Ha-HA!~
yawmarka at 2007-7-21 16:11:29 > top of Java-index,Java Essentials,Java Programming...
# 22
>or make it a Constant Field Value like>class java.sql.Types.COMPLEX>?>(but dont ask me how or where to define it .. :o[ )I see your very " clever " ....
bueno.a at 2007-7-21 16:11:29 > top of Java-index,Java Essentials,Java Programming...
# 23

> > 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 ;-)

JosAHa at 2007-7-21 16:11:29 > top of Java-index,Java Essentials,Java Programming...
# 24
> Now, once again: *beep!* en garde *beep!**beepity!* *beepity!* *beepity!* (<-- double-bladed)Ha-HA!~
yawmarka at 2007-7-21 16:11:29 > top of Java-index,Java Essentials,Java Programming...
# 25

> > 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

JosAHa at 2007-7-21 16:11:29 > top of Java-index,Java Essentials,Java Programming...
# 26
*honk.*How 'bout THAT, you old so-and-so?~;o)
yawmarka at 2007-7-21 16:11:29 > top of Java-index,Java Essentials,Java Programming...
# 27
> *honk.*> > How 'bout THAT, you old so-and-so?******!*Say what?kind regards,Jos ;-)
JosAHa at 2007-7-21 16:11:29 > top of Java-index,Java Essentials,Java Programming...