|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
|
#2
|
|||
|
|||
|
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... |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
|||
|
|||
|
Quote:
This is what i did ... and i manage to compile without errors but i am not getting any results. Can anyone shed some light? |
|
#5
|
||||
|
||||
|
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() |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
||||
|
||||
|
Does everything work now?
|
|
#8
|
|||
|
|||
|
Quote:
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. |
|
#9
|
||||
|
||||
|
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 =) |
|
#10
|
|||
|
|||
|
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.... |
|
#11
|
||||
|
||||
|
Good luck!
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Convert from binary to decimal? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|