Java Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingJava Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old January 22nd, 2005, 10:46 PM
miss_mushroom miss_mushroom is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 7 miss_mushroom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 38 m 27 sec
Reputation Power: 0
Cool Convert from binary to decimal?

Hi all.... I am new in Java Programming and need your help for the following :

Convert binary numbers to decimal.
The program should prompt user to enter the number. Upon clicking the button, the program should display the number, the positional wightage of each digit of the number ( I have done some researched n found that i have to use pow method for this....is this correct??? ) .........and the decimal equivalent of the number.


Please help. I really appreciate it.

Thanks.

Reply With Quote
  #2  
Old January 24th, 2005, 10:16 PM
Programmer1199 Programmer1199 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 4 Programmer1199 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 33 m 17 sec
Reputation Power: 0
EXAMPLE:

(11010.01) to Decimal -

take each number invdividually and multiply it by 2 to the power of the placement of the number - 1. (5 places to the left of the decimal would be 1 * 2/\4)

so you start with the number far left and move right like this :

(1 * 2/\4) + (1 * 2/\3) + (0 * 2/\2) + (1 * 2/\1) + (0 * 2/\0) + (0 * 2/\-1) + (1 * 2/\-2) = 26.25

so thats how you would go from binary to decimal...so you will need to write a program that will use that basic formula.

hope that makes some sense...

Reply With Quote
  #3  
Old January 25th, 2005, 08:48 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 10
Code:
public class Class1 
{
  public static void main(String[] args) 
  {
    System.out.println(Integer.parseInt("110101",2));
  }
}


This outputs 53

Refer to the documentation

In the same class you could use toBinaryString(int i) to convert binary into decimal.

Reply With Quote
  #4  
Old January 26th, 2005, 04:24 AM
miss_mushroom miss_mushroom is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 7 miss_mushroom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 38 m 27 sec
Reputation Power: 0
Post Look

Quote:
Originally Posted by MadCowDzz
Code:
public class Class1 
   {
     public static void main(String[] args) 
     {
       System.out.println(Integer.parseInt("110101",2));
     }
   }


This outputs 53

Refer to the documentation

In the same class you could use toBinaryString(int i) to convert binary into decimal.


This is what i did ... and i manage to compile without errors but i am not getting any results. Can anyone shed some light?

Reply With Quote
  #5  
Old January 26th, 2005, 09:53 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 10
when you say you get no results, what happens?

Are you calling the Main method from the commandline, or are you pressing the button visually from a browser?

The reason I ask is that I noticed the main method has no output at all...
It simply does the calculation...
If you are calling the main method from the commandline, perhaps a System.out.print statement would help?

I don't remember too much of Applets anymore, but do browsers ever call the main method?
I thought they called init(), start(), and paint()

Reply With Quote
  #6  
Old January 27th, 2005, 12:55 AM
miss_mushroom miss_mushroom is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 7 miss_mushroom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 38 m 27 sec
Reputation Power: 0
when you say you get no results, what happens?

>>>well i don't get the answer for the conversion. if i key in the binary number in the textfield for example 10110 and press the convert button it shows nothing. Just blank.

Are you calling the Main method from the commandline, or are you pressing the button visually from a browser?

>>>i've done some homework and just realised that i can't use main method for applet so i changed it to private void compute.

The reason I ask is that I noticed the main method has no output at all...
It simply does the calculation...
If you are calling the main method from the commandline, perhaps a System.out.print statement would help?
>>>i used the system.out.println method at first but didn't get any results so i switched to paint method.


I don't remember too much of Applets anymore, but do browsers ever call the main method?
I thought they called init(), start(), and paint() >>>nope it doesn't. I just found out haha

Reply With Quote
  #7  
Old January 27th, 2005, 07:33 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 10
Does everything work now?

Reply With Quote
  #8  
Old January 28th, 2005, 09:32 AM
miss_mushroom miss_mushroom is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 7 miss_mushroom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 38 m 27 sec
Reputation Power: 0
Quote:
Originally Posted by MadCowDzz
Does everything work now?


Nope. I'm changing some parts of my codes (especially the ones that doesn't make any sense to me) as i want to complete my assignment with full understanding.

I have a couple of questions though that I hope u can clarify for me.

1) Is the system.out.println method only to be used with the public static void main (String[] args)? Can i use it like for example public void compute?

2) There's a statement i read up from java sun that i don't really understand. If i write down like for example parseInt( " ",2) an exception of type NumberFormatException is thrown.

3) And is the parseInt method only applicable for values known like let's say if i have a known int 10001 and i would like to convert it to decimal. But if i don't know what the int is(as the user will be keying in the digits) can I still use it?

4) I have been told that i need a separate variable to keep track of the value's calculated so far, for each value calculated in my code, add it to this variable. Do you think this is necessary or will i do fine without it?

5) I have also been told to ditch the paint method that i used (as u can see in my code) and to create a Label for my answer (actualAnswer) and set the Text. I did it however i am not sure whether i am doing it right. Please correct me if I'm wrong. This is what i did:
public void actionPerformed (ActionEvent event){
answerLabel.setText("actualAnswer");

it looks kinda strange to me and after compiling... well it still doesn't show
any output (actualAnswer) so i guess
I'm doing something wrong here.

6) Is there a difference between :

private static void doPower
public static void power(int num, int power)
public static int pow(int num, int power)
public static double double pow (double num, doube power)

Since my num and power is int i am using public static int power but i am just curious why there are different representation for the method pow.


Lastly, i hope you don't mind me asking a lot of questions, I am in no hurry of finishing this assignment. Its due in one months time. I finished doing the draft its making all the parts work thats challenging.

Take your time. I really appreciate it.
Thank you.

Reply With Quote
  #9  
Old January 31st, 2005, 08:55 AM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 10
1) Is the system.out.println method only to be used with the public static void main (String[] args)? Can i use it like for example public void compute?

If it is an applet you are creating, your System.out.println() statements won't get output properly.



2) There's a statement i read up from java sun that i don't really understand. If i write down like for example parseInt( " ",2) an exception of type NumberFormatException is thrown.

The exception is because String.parseInt() is expecting to find a number in those quotes. If it doesn't, it gets confused and throws an exception. You could prepare for those errors by using a try/catch... or using some type of trimming function on the String [to remove white space].



3) And is the parseInt method only applicable for values known like let's say if i have a known int 10001 and i would like to convert it to decimal. But if i don't know what the int is(as the user will be keying in the digits) can I still use it?


parseInt("10001",2) should return the decimal value of that string, 17... before you run the parseInt(), you might want to make sure that the user only input a 1 or 0... i'll leave it up to you to figure that one out =) [also, this might help you on the above question too]




4) I have been told that i need a separate variable to keep track of the value's calculated so far, for each value calculated in my code, add it to this variable. Do you think this is necessary or will i do fine without it?

I don't understand the idea of seperate variable... perhaps you could expand on that.



5) I have also been told to ditch the paint method that i used (as u can see in my code) and to create a Label for my answer (actualAnswer) and set the Text. I did it however i am not sure whether i am doing it right. Please correct me if I'm wrong. This is what i did:
public void actionPerformed (ActionEvent event){
answerLabel.setText("actualAnswer");

it looks kinda strange to me and after compiling... well it still doesn't show
any output (actualAnswer) so i guess
I'm doing something wrong here.



how have you declared the label? You have to add the label to a container. if this is a school assignment, it is best to find out what the professor wants/expects.




6) Is there a difference between :

private static void doPower
public static void power(int num, int power)
public static int pow(int num, int power)
public static double double pow (double num, doube power)

Since my num and power is int i am using public static int power but i am just curious why there are different representation for the method pow.


where'd you see that code? I agree with you, that power values are likely always going to be int.



Lastly, i hope you don't mind me asking a lot of questions, I am in no hurry of finishing this assignment. Its due in one months time. I finished doing the draft its making all the parts work thats challenging.

Good work, starting off with proper preparation work is the key to easier coding. The better you have it laid out in the start, the easier things will be later on. Don't forget to ask questions =)

Reply With Quote
  #10  
Old January 31st, 2005, 09:33 AM
miss_mushroom miss_mushroom is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 7 miss_mushroom User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 38 m 27 sec
Reputation Power: 0
Wow that was a fast reply

Thanks a bunch and don't worry i'll keep on posting when i have some doubts.

For now, adios....

Reply With Quote
  #11  
Old January 31st, 2005, 12:17 PM
MadCowDzz's Avatar
MadCowDzz MadCowDzz is offline
I'm Internet Famous
Dev Articles Frequenter (2500 - 2999 posts)
 
Join Date: Jan 2003
Location: Toronto, Canada
Posts: 2,890 MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level)MadCowDzz User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 1 Week 16 h 19 m 35 sec
Reputation Power: 10
Good luck!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingJava Development > Convert from binary to decimal?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.


© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek