943,972 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 5255
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 17th, 2007
0

Weird Binary to Integer Problem

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rpk5000 is offline Offline
23 posts
since Apr 2007
Nov 18th, 2007
0

Re: Weird Binary to Integer Problem

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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Nov 18th, 2007
0

Re: Weird Binary to Integer Problem

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)
  1. Tester
  2. case 3:
  3. System.out.println("Please Input a binary number");
  4. String ys;
  5. ys=sc.next();
  6. int [] binary = new int [ys.length()];
  7. for (int i = 0; i < binary.length; i++) {
  8. binary[i] = (int)ys.charAt(i)-(int)'0';
  9. }
  10. System.out.println("The number in base 10 is " + (mp.binToInt(binary)));
  11. break;
  12.  
  13.  
  14.  
  15. Method
  16. //pre: Given a binary number
  17. //post Returns the decimal equivelent of the number as an integer
  18. public Integer binToInt(int[] binary) {
  19. int decimal = 0;
  20. for (int i = binary.length - 1; i > -1; i--) {
  21. decimal += Math.pow(2, (binary.length - i - 1)) * binary[i];
  22. }
  23. return decimal;
  24. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rpk5000 is offline Offline
23 posts
since Apr 2007
Nov 18th, 2007
0

Re: Weird Binary to Integer Problem

Just convert the given boolean array to an integer array by looping through the contents and pass it to your existing function.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Nov 18th, 2007
0

Re: Weird Binary to Integer Problem

The tester shown is what I wrote... and the test data he provided gives the numbers as 1100, just as binary numbers, not as true false conditions...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rpk5000 is offline Offline
23 posts
since Apr 2007
Nov 18th, 2007
0

Re: Weird Binary to Integer Problem

Did you not read what s.o.s wrote. Write a function to convert your boolean array (true/false array) into a binary array. Its a really simple task.

Then use your existing tester to take the binary array and convert to a decimal.
Reputation Points: 11
Solved Threads: 6
Light Poster
InfiNate is offline Offline
30 posts
since Nov 2007
Nov 18th, 2007
0

Re: Weird Binary to Integer Problem

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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rpk5000 is offline Offline
23 posts
since Apr 2007
Nov 18th, 2007
0

Re: Weird Binary to Integer Problem

boolean array = array of booleans.

in your op you said input was an array of booleans, now its not???
Reputation Points: 11
Solved Threads: 6
Light Poster
InfiNate is offline Offline
30 posts
since Nov 2007
Nov 18th, 2007
0

Re: Weird Binary to Integer Problem

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rpk5000 is offline Offline
23 posts
since Apr 2007
Nov 18th, 2007
0

Re: Weird Binary to Integer Problem

Click to Expand / Collapse  Quote originally posted by rpk5000 ...
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)
  1. Tester
  2. case 3:
  3. System.out.println("Please Input a binary number");
  4. String ys;
  5. ys=sc.next();
  6. int [] binary = new int [ys.length()];
  7. for (int i = 0; i < binary.length; i++) {
  8. binary[i] = (int)ys.charAt(i)-(int)'0';
  9. }
  10. System.out.println("The number in base 10 is " + (mp.binToInt(binary)));
  11. break;
  12.  
  13.  
  14.  
  15. Method
  16. //pre: Given a binary number
  17. //post Returns the decimal equivelent of the number as an integer
  18. public Integer binToInt(int[] binary) {
  19. int decimal = 0;
  20. for (int i = binary.length - 1; i > -1; i--) {
  21. decimal += Math.pow(2, (binary.length - i - 1)) * binary[i];
  22. }
  23. return decimal;
  24. }
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.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: C programmers guide to Java
Next Thread in Java Forum Timeline: StringUtil





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC