so I have this homework to convert decimal input into binary using an iterative method as well as a recursive one. Now no worries, I think already got that down =P. The problem is, the output is obviously backwards. Is there a way I can add some extra lines to solve this problem, or must I write a method to flip the output around =/.
Here's the code (if you wish, I also want to see how I'm doing with documentation, I want it to be as clear as water =D) :
/**
Assign5
Converts decimals to binary using an iterative or a recursive method
*/
import java.util.*;
public class bconvert
{
/**
user input is allowed with the creation of object 'in' for the Scanner class
of the imported java utilities pack.
*/
int x,y;
Scanner in = new Scanner(System.in);
public void conversion()
{
int x = in.nextInt();
/**
to convert decimal to binary in one method, divide a number by 2 and record the remainder, and keep dividing until it is 0
a while loop is used to carry out this method as long as the number is not 0.
variable x is assigned a number by user input.
*/
while(x%26gt;0)
{
y=x%2;
x=x/2;
System.out.print(y);
/**
The while loop will run until the variable x is equal to 0. While running,
this loop will get the remainder of x divided by 2, assign the remainder to variable y,
divides x by 2, and prints out y to display to the user The process repeats until x is 0.
*/
}
}
public void convRecursive(int n)
{
/**
This recursive method uses an assigned integer for testing purposes, variable n, and will
get the remainder of n divided by 2 using the modulo(%) operator. The remainder is assigned to
variable x, which will then be sent to be output and be displayed to the user.
Recursion process begins as the method calls on itself to do the computation again, the only
difference is now it will compute to solve n divided by 2.
A stopping condition is written with the if loop to check whether the number is no longer divisible by 2,
and if the condition is true, the method will stop.
*/
x = n%2;
System.out.print(x);
if(n==0)
{
System.exit(0);
}
convRecursive(n/2);
}
/**
a main method is added to run the program
object btest is created for class bconvert to test either method
conversion() or convRecursive(int n)
for now, convRecursive(int n) is being tested, and an integer i is created
and assigned a number to test the method.
*/
public static void main(String[] args)
{
int i = 66;
bconvert btest = new bconvert();
btest.convRecursive(i);
}
}|||First of all, there is a common misconception that these converters convert "from decimal" to binary. Numbers are stored on computers (typically) in binary, the only time a conversion from decimal happens is when a number is read from a string of digit characters (like if it were read from the user, like by the Scanner class, or from an ASCII file). So after the line "int i = 66;", "i" is stored in binary. The conversion involved in this (and many similar) problem(s) is to turn the number (which happens to be stored in binary, but the algorithm doesn't care) into a string of "bit" characters ("0" and "1" characters). If the problem were to take a string of digit characters ("0".."9" characters) and turn that into a string of "bit" characters ("0" and "1" characters), THAT would be a kind of conversion from decimal to binary.
Sorry for the lecture, but I see this misconception all the time here at Yahoo Answers.
Here are a couple of methods that will do what you need:
static void convert ( int n )
{
if ( n == 0 )
System.out.print ( "0" ) ;
else
{
String bits = "" ;
do
{
bits = ( char ) ( n % 2 + '0' ) + bits ;
n /= 2 ;
}
while ( n %26gt; 0 ) ;
System.out.print ( bits ) ;
}
}
static void convertRecursive ( int n )
{
if ( n == 0 )
System.out.print ( "0" ) ;
else
{
if ( n / 2 %26gt; 0 )
convertRecursive ( n / 2 ) ;
System.out.print ( ( char ) ( n % 2 + '0' ) ) ;
}
}
They are tested and work. The special cases for n == 0 are to prevent it from printing nothing when passed zero (or conversely, printing a leading '0' on every number but zero). Many simpler implementations suffer from this shortcoming.
EDIT: To have the extra output ("X in binary is Y"), the methods I wrote above would be called from another method, most likely the 'main' method of your overall program:
public class Convert
{
//... those two conversion methods here
public static void main ( String args [ ] )
{
int val = ... ; // read value to conver from user here
System.out.print ( val + " in binary (nonrecursive) is " ) ;
convert ( val ) ;
System.out.println ( ) ;
System.out.print ( val + " in binary (recursive) is " ) ;
convertRecursive ( val ) ;
System.out.println ( ) ;
}
}
Even better would be to have those methods I wrote return a string instead of printing it themselves. You should be able to see how to easily make that change. Then the printing part in main becomes just this (one such lien for each of the two conversion methods):
System.out.println ( val + " in binary is " + convert ( val ) ) ;
Regarding questions you may have about how some of the code I wrote works, I suggest trying to figure that out by finding the descriptions of the language constructs I used in any Java Language Reference Manual.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment