Welcome to Dream.In.Code
Become a Java Expert!

Join 149,602 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,910 people online right now. Registration is fast and FREE... Join Now!




Decimal to Binary

 
Reply to this topicStart new topic

Decimal to Binary, Homework help

BBjr
11 Oct, 2007 - 09:37 AM
Post #1

New D.I.C Head
*

Joined: 25 Sep, 2007
Posts: 8


My Contributions
Here is what I have to do:
Problem
Given an integer number N (the number can be initialized in your program), write a JAVA
program that prints the binary representation of N.
Hint: The representation of integer numbers in binary (base 2) was explained and exemplified in
the lecture on September 5. For instance,
if N=264 you must print 0100001000;
if N=-264 you must print 1011111000;
if N=0 you must print 0.
Please remember that the most significant (leftmost) bit in the representation is the ”sign”
bit: 0 for positive numbers, 1 for negative numbers.

I have found 2 codes that will do this i think,

CODE
class binary{

public static void main (String[] args) {

System.out.print("Binary: ");

System.out.println(Integer.toBinaryString(-14));

}

}


CODE
class binarys{

public static void main (String[] args) {

int v = 8;


System.out.println( Integer.toString( v, 2 ));
System.out.println( Integer.toString( -v, 2 ));
}

}


Here is my problem.. niether one of these codes do what I need.. I need to actually show the number being converted in the code.

example:
264 : 2 = quotient 132 remainder 0
and so on you know divide the quotient till you have zero and use the remainders as binary representations.
Basically i need to show how it's done.. Not just do it.

If anybody can point me in the right direction on how to accomplish this I would very much appreciate it.

Thank you

User is offlineProfile CardPM
+Quote Post

Programmist
RE: Decimal To Binary
11 Oct, 2007 - 10:34 AM
Post #2

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,250



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
Converting Decimal to Binary
User is offlineProfile CardPM
+Quote Post

BBjr
RE: Decimal To Binary
11 Oct, 2007 - 11:31 AM
Post #3

New D.I.C Head
*

Joined: 25 Sep, 2007
Posts: 8


My Contributions
Thank you for your help.. here is where im going with this now.. do I need to turn around and do something else, or am i going the right way. And yes Ive been reading java tutorials and looking at Google for hours.
Thank you for any input you guys/gals leave.

CODE

class binaryy
{
public static void main (String [] args);
  {
int decimal, binary;

  decimal=56;
  binary=decimal % 2;
  do
  {

    binary=decimal % 2;
    decimal=decimal / 2;
     } while (decimal>0);
   System.out.println("The binary representation of (decimal); is  

  (binary)");
    }  
  }


I got one error when trying to compile:
missing method body, or declare abstract
public static void main (String [] args); // the pointer was below the m in main.

This post has been edited by BBjr: 11 Oct, 2007 - 11:33 AM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Decimal To Binary
11 Oct, 2007 - 12:14 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,306



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

My Contributions
Remove the semi-colon from the end of your main declaration.
CODE

public static void main (String [] args)

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Decimal To Binary
11 Oct, 2007 - 12:21 PM
Post #5

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Another thing, use an array to store the values for the binary number, and the printing line is not correct.
User is offlineProfile CardPM
+Quote Post

BBjr
RE: Decimal To Binary
11 Oct, 2007 - 01:46 PM
Post #6

New D.I.C Head
*

Joined: 25 Sep, 2007
Posts: 8


My Contributions
Thank you very much I will do that.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:47PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month