| | |
Weird Binary to Integer Problem
Thread Solved |
•
•
Join Date: Apr 2007
Posts: 23
Reputation:
Solved Threads: 0
n. Write a method that will convert a number from binary to decimal. The binary number will
be passed in as an array of Booleans (a true represents a 1 and a false represents a 0.)
This is part of a methods practice assignment I was given. I'm currently taking AP Comp Sci.
I'm not sure what my teacher wants me to do. (I don't think I'll have a problem actually doing it once I know what he wants)
//Extra stuff if you care
Basically the setup of the the assignment is:
MethodPracticeTest -> MethodPractice
(Classes)
Where MethodPracticeTest is a menu and a switch case. There I read in the data, pass it to MethodPractice, and use a return to give back the answer.
Writing a binary to decimal converter would be easy, I'm not sure what he means by the stuff in the second sentence though.
Any ideas?
be passed in as an array of Booleans (a true represents a 1 and a false represents a 0.)
This is part of a methods practice assignment I was given. I'm currently taking AP Comp Sci.
I'm not sure what my teacher wants me to do. (I don't think I'll have a problem actually doing it once I know what he wants)
//Extra stuff if you care
Basically the setup of the the assignment is:
MethodPracticeTest -> MethodPractice
(Classes)
Where MethodPracticeTest is a menu and a switch case. There I read in the data, pass it to MethodPractice, and use a return to give back the answer.
Writing a binary to decimal converter would be easy, I'm not sure what he means by the stuff in the second sentence though.
Any ideas?
So, wht do you have so far? We are not going to do your homework for you, that is not only morally wrong, it is against the Daniweb terms and policies, which you agreed to when creating an account here.
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
•
•
Join Date: Apr 2007
Posts: 23
Reputation:
Solved Threads: 0
See that's just a rude comment. I of course actually read the policies, and am well aware that it would not make sense to just give me the answer. Maybe you should take time to consider that not all people on the site are as ignorant of the rules as some. Notice the part where I'm taking AP Comp Sci? Anyone in even the basic Java course could make a good stab at this problem. What I wanted was help understanding what he wanted me to do and what he was talking about in the second sentence.
Here's code to do it using an array of integers.
Here's code to do it using an array of integers.
Java Syntax (Toggle Plain Text)
Tester case 3: System.out.println("Please Input a binary number"); String ys; ys=sc.next(); int [] binary = new int [ys.length()]; for (int i = 0; i < binary.length; i++) { binary[i] = (int)ys.charAt(i)-(int)'0'; } System.out.println("The number in base 10 is " + (mp.binToInt(binary))); break; Method //pre: Given a binary number //post Returns the decimal equivelent of the number as an integer public Integer binToInt(int[] binary) { int decimal = 0; for (int i = binary.length - 1; i > -1; i--) { decimal += Math.pow(2, (binary.length - i - 1)) * binary[i]; } return decimal; }
•
•
Join Date: Apr 2007
Posts: 23
Reputation:
Solved Threads: 0
When you say binary array, I'm assuming you mean my array of integers. Also, where the heck is my boolean array? Did you not read what I wrote? The input is ones and zeros not true/false values, so why am i bothering with a boolean array. If the number is given as an int, why convert it to booleans just to convert it back?
•
•
Join Date: Apr 2007
Posts: 23
Reputation:
Solved Threads: 0
Yes I do know that a boolean array is an array of booleans, but when you said binary array, I wasn't sure if you meant the array of integers, as I've never actually heard of a "binary" array. In the op the text I copy and pasted seems to refer to an array of booleans, while the test data hes provided (http://www.shenet.org/high/hsacaddep...od%20Pract.pdf) here, (problem n, seems to show the input as an int.
•
•
•
•
See that's just a rude comment. I of course actually read the policies, and am well aware that it would not make sense to just give me the answer. Maybe you should take time to consider that not all people on the site are as ignorant of the rules as some. Notice the part where I'm taking AP Comp Sci? Anyone in even the basic Java course could make a good stab at this problem. What I wanted was help understanding what he wanted me to do and what he was talking about in the second sentence.
Here's code to do it using an array of integers.
Java Syntax (Toggle Plain Text)
Tester case 3: System.out.println("Please Input a binary number"); String ys; ys=sc.next(); int [] binary = new int [ys.length()]; for (int i = 0; i < binary.length; i++) { binary[i] = (int)ys.charAt(i)-(int)'0'; } System.out.println("The number in base 10 is " + (mp.binToInt(binary))); break; Method //pre: Given a binary number //post Returns the decimal equivelent of the number as an integer public Integer binToInt(int[] binary) { int decimal = 0; for (int i = binary.length - 1; i > -1; i--) { decimal += Math.pow(2, (binary.length - i - 1)) * binary[i]; } return decimal; }
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- Weird ASP/SQL Problem (ASP)
- binary to decimal problem (C++)
- binary integer logic (IT Professionals' Lounge)
- Exercise using: unsigned int datecode(int year, int month, int day); (C++)
- not-a-virusadware (Viruses, Spyware and other Nasties)
- Reversing Integer, Magic Squares, and LCM problems (Java)
- Weird IE6 printing problem... (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: C programmers guide to Java
- Next Thread: StringUtil
| Thread Tools | Search this Thread |
actuate add android api applet application applications array arrays automation balls bank binary bluetooth business chat class clear client code codesnippet collections component database defaultmethod development dice digit dragging ebook eclipse equation error event formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite int integer invokingapacheantprogrammatically j2me java javame javaprojects jni jpanel julia linux list main map method methods mobile myregfun mysql netbeans nonstatic openjavafx parameter pearl php problem program programming project recursion repositories scanner scrollbar server set sms sort sorting spamblocker sql sqlserver state storm string sun superclass swing swt thread threads tree windows






