Jump to content

Welcome to Geeks to Go - Register now for FREE

Geeks To Go is a helpful hub, where thousands of volunteer geeks quickly serve friendly answers and support. Check out the forums and get free advice from the experts. Register now to gain access to all of our features, it's FREE and only takes one minute. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more.

Create Account How it Works
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