• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Getting wrong answers for conversion

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,


I've been working on a program for base conversion.
After making some adjustments I am yet to get the "correct" answer for my conversion. For example when i key in 100 i get 10 as my answer.

I am using a label to set the text of my answer but all it does is take the number entered in the textfield i guess. Appreciate all replies.

I understand that my Compute method doesn't get called. How do i accomplish that?


[ February 09, 2005: Message edited by: Alisha Burke ]
 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Alisha...

Okay I took a look at it and made these changes to your compute method...

included are comment documenting the changes... basically we know that binary number are read right
to left, so we must start at the last element and work our way to the first. So as an example 1011 is
converted to a 4 element char array {'1','0','1','1'} with the index increasing from left to right.
And this is where a problem arises... If we use the array index to compute what power 2 is raised to
the first element ( left most ) will be 0 with the last in our example being 3. So whether we count up
or down the coversion will be wrong. So what we have to do is either reverse the array
( which would require even more code ) or simple reverse how we increase the power that 2 is raised by.
i.e.
3-2-1-0 array index reading from right to left
1-1-0-1 element values reversed ( actual is 1011 = 11)
1-2-3-4 exponent values reading from right to left

the reason this was done in reverse is so that it follows how we would logically interpet a binary number...

Also what was done was that the String numberkeyin that get returned from you numberField.getText() method
is converted to a char array using the method java.lang.String.toCharArray() which all Strings are able to
utilize. Doing the binary conversion is much easier with a char array than using the charAt() method...
at least I think so...

Next... you mentioned that your Compute method does not get called...
So here is another snippet that I modified to show you how to call it from your action performed method...


here basically what was done was the call to Compute() was inserted before you updated your label.
A class can call it's own method just by calling it method name and parameters if any...

Simple example...



Anyway here is you entire class so that you can get a better idea of what was done...


Hope this helps...

- Liam...

- ' He who never sleeps... '
 
Alisha Burke
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Liam,

Thanks a lot! I was so worried i'd never get the answer for my binary to decimal conversion. Now I can start working on my octal base conversion. Just wondering is it necessary to create another for loop for my octal to decimal conversion.

And there is one part of the code that you gave me that i just can't quite grasp after a couple of times reading it. I would really appreciate it if you could explain it in the simplest manner as possible.



I am confused:

a) with the bits.length is that the length() method your using there?
b} how come its i-- and not i++

I understand that binary reads from right to left and we need to start off with the right most element. I just don't understand how the code above manage to accomplish that.
 
Liam Tiarnach
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again ...

Okay, no problem... let's start off with the first question...

here we see the loop setup to run in reverse. So let break it down... ( you all ready know the for loop syntax, so I explain the code within...

we initilize int i to be bits.length which using a 4 element array as an example would return a length of 4, but arrays are 0 indexed, so the last element in that array would be 3, so we subtract 1 to correct it.

basically, once we reach 0, end the loop

each iteration decrements i by 1... a count down until it reaches 0 which ends the loop...

then we setup the loop so that is start with the right most element.

here i called "i" index for clarity...
next we check if the element is 1, if so then we add it binary value to our answer variable

so now we are almost at the end of the loop, before we go to the next element to the left, we need to increase our exponent value by 1

and the loop will count down until 0 is reached...
So using the example again using a 4 element array...
1-0-1-0 <- our char array representing a binary number 1010
0-1-2-3 <- our char array index values for each element in the char array
3-2-1-0 <- the exponent that we raise 2 by ( Math.pow(2.0,powers))

let step through it...
on the loops first run our index (index)starts at 3 (index = bits.length-1)
and our exponent (powers) is 0
at index 3, our binary number (bits[index]) is '0' so no math is done
answer current is 0
now that the check is done we increment our exponent (powers) by one (powers++)

on then second run of our loop, our index (index) is now 2 (index--)
and our exponent (powers) is now 1
at index 2, our binary number (bits[index]) is now '1' so we do the math (answer += Math.pow(2.0,powers)
answer is now 2
and all that done we increment our exponent (powers) by one again (powers++)

on the third run of out loop, you guessed it... index (index) is now 1 (index--)
our exponent (powers) is now 2
and at index 1, our binary number (bits[index]) is again '0' so no math
answer is still 2
and again we increment our exponent (powers) by one (powers++)

on the four and final run of our loop, index (index) is now 0 (index--)
our exponent (powers) has now reached 3
at index 0, our binary number (bits[index]) in '1' so we do the math (Math.pow(2.0,powers)
and answer is now 10 ( 2 + 8 )
although we still increment our exponent (powers) we no longer need it since our loop has ended...

Hopefully this helps...
[ February 09, 2005: Message edited by: Liam Tiarnach ]
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Often you use a for loop to count up from zero, but it doesn't have to. As described above, a for loop can just as easily count down. In this case, "i--" makes a lot of sense.

Layne
 
Alisha Burke
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the thourough explanation Liam... it sure cleared up a lot of my doubts.

*Smile with appreciation*
 
Liam Tiarnach
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want to take a look at this method...
reply
    Bookmark Topic Watch Topic
  • New Topic