Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Java problem!


  • Please log in to reply

#1
Pinky_Girl

Pinky_Girl

    New Member

  • Member
  • Pip
  • 2 posts
Hi guys,

Here is the method that I wrote to convert numbers from binary to decimal.

public static void binary2decimal(int a){
String r=a+"";
double sum=0;
for(int i=0;i<r.length();i++){
if(r.charAt(i)=='1'){
double product=1;
for(int q=0;q<r.length()-i-1;q++){
product=product*2;}
sum=sum+product;}
else if(r.charAt(i)=='.'){
int pos=i+1;
while(r.charAt(pos)=='1'){
double prod=1;
for(int q=0;q<r.length()-pos-1;q++){
prod=prod*1/2;
pos++;
if(pos==r.length()-1){
break;}}
sum=sum+prod;}}}
System.out.println(sum);
}

//--------------------------------------------------------------

How can I write it recursively????
  • 0

Advertisements


#2
destin

destin

    Member

  • Member
  • PipPip
  • 53 posts
Okay, first explain how to do the conversion in words (not that I don't know, this will just help the recursive thinking).
  • 0

#3
gust0208

gust0208

    Member

  • Member
  • PipPipPip
  • 311 posts

Okay, first explain how to do the conversion in words (not that I don't know, this will just help the recursive thinking).

This is good advice to always write some pseudocode first to get the basic algorithm on paper. Check out this wikipedia page for the example of converting binary to decimal and it is a near-recursive format already. Here is the link:
http://en.wikipedia....nary_Simplified

Once you have some psuedo code down, I have pasted a working recursive version of this program and function, take a look for one example on a way to solve this problem. By no means the optimal version, but tested and works. Also attached as a zip file.

import javax.swing.*;
import java.util.*;
import java.io.*;
import java.lang.Math;

public class binary_conv {
public static void main(String[] args) {
int binary = 101001;
System.out.println("binary = " + binary);
int decimal = binary2decimal(binary);
System.out.println("decimal = " + decimal);
}

public static int binary2decimal(int a){
String binary_str = a + "";
int current_power = binary_str.length() - 1;
if (current_power == 0) {
// Hit end of recursive chain, return the val of a * 2^0 == a
return a;
}
//current_val holds the calculation 2^current_power * (first binary digit)
int current_val = (int)Math.pow(2,current_power) * Integer.parseInt(binary_str.substring(0,1));
return current_val + binary2decimal(Integer.parseInt(binary_str.substring(1,binary_str.length())));
}
}

Attached Files


  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP