Weird Binary to Integer Problem

Thread Solved

Join Date: Apr 2007
Posts: 23
Reputation: rpk5000 is an unknown quantity at this point 
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Weird Binary to Integer Problem

 
0
  #1
Nov 17th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,341
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 250
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Weird Binary to Integer Problem

 
0
  #2
Nov 18th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 23
Reputation: rpk5000 is an unknown quantity at this point 
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Re: Weird Binary to Integer Problem

 
0
  #3
Nov 18th, 2007
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.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Weird Binary to Integer Problem

 
0
  #4
Nov 18th, 2007
Just convert the given boolean array to an integer array by looping through the contents and pass it to your existing function.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 23
Reputation: rpk5000 is an unknown quantity at this point 
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Re: Weird Binary to Integer Problem

 
0
  #5
Nov 18th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 30
Reputation: InfiNate is an unknown quantity at this point 
Solved Threads: 6
InfiNate InfiNate is offline Offline
Light Poster

Re: Weird Binary to Integer Problem

 
0
  #6
Nov 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 23
Reputation: rpk5000 is an unknown quantity at this point 
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Re: Weird Binary to Integer Problem

 
0
  #7
Nov 18th, 2007
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 30
Reputation: InfiNate is an unknown quantity at this point 
Solved Threads: 6
InfiNate InfiNate is offline Offline
Light Poster

Re: Weird Binary to Integer Problem

 
0
  #8
Nov 18th, 2007
boolean array = array of booleans.

in your op you said input was an array of booleans, now its not???
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 23
Reputation: rpk5000 is an unknown quantity at this point 
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Re: Weird Binary to Integer Problem

 
0
  #9
Nov 18th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,341
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 250
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Weird Binary to Integer Problem

 
0
  #10
Nov 18th, 2007
Originally Posted by rpk5000 View Post
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.

  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.
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
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC