Welcome, Guest. Please login or register.
Did you miss your activation email?
Your Ad Here
Pages: [1]   Go Down
  Print  
Author Topic: Recursive Java decimal to binary converter.  (Read 4954 times)
0 Members and 1 Guest are viewing this topic.
deAppel
Favorite member
Member
*

Reputation: 0
HappyFace plants one on you
Offline Offline
Posts: 45
Referrals: 0
Activity
0%

Awards
« on: December 05, 2007, 07:04:20 PM »

Code
GeSHi (java):
  1. package released;
  2.  
  3. /*--------------------------------------------------------------------------
  4. //Author List:
  5. //   deAppel <Creator>
  6. //
  7. //Description:
  8. // A recursive method to convert decimal to binary.
  9. // This is an assignment for the "Datastructuren en Alghoritmen" course @ Hogeschool Utrecht.
  10. // Note: NumberFormatException is not catched.
  11. //
  12. //Environment:
  13. //   This software was developed using Eclipse and Java 1.6
  14. //
  15. //Copyright Information:
  16. //   Copyright (C) 2007      <Institution><None>
  17. // Ark de Appel www.engineeringserver.com
  18. //
  19. //----------------------------------------------------------------------*/
  20.  
  21. import java.util.Scanner;
  22.  
  23. public class BinaryDemo {
  24. static boolean running = true;
  25. static int i;
  26. public static void main(String[] args) {
  27. System.out.println("Start BinaryDemo\n");
  28. BinaryDemo BD = new BinaryDemo();
  29. Scanner sc = new Scanner(System.in);
  30.  
  31. System.out.print("print getBinary().. up to: ");
  32. String strBinNumber = sc.nextLine();
  33. int maxBinNumber = Integer.parseInt(strBinNumber);
  34.  
  35. for (i = -1; i <=maxBinNumber; i++){
  36. System.out.println("getBinary(" + i + ")= \t " + BD.getBinary(i));
  37. }
  38. System.out.println("\nManual getBinary()\n");
  39.  
  40. while(running){
  41. System.out.print("Convert: ");
  42. String read = sc.nextLine();
  43. if(read.equals("quit")){
  44. running = false;
  45. }
  46. else{
  47. int binaryNumber = Integer.parseInt(read);
  48. System.out.println("getBinary(" +binaryNumber + ") = " +  BD.getBinary(binaryNumber));
  49. }
  50. }
  51. System.out.println("Quit");
  52. System.exit(0);
  53. }
  54.  
  55. public String getBinary(int n){
  56. String tmp = "";
  57. if (n < 0){
  58. tmp = "?";
  59. }
  60. else if (n > 0){
  61. tmp = getBinary(n/2) + n%2;
  62. }
  63. else{
  64. return "0";
  65. }
  66. return tmp;
  67. }
  68. }
Created by GeSHI 1.0.7.20
Logged

Earn $0.04 or more for each search you make! http://www.myhpf.co.uk/apply001.asp?Friend=77889

If you need homework help, just post your assignment and i'll code it before your deadline =) i'll provide you "t3h c0d3z" but only if you show me that you have done something and not just ask for me to write everything for you. Just tell us what the problem is and i'm sure we can fix it for you!

I was born into the Hakka lineage, A bloodline that
traces it's roots back to the original Han emperors of
China. During the rise of the 3 Kingdoms (China's
famous warring period), the Hans were overthrown
and exiled. Forced to flee, they headed south finding
refuge within the mountains of Guilin Province where
they lived under their new title, the Hakka and patiently
wait for their chance to return to the throne as the
Sons Of Heaven...
Javaforums.net/forum :: A community for software developers
« on: December 05, 2007, 07:04:20 PM »

 Logged
HappyFace
Javaforums.net admin
Senior Member
*

Reputation: 16
Developer @ Engineeringserver network
Offline Offline
Posts: 2552
Referrals: 12
Activity
14%

WWW Awards
« Reply #1 on: January 27, 2008, 10:25:11 AM »

Replace the getBinary() method with this to show the 0/1 i forgot to check in my previous post.

Code
GeSHi (java):
  1. public String getBinary(int n){
  2. String tmp = "";
  3. if (n < 0){
  4. tmp = "?";
  5. }
  6. else if (n == 0){
  7. tmp = "0";
  8. }
  9. else if (n > 1){
  10. tmp = getBinary(n/2) + n%2;
  11. }
  12. else if(n == 1){
  13. tmp = "1";
  14. }
  15. return tmp;
  16. }
Created by GeSHI 1.0.7.20



Start BinaryDemo

getBinary().. up to: 9
getBinary(-1) =       ?
getBinary(0) =       0
getBinary(1) =       1
getBinary(2) =       10
getBinary(3) =       11
getBinary(4) =       100
getBinary(5) =       101
getBinary(6) =       110
getBinary(7) =       111
getBinary(Cool =       1000
getBinary(9) =       1001
Logged

It doesn't matter how hard you've studied; the material won't be on the exam anyway.
Admin @ www.Engineeringserver.com General Admin @ www.Javaforums.net and it's subsites Forum admin @ www.Javaforums.net Global moderator @ www.Engineeringserver.com network Java / .NET blogger @ www.Engineeringserver.com/blog
XMA performer since 2006 @ http://www.engineeringserver.com/XMA-NaN/ Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://www.javaforums.net...lery.html;sa=media;id=170
http://img222.imageshack....707/arkietomatoesmall.jpg
http://img208.imageshack.us/img208/7141/smalli.png
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net/forum :: A community for software developers
   

 Logged
Pages: [1]   Go Up
  Print  
 
Jump to: