Difference between revisions of "Binary Nums"
ElsanussiM (Talk | contribs) (deletion of Hand-in section) |
ElsanussiM (Talk | contribs) (Added first part of solution (pseudocode and Java class shell)) |
||
Line 85: | Line 85: | ||
|Solution= | |Solution= | ||
+ | |||
+ | ==Determining how to solve the problem== | ||
+ | The greatest ways to start a problem is to divide it into smaller problems | ||
+ | and understand which parts you need to focus on. | ||
+ | |||
+ | It is clear that the main problem to solve is converting binary numbers to | ||
+ | decimal numbers. However, in order to make a program that does this, you | ||
+ | will need to use the Scanner class, loops, and System.out . You know this from | ||
+ | reading the program specification. Let's start by trying to figure out how | ||
+ | to convert from binary numbers to decimal numbers. | ||
+ | |||
+ | ===Converting from Binary Numbers to Decimal Numbers=== | ||
+ | |||
+ | So you don't know how to convert binary numbers to decimal | ||
+ | numbers. That's great. We can figure it out. | ||
+ | |||
+ | To solve the problem, we will initially forget about | ||
+ | writing good java code, and instead use anything simpler. | ||
+ | We can draw diagrams, write equations, and write pseudocode. We can | ||
+ | always write java code after we understand how to solve the problem. | ||
+ | |||
+ | The first thing to do is gather information from the problem specification. | ||
+ | In it we were told: | ||
+ | |||
+ | |||
+ | The value of the binary number 1011001 is | ||
+ | |||
+ | '''1''' * 2^6 + '''0''' * 2^5 + '''1''' * 2^4 + '''1''' * 2^3 + '''0''' * 2^2 + '''0''' * 2^1 + '''1''' * 2^0 = 89 | ||
+ | |||
+ | If we look at this carefully, we see that the right most digit of the binary number is multiplied by 2^0. The second left most | ||
+ | digit is multiplied by 2^1. The digit to the right of that is multiplied by 2^2. Then all the products are summed up. | ||
+ | |||
+ | However, we need to make sure we are correct. Let's check that we understand by converting some of the following numbers from binary to decimal. | ||
+ | |||
+ | <ul> | ||
+ | <li>0</li> | ||
+ | <li>1</li> | ||
+ | <li>1</li> | ||
+ | <li>10</li> | ||
+ | <li>11</li> | ||
+ | <li>101</li> | ||
+ | <li>1000</li> | ||
+ | <li>1101</li> | ||
+ | <li>10101</li> | ||
+ | </ul> | ||
+ | |||
+ | Now, try to write a set of instructions that anyone could follow to convert from binary to decimal. These instructions will be | ||
+ | your pseudocode. | ||
+ | |||
+ | <pre> | ||
+ | 1. take the rightmost digit, | ||
+ | if it is a 1, multiply it by 2 to the power of the current exponent and add it to the sum. | ||
+ | 2. do the same with digits to the left of the right most digit. | ||
+ | </pre> | ||
+ | |||
+ | A little bit more thinking shows we need to tell what sum, and what exponent to start at. Anything multiplied | ||
+ | by zero is zero, so we can simplify the pseudocode slightly. Let's also add variables for clarity. | ||
+ | |||
+ | <pre> | ||
+ | exponent = 0 | ||
+ | sum = 0 | ||
+ | |||
+ | take the rightmost digit ''d'', | ||
+ | sum = sum + ''d'' * 2^exponent | ||
+ | do the same with digits to the left of the rightmost digit. | ||
+ | </pre> | ||
+ | |||
+ | You can continue improving the pseudocode slightly until you have replaced much of the English instructions with Java-like code. | ||
+ | Here is an example of what you might get. | ||
+ | |||
+ | |||
+ | <pre> | ||
+ | exponent = 0 | ||
+ | sum = 0 | ||
+ | |||
+ | while (binaryNum!= 0) | ||
+ | { | ||
+ | rightmost = binaryNum % 10 //right most digit | ||
+ | sum = sum + ''d'' * 2^exponent | ||
+ | binaryNum = binaryNum / 10; //do the same with the digits to the left of the rightmost digit | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | It is good to make sure you understand what the above code is doing the percent sign is modulus, which gives the remainder. You can verify that the remainder of any number divided by 10 is the right most digit. Also don't forget that the forward slash represents integer division here. | ||
+ | |||
+ | Now that you have some pseudocode, we want to make sure that it is sound, one good way is to turn it into Java code and run it through the compiler. | ||
+ | |||
+ | ==Creating the Structure of the Program== | ||
+ | |||
+ | Now create a Java | ||
+ | file with the general structure of your program. | ||
+ | |||
+ | Define the class in a java file. | ||
+ | Name the file the same name as the class name plus the ".java" | ||
+ | extension. | ||
+ | Define the main method. | ||
+ | |||
+ | The following code shows the result of these steps. | ||
+ | <pre> | ||
+ | public class BinaryNum | ||
+ | { | ||
+ | public static void main(String[] args) | ||
+ | { | ||
+ | } | ||
+ | } | ||
+ | </pre> | ||
+ | |||
+ | To be continued... | ||
Revision as of 11:46, 27 March 2011
Back to the Case Studies homepage
ProblemAs you may know, computers work on the binary system. In this system, numbers are written using only 0s and 1s. In this question, you’ll write a program to convert binary numbers to normal (decimal) numbers. How are all numbers represented in 0s and 1s?
Here are the first few numbers written in both decimal (normal) notation and binary:
Suppose we have a binary number (a sequence of zeroes and ones) and we want to convert it to a decimal number. To do this, we multiply the digit (0 or 1) in each position of the number by an increasing power of two the further we move left. So the value of the binary number 1011001 is 1 * 2^6 + 0 * 2^5 + 1 * 2^4 + 1 * 2^3 + 0 * 2^2 + 0 * 2^1 + 1 * 2^0 = 89
Program Specifications
OutputUse System.out for output. Follow the format of the output shown above. You may notice that your program fails for large numbers. That's because of integer overflow. If you would like to get your program to work for larger binary numbers, replace int with long in your program (including using the Scanner method nextLong()). |
| |||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
SolutionDetermining how to solve the problemThe greatest ways to start a problem is to divide it into smaller problems and understand which parts you need to focus on. It is clear that the main problem to solve is converting binary numbers to decimal numbers. However, in order to make a program that does this, you will need to use the Scanner class, loops, and System.out . You know this from reading the program specification. Let's start by trying to figure out how to convert from binary numbers to decimal numbers. Converting from Binary Numbers to Decimal NumbersSo you don't know how to convert binary numbers to decimal numbers. That's great. We can figure it out. To solve the problem, we will initially forget about writing good java code, and instead use anything simpler. We can draw diagrams, write equations, and write pseudocode. We can always write java code after we understand how to solve the problem. The first thing to do is gather information from the problem specification. In it we were told:
1 * 2^6 + 0 * 2^5 + 1 * 2^4 + 1 * 2^3 + 0 * 2^2 + 0 * 2^1 + 1 * 2^0 = 89 If we look at this carefully, we see that the right most digit of the binary number is multiplied by 2^0. The second left most digit is multiplied by 2^1. The digit to the right of that is multiplied by 2^2. Then all the products are summed up. However, we need to make sure we are correct. Let's check that we understand by converting some of the following numbers from binary to decimal.
Now, try to write a set of instructions that anyone could follow to convert from binary to decimal. These instructions will be your pseudocode. 1. take the rightmost digit, if it is a 1, multiply it by 2 to the power of the current exponent and add it to the sum. 2. do the same with digits to the left of the right most digit. A little bit more thinking shows we need to tell what sum, and what exponent to start at. Anything multiplied by zero is zero, so we can simplify the pseudocode slightly. Let's also add variables for clarity. exponent = 0 sum = 0 take the rightmost digit ''d'', sum = sum + ''d'' * 2^exponent do the same with digits to the left of the rightmost digit. You can continue improving the pseudocode slightly until you have replaced much of the English instructions with Java-like code. Here is an example of what you might get.
exponent = 0 sum = 0 while (binaryNum!= 0) { rightmost = binaryNum % 10 //right most digit sum = sum + ''d'' * 2^exponent binaryNum = binaryNum / 10; //do the same with the digits to the left of the rightmost digit } It is good to make sure you understand what the above code is doing the percent sign is modulus, which gives the remainder. You can verify that the remainder of any number divided by 10 is the right most digit. Also don't forget that the forward slash represents integer division here. Now that you have some pseudocode, we want to make sure that it is sound, one good way is to turn it into Java code and run it through the compiler. Creating the Structure of the ProgramNow create a Java file with the general structure of your program. Define the class in a java file. Name the file the same name as the class name plus the ".java" extension. Define the main method. The following code shows the result of these steps. public class BinaryNum { public static void main(String[] args) { } } To be continued... | ||||||||||||||||||||||||||
Code |