• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Base Converter

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok so I have an assignment to use iteration to convert from base "a" to base "b"? I know there is such a simple way to do this but since I am a beginner I guess Imma take the long hard road since I don't know all the commands. Ummmmm here is the problem, I am trying to write a constructor from base 10 to 2. So I named the constructor "public double base10_to_2(double in)". I was planning on using a for loop for the iteration of course like "

for ( double a = in; q > 0)
double q = a/2;
double "r" = a%2;

**Here is where the problem is** t
How am I suppose to store the remainder in a variable and then return it to iterate to show that if that quotient is greater than 0 to go again. But after it goes it again it stores it in a different variable then I display the answer reversed. Like I know its so wrong right now but here is an example because I need to understand base conversion in general I know.

11 base 10 to base 2 is 1011 . 11/ 2 your quotient is 5, but remainder is 1 and 5 / 2 gives you quotient 2 but remainder 1 and so on. I noticed that once you do this whole method the answer is all the remainders written backwards. Like from 11 you get 1101 but clearly its 1011. I know I just dragged my whole explanation but I am so lost and its confusing me sooo much and I can't really think straight. But if there is a simple way to do this then please show me.
 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is it possible to store the remainders in an array and then reverse the order of the array?
 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Some pointers -

1. You are writing a method and not a constructor.
2. You need to use while instead of for loop as you loop till a condition gets satisfied. So check for remainder > 0 in the while condition.
3 Yes you can store all binary (0 or 1) elements in an array and reverse them.

Check this recent thread about decimal to binary conversion discussion - https://coderanch.com/t/556340/java/java/Convert-decimal-value-binary
 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok... I cant seem to get it working yet. I need to really sit down with it but Im going to show my current code. I have to use double "num" but see that double is really messing up all my coding I want to do. I can't increment the array because its a double and its telling me Im losing precision.

 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It will be good for you to intend the code better.



The compilation errors I received were -
1. Duplicate usage of variable in in the base10_to_2 method. See you have already have a double variable named in (in the method argument)
2. int should be used to specify array size. But you have used a double variable c.

There is also a while loop with no starting and ending curly braces. In the indented code you see that only the first statement is under the while loop. Please read while condition

Is there any specific reason for you to have array to store the digits. Prefer String as specified by the other thread I posted in my previous reply.
Reasons are -
1. You can only store a specific number of binary digits - which will be equal to the size of the array.
2. 0 gets assigned as a default value for int arrays. That will lead to confusion when you intend to print 1's and 0's binary representations.

Better use String to append the binary digits which you compute.
 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, but how do I go about storing the values I calculated during the while loop to be stored in the array. I was thinking of putting the remainder "r" in the array, that was it can store the values I calculated. Like it saves all the values during its looping until the quotient hits zero. Yea I looked at the topic but I got kinda confused a bit on what was going on.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tarrell Fletcher wrote:Ok, but how do I go about storing the values I calculated during the while loop to be stored in the array. I was thinking of putting the remainder "r" in the array, that was it can store the values I calculated. Like it saves all the values during its looping until the quotient hits zero.


Seems reasonable to me, but I don't see anywhere where you're storing the remainder in the array. You've created it, sure, but until you actually put something in it, all its elements will be 0 (or null).

Yea I looked at the topic but I got kinda confused a bit on what was going on.


I can't believe your teacher gave you a problem like this without thinking that you knew how to use arrays. Or were you catching some ZZZs when the bit about addressing array elements came up?

Winston
 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Lol no see my computer science course is a bit flawed. The only thing I needed to take the course was to pass cal 1 and thats it. The course gets started and its like you should know how to use Java. My professor thinks that taking java should be a prerequisite because now its like I am trying to learn java at the same time do these assignments. Arrays kinda confuse me period, but this lab is due like Wednesday. I just like to start early since I know I am gonna get lost and not know how to do certain things in the language.
 
Tarrell Fletcher
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main assignment is to

A. Write, compile, and run a Java program that: (1) uses a single line comment to
document your source code with your name and section number. This should be the
very first line of your program, (2) prints your name and section number as the first line
of your output, (3) prompts the user to enter a number, its base, and base to be
converted to (4) reads these inputs from the keyboard and stores the number in a
double “num”, base b in int “b”, base a in int “a”. (5) uses iteration to calculate the
b a a conversion of num to num (6) uses iteration to calculate the conversion of num to
b num (7) prints the inputs num, b, and a the values of the conversions in appropriately
formatted and informative manner.

This is the assignment. I think there is a simple way to do it , but like I said since I don't know the language that well I gotta do it the long drawn out way I suppose.
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take one step at a time. Code the step. Mark it complete. And then mover over to other.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tarrell Fletcher wrote:This is the assignment. I think there is a simple way to do it , but like I said since I don't know the language that well I gotta do it the long drawn out way I suppose.


Well, since you plainly understand the problem, and have had a try, I'll give you a kick start.

An array is a structure that holds many elements, numbered from 0 (ZERO. DO NOT FORGET). If you have an array x, that you want to create with n element slots in it, you use
type[] x = new type[n];
where type is the type of each element in the array.

If you want to put something in said array, maybe in the 3rd element (NUMBERED FROM ZERO):
x[2] = somevalue;
and 2 doesn't have to be a literal number; it can be any variable or expression that evaluates to an int.

And to get something out, exactly the same notation:
System.out.println( x[2] );

What you've done so far is to create an array, but you haven't put anything in it.

Winston
 
Yeah, but is it art? What do you think tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic