User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Apr 2007
Location: US
Posts: 21
Reputation: rpk5000 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Question Weird Binary to Integer Problem

  #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?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,508
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: Weird Binary to Integer Problem

  #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  
Join Date: Apr 2007
Location: US
Posts: 21
Reputation: rpk5000 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Re: Weird Binary to Integer Problem

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

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;
  }
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 7,009
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 25
Solved Threads: 368
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Weird Binary to Integer Problem

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

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Apr 2007
Location: US
Posts: 21
Reputation: rpk5000 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Re: Weird Binary to Integer Problem

  #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  
Join Date: Nov 2007
Posts: 30
Reputation: InfiNate is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
InfiNate InfiNate is offline Offline
Light Poster

Re: Weird Binary to Integer Problem

  #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  
Join Date: Apr 2007
Location: US
Posts: 21
Reputation: rpk5000 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Re: Weird Binary to Integer Problem

  #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  
Join Date: Nov 2007
Posts: 30
Reputation: InfiNate is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
InfiNate InfiNate is offline Offline
Light Poster

Re: Weird Binary to Integer Problem

  #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  
Join Date: Apr 2007
Location: US
Posts: 21
Reputation: rpk5000 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rpk5000 rpk5000 is offline Offline
Newbie Poster

Re: Weird Binary to Integer Problem

  #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  
Join Date: Feb 2006
Posts: 1,508
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: Weird Binary to Integer Problem

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

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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 9:01 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC