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?

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.

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;
  }

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

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

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.

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?

boolean array = array of booleans.

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

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.

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.

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 array is a binary array. Each boolean array element is one bit in the binary sequence.

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?

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 post The binary number will be passed in as an array of Booleans (a true represents a 1 and a false represents a 0.).

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.

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.

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

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

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.

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.

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.

Let me start by saying that the code I have given here (post 3 i think) is an acceptable solution to the problem.

Yes it was obvious I needed to take into the program a number and convert it into an array.
I didn't want any help writing any of the code at all, when people give code the person seeking help doesn't learn anything. I'd much rather write it myself and go through the sun documentation.

What I didn't get was why you would want to convert the input to a boolean array ever. Mathematically it seems much easier to do it the way i did, with an array of integers. When I emailed him he said that I could do it that way.

My only issue was why would you want to do the extra work of converting it into booleans and back. I have problems doing things in a stupid way, earlier in this same assignment he had us create a new instance just to store the data.

class MusicDisc {
public String title, artist;
public int year;
};

I asked him why and he said that you couldn't return more than one piece of data at a time, my response was to use toString and put it all in there instead of making a new instance. (I like efficiency over OOP)

To tell you the truth, that may seem efficient, however, once returned, the rest of th program must then "parse" that String to extract the info (it won't necessarilly only be used for output), whereas that object will allow the rest of the program to directly access the data without having to parse it first.

Oh, sorry, I meant efficient as I have other stuff to do and I need to get this assignment done first :). (I do understand why its important to do it the other way though)

This question has already been answered. Start a new discussion instead.