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.
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 - http://www.coderanch.com/t/556340/java/java/Convert-decimal-value-binary
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 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?
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.
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 = 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 );
What you've done so far is to create an array, but you haven't put anything in it.