Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Number Base Calcuator application

Reply
Thread Tools

Number Base Calcuator application

 
 
Alex Buell
Guest
Posts: n/a
 
      07-24-2003
Hi,

I'm learning Java and I'm writing a number base calculator just like the
one in Arachnophilia 5.2. It has four JTextFields, for binary, octal,
decimal, and hexadecimal. The user can enter a number into any of these
four fields and see the converted number in the other fields in real time,
just as they type in the number.

The problem I've run into is that trying to update the other fields often
throws exceptions.

What is the best way to do what I've got in mind?

For example:

User types in 1 in the binary field
Rest of fields gets updated with '1'
User enters a 0 in the binary field
Rest of fields gets updated with '2'
User enters a 1 in the binary field
Rest of fields gets updated with '5'
User enters a 0 in the binary field
Octal field is updated with 12, decimal field is updated with 10, and
hexadecimal field is updated with a.

I've put the source up at
http://www.munted.org.uk/programming/basecalc.java so you can see what I'm
doing wrong.

Thanks for any help, it can only make me a better Java coder!

--
http://www.munted.org.uk

Telephone: A clever device by which two people in different places can
mutually admire each other's total inability to say anything coherent
 
Reply With Quote
 
 
 
 
Roger Irwin
Guest
Posts: n/a
 
      07-25-2003
What exceptions?

It may be that the best way is to catch the exceptions and handle them.



Alex Buell wrote:
> Hi,
>
> I'm learning Java and I'm writing a number base calculator just like the
> one in Arachnophilia 5.2. It has four JTextFields, for binary, octal,
> decimal, and hexadecimal. The user can enter a number into any of these
> four fields and see the converted number in the other fields in real time,
> just as they type in the number.
>
> The problem I've run into is that trying to update the other fields often
> throws exceptions.
>
> What is the best way to do what I've got in mind?
>
> For example:
>
> User types in 1 in the binary field
> Rest of fields gets updated with '1'
> User enters a 0 in the binary field
> Rest of fields gets updated with '2'
> User enters a 1 in the binary field
> Rest of fields gets updated with '5'
> User enters a 0 in the binary field
> Octal field is updated with 12, decimal field is updated with 10, and
> hexadecimal field is updated with a.
>
> I've put the source up at
> http://www.munted.org.uk/programming/basecalc.java so you can see what I'm
> doing wrong.
>
> Thanks for any help, it can only make me a better Java coder!
>


 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      07-25-2003
Alex Buell wrote:
> I'm learning Java and I'm writing a number base calculator just like the
> one in Arachnophilia 5.2. It has four JTextFields, for binary, octal,
> decimal, and hexadecimal. The user can enter a number into any of these
> four fields and see the converted number in the other fields in real time,
> just as they type in the number.
>
> The problem I've run into is that trying to update the other fields often
> throws exceptions.
>
> What is the best way to do what I've got in mind?


A good way to do this is to link the models for the fields together,
perhaps this is already what you're doing. In this case, part of the
problem is that the various text fields' immediate models (each one a
Document instance) are really part of the view -- they present different
representations of the same underlying model, a number.

What I would probably do for your particular problem is write a custom
Document class whose instances could be used as the models for your text
fields. This class would be configurable with a particular number
radix, so that I would only need one class instead of several. (I.e. it
would replace your four (!) custom documents with one. It would handle
conversion of the underlying number into the correct display format,
validation of new input, and update of the underlying number. Then I
would write a class to encapsulate the underlying number, complete with
a listener API that the custom Documents would use to keep abreast of
changes to the number applied by any other instances using the same
number instance.

In comparison to your code, that replaces eight classes with two, and
relieves you from having to wire things together at the front end. It
prevents any fundamental data inconsistency because there is only one
copy of the number being represented. (There is still an issue
regarding getting the display to update everywhere when a change has
been made, but once you get that once it is solved for good.) It also
relieves the application from the tight coupling imposed by the
KeyListener implementations you are currently using -- with my suggested
approach any number of numeric fields displaying data in the same or
different bases could be supported without any class knowing about the
others. (The relevant _instance_ of the number class would know about
all the dependant instances of the Document classes, but that's it.)


John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Alex Buell
Guest
Posts: n/a
 
      07-25-2003
On Fri, 25 Jul 2003, John C. Bollinger wrote:

> Alex Buell wrote:
> > I'm learning Java and I'm writing a number base calculator just like the
> > one in Arachnophilia 5.2. It has four JTextFields, for binary, octal,
> > decimal, and hexadecimal. The user can enter a number into any of these
> > four fields and see the converted number in the other fields in real time,
> > just as they type in the number.
> >
> > The problem I've run into is that trying to update the other fields often
> > throws exceptions.
> >
> > What is the best way to do what I've got in mind?

>
> A good way to do this is to link the models for the fields together,
> perhaps this is already what you're doing. In this case, part of the
> problem is that the various text fields' immediate models (each one a
> Document instance) are really part of the view -- they present different
> representations of the same underlying model, a number.
>
> What I would probably do for your particular problem is write a custom
> Document class whose instances could be used as the models for your text
> fields. This class would be configurable with a particular number
> radix, so that I would only need one class instead of several. (I.e. it
> would replace your four (!) custom documents with one. It would handle
> conversion of the underlying number into the correct display format,
> validation of new input, and update of the underlying number. Then I
> would write a class to encapsulate the underlying number, complete with
> a listener API that the custom Documents would use to keep abreast of
> changes to the number applied by any other instances using the same
> number instance.
>
> In comparison to your code, that replaces eight classes with two, and
> relieves you from having to wire things together at the front end. It
> prevents any fundamental data inconsistency because there is only one
> copy of the number being represented. (There is still an issue
> regarding getting the display to update everywhere when a change has
> been made, but once you get that once it is solved for good.) It also
> relieves the application from the tight coupling imposed by the
> KeyListener implementations you are currently using -- with my suggested
> approach any number of numeric fields displaying data in the same or
> different bases could be supported without any class knowing about the
> others. (The relevant _instance_ of the number class would know about
> all the dependant instances of the Document classes, but that's it.)


Thank you for all the advice, I'l reimplement the classes in a more
generic way.

--
http://www.munted.org.uk

Telephone: A clever device by which two people in different places can
mutually admire each other's total inability to say anything coherent
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-29-2003
On Fri, 25 Jul 2003 08:27:23 GMT, Roger Irwin <(E-Mail Removed)> wrote
or quoted :

>> I'm learning Java and I'm writing a number base calculator just like the
>> one in Arachnophilia 5.2. It has four JTextFields, for binary, octal,
>> decimal, and hexadecimal.


have a look at http://mindprod/projhexcalc.html

for some ideas on how to implement. Work internally with binary longs
or doubles and format your displays at the last second.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
help new C++ progrmaer program a calcuator andrewquerol@gmail.com C++ 7 06-24-2007 09:39 PM
how to translate base 10 number into base 2 number chen li Ruby 6 01-23-2007 07:45 PM
Implementing a "calcuator" feature Sonny Chee Ruby 1 01-21-2007 12:57 AM
OT: Number Nine, Number Nine, Number Nine FrisbeeŽ MCSE 37 09-26-2005 04:06 PM
How to print a 32-bit number as a base-10 number Steve C Programming 5 12-13-2004 07:07 PM



Advertisments