954,554 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Weird Binary to Integer Problem

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?

rpk5000
Newbie Poster
24 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

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;
  }
rpk5000
Newbie Poster
24 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

Just convert the given boolean array to an integer array by looping through the contents and pass it to your existing function.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

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...

rpk5000
Newbie Poster
24 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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.

InfiNate
Light Poster
30 posts since Nov 2007
Reputation Points: 11
Solved Threads: 6
 

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?

rpk5000
Newbie Poster
24 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

boolean array = array of booleans.

in your op you said input was an array of booleans, now its not???

InfiNate
Light Poster
30 posts since Nov 2007
Reputation Points: 11
Solved Threads: 6
 

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/hsacaddept/technology/chanley/assigns/2007/Test%20Plan%20for%20Method%20Pract.pdf ) here, (problem n, seems to show the input as an int.

rpk5000
Newbie Poster
24 posts since Apr 2007
Reputation Points: 10
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.

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.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
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/hsacaddept/technology/chanley/assigns/2007/Test%20Plan%20for%20Method%20Pract.pdf ) here, (problem n, seems to show the input as an int.

He refered to it as a "binary aray" because that is essentially what a boolean array is. Binary is 1 or 0, boolean is true or false. So, in the sense of this homework assignment, that boolean arrayis a binary array. Each boolean array element is one bit in the binary sequence.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
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 supposed 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.

Almost word for word.... thats my issue, I'm confused by the wording of the assignment...

As to it looking like I wanted you to do it for me...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)

...

Writing a binary to decimal converter would be easy, I'm not sure what he means by the stuff in the second sentence though.

Next time, don't make poor assumptions and you would avoid looking like an idiot?
Don't take your anger out on me because you didn't read the question and are suddenly surprised when someone points that out.He referred to it as a "binary array" because that is essentially what a boolean array is. Binary is 1 or 0, boolean is true or false.

Yes I get that.

So, in the sense of this homework assignment, that boolean array is a binary array. Each boolean array element is one bit in the binary sequence.

Everyone keeps mentioning a boolean array, where the heck is it? Or, am I already done and the bit about boolean arrays in the problem wasn't necessary to solving it?

rpk5000
Newbie Poster
24 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

Almost word for word.... thats my issue, I'm confused by the wording of the assignment...

As to it looking like I wanted you to do it for me...

Next time, don't make poor assumptions and you would avoid looking like an idiot? Don't take your anger out on me because you didn't read the question and are suddenly surprised when someone points that out.

No where in that first post did you give any indication of having done any work. Made a lot of statements about "I thought" "I tried" but didn't show any of that effort, and it's easy to claim. So it was still nothing but a "do my work for me" post. And the only one looking like an idiot is you doing your "I didn't say that" backpedaling.Everyone keeps mentioning a boolean array, where the heck is it? Or, am I already done and the bit about boolean arrays in the problem wasn't necessary to solving it?

You mentioned the boolean array, here the quote from your first postThe binary number will be passed in as an array of Booleans (a true represents a 1 and a false represents a 0.).

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Perhaps you should clarify with your teacher since the only thing you are confused with is his wording.

FWIW my definition of a boolean array would be:
bool[] boolArray;
Which.. as I said before, is an array of booleans.

InfiNate
Light Poster
30 posts since Nov 2007
Reputation Points: 11
Solved Threads: 6
 

I did, I sent an email to him yesterday.

No where in that first post did you give any indication of having done any work. Made a lot of statements about "I thought" "I tried" but didn't show any of that effort, and it's easy to claim. So it was still nothing but a "do my work for me" post. And the only one looking like an idiot is you doing your "I didn't say that" backpedaling.

You can't edit your posts here (or at least not that I'm aware of) so I don't need to further point out whats wrong with your comments, I'll just let people review them and come up with their own opinion.You mentioned the boolean array, here the quote from your first post The binary number will be passed in as an array of Booleans (a true represents a 1 and a false represents a 0.).

That line is the problem (ie, what the assignment was). The whole conversation has been about what that line means. Since you missed that, I'd prefer that you would remain out of the thread, until you calm down or have something to contribute.

rpk5000
Newbie Poster
24 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

C'mon guys, cool down - let's just have a virtual handshake and a beer and start again, marking this one down to misunderstanding...

happygeek
Freelance Word Punk
Administrator
27,471 posts since Mar 2006
Reputation Points: 1,457
Solved Threads: 56
 

I can't have beer but my teacher finally replied and said that reading in as a string would be fine...

rpk5000
Newbie Poster
24 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

Whatever. Continue on in your world of percieved slights. I'm not the one here asking for help.

You know what they say about biting the hand that feeds you.

And, you can prefer anything you wish, not that you can make it come true.

Edit: But, it's fairly obvious that you read in the number anyway you want to, and create the "binary array" from the read string (I assume from that snippet that you read the "number" already in binary format) and pass that array to the method that is to do the conversion. It is kind of hard for your program to receive the input as an array already already (unless it is to read it over a socket using an ObjectIputStream or something, which is beyond the current class objectives, I would assume) so it should also be fairly obvious that you need to create the argument you pass to the array from whatever you read in, reagardless of how you read it in, so, I don't see what has been giving you a problem.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Heres what my problem was:
"The binary number will be passed in as an array of Booleans"

I couldn't understand why in the world you would pass in an array of booleans. I think about booleans as ... booleans... true/false values. So why would someone enter: TrueFalseTrueFalse... or something like that. In his email back, he told me that reading it in as an int or string was fine. The sentence also conflicted with the test data, which gave the binary number as a binary value (eg:. 1001, 11001). When he said array of booleans, he really meant an array of any valid type that could store 1-0 values.

rpk5000
Newbie Poster
24 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

Okay, I will try to say this nicely.

Given that the data to be read is 1001,11001, etc and given the text The binary number will be passed in as an array of Booleans (a true represents a 1 and a false represents a 0) it seems faairly obvious that you need to create the binary array yourself. It's a stupid requirement, yes, but that's the way the world is. I've seen requirements much worse in real projects.

Now, maybe it's just me, but that all seems to be glaringly obvious, so your first post said to me (reading between the lines), "write (at least this part of) the program for me". Which was the reason for my first post. I apologize if that was not the case, but that is the way I interpreted the first post (and we get that here so often it has become the norm, rather than the exception).

But, in any case, read in the String, create a boolean array of the same length as the String, then loop over the String setting each element in the array according to the requirements provided in the assignment text. (Really, you only need to worry about the ones, as a boolean array is initiated with all elements false, which is how 0 is to be represented.)

Edit: And a boolean is, yes, a boolean. But, what is a boolean? It is a switch, an "either/or", in other words, which is also exactly what a bit is. It uses 1/0 as the textual representation, whereas a boolean uses true/false, but they mean the same thing, on/off or used/free. That is another way of seeing a boolean, and what, I believe, is meant here.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You