•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 455,964 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,609 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2321 | Replies: 22 | Solved
![]() |
•
•
Join Date: Apr 2007
Location: US
Posts: 21
Reputation:
Rep Power: 2
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
Location: US
Posts: 21
Reputation:
Rep Power: 2
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.
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
Location: US
Posts: 21
Reputation:
Rep Power: 2
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
Location: US
Posts: 21
Reputation:
Rep Power: 2
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.
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; }
And no this is not just a rude comment. You posted your homework assignment, almost word for word it looks like, without giving any indication that you had done anything at all. What is anyone suppossed to infer from that other than "Do my homework for me"? Next time, post the work you have already done when you open the thread and you can avoid the accusations.
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Weird ASP/SQL Problem (ASP)
- binary to decimal problem (C++)
- binary integer logic (Software Developers' 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



Linear Mode