(DecBin2) Write a program that will accept a number in decimal and display the number in binary. In Programming Assignment 2, you wrote DecBinl which converted and displayed a decimal number in binary for BUT your program was limited to decimal numbers between 0 and 15 due to the fixed number of bits being sought ( four). In this programming assignment we seek to remedy this limitation! (DecBin1) Consider the conversion of a non-negative decimal (base 10) integer to base 2 (binary). We will assume the non- negative base 10 integer has value between 0 and 15 inclusive. As a guiding example, let's convert (13), to binary (base 2). Using long division, we successively divide by the base to which we are converting, 2 in this case. 6 3 1 0 2)13 26 -12 6 0 101 1 The resulting binary number is (1101), (writing the remainders as read from right to left). Now let's try to express this sequence of operations in Java using integer division and remainder (modulus). In Java, we can express the quotient and remainder in long division as follows. 6 2)13 -12 1 Let's assume the initial decimal number is in variable n (an int) and let's refer to the digits of the result as (b3 b2 b1 b0)₂ = (1 101), (respectively). 2 Then 13/2 integer division yields the quotient b0-n2; b1 = (n/2) = 2; b2= ((n / 2) / 2) = 2; b3 (((n / 2) / 2) / 2 ) = 2; and b0 = n2; n-n / 2; b1 = n2; n-n / 2; b2 = n2; n-n / 2; b3 = n2; This is rather tedious as we keep having the calculate the new quotient using prior quotients. An easier way to do this is to change n to the most recent quotients as we go, //b0 will be 1 // new n will be 6 // bl will be 0 (since 6 // new n will be 3 13%2 modulus operator yields the remainder // b2 will be 1 (since 3 // new n will be 1 // b3 will be 1 (since 1 = 1 2 - 0) 2 - 1) 2 - 1)

icon
Related questions
Question
(DecBin2) Write a program that will accept a number in decimal and display the number in binary.
In Programming Assignment 2, you wrote DecBinl which converted and displayed a decimal number in binary form
BUT your program was limited to decimal numbers between 0 and 15 due to the fixed number of bits being sought (i.e.
four). In this programming assignment we seek to remedy this limitation!
(DecBin1) Consider the conversion of a non-negative decimal (base 10) integer to base 2 (binary). We will assume the non-
negative base 10 integer has value between 0 and 15 inclusive.
As a guiding example, let's convert (13), to binary (base 2).
Using long division, we successively divide by the base to which we are converting, 2 in this case.
6
3
0
2)13 26 23
-12
6
2
0
1
0
1
1
The resulting binary number is (1101), (writing the remainders as read from right to left).
Now let's try to express this sequence of operations in Java using integer division and remainder (modulus). In
Java, we can express the quotient and remainder in long division as follows.
-12
1
Let's assume the initial decimal number is in variable n (an int) and let's refer to the digits of the result as
(b3 b2 b1 b0), =(1 101), (respectively).
Then
13/2
integer division
yields the quotient
b0n & 2;
b1 = (n/2) & 2;
b2= ((n / 2)/2) & 2;
b3 (((n / 2)/2)/2 ) & 2;
and
13%2
modulus operator.
yields the remainder
This is rather tedious as we keep having the calculate the new quotient using prior quotients. An easier way to do this is to
change n to the most recent quotients as we go,
b0n & 2;
nn / 2;
bln 2;
nn / 2;
b2 n 2;
n = n / 2;
b3 = n2;
n = n / 2;
We're done once the quotient becomes 0 which will be on the last step.
//b0 will be 1
// new n will be 6.
// bl will be 0 (since 6 & 2 = 0)
// new n will be 3.
// b2 will be 1 (since 3 & 2 = 1)
// new n will be 1
// b3 will be 1 (since 1 & 2 = 1)
// new n will be 0 (since 1 / 2 = 0)
Sample Output:
Enter a positive integer: 37
The number 37 in binary is 100101.
Notice how we were essentially repeating divisions by 2 and updating the integer, n, containing the remaining bits in the
number. The situation now is that we don't know ahead of time how many times we need to repeat this sequence of
divisions, but we know were have finished when there are no more remaining bits to extract, i.e. when n becomes 0.
Transcribed Image Text:(DecBin2) Write a program that will accept a number in decimal and display the number in binary. In Programming Assignment 2, you wrote DecBinl which converted and displayed a decimal number in binary form BUT your program was limited to decimal numbers between 0 and 15 due to the fixed number of bits being sought (i.e. four). In this programming assignment we seek to remedy this limitation! (DecBin1) Consider the conversion of a non-negative decimal (base 10) integer to base 2 (binary). We will assume the non- negative base 10 integer has value between 0 and 15 inclusive. As a guiding example, let's convert (13), to binary (base 2). Using long division, we successively divide by the base to which we are converting, 2 in this case. 6 3 0 2)13 26 23 -12 6 2 0 1 0 1 1 The resulting binary number is (1101), (writing the remainders as read from right to left). Now let's try to express this sequence of operations in Java using integer division and remainder (modulus). In Java, we can express the quotient and remainder in long division as follows. -12 1 Let's assume the initial decimal number is in variable n (an int) and let's refer to the digits of the result as (b3 b2 b1 b0), =(1 101), (respectively). Then 13/2 integer division yields the quotient b0n & 2; b1 = (n/2) & 2; b2= ((n / 2)/2) & 2; b3 (((n / 2)/2)/2 ) & 2; and 13%2 modulus operator. yields the remainder This is rather tedious as we keep having the calculate the new quotient using prior quotients. An easier way to do this is to change n to the most recent quotients as we go, b0n & 2; nn / 2; bln 2; nn / 2; b2 n 2; n = n / 2; b3 = n2; n = n / 2; We're done once the quotient becomes 0 which will be on the last step. //b0 will be 1 // new n will be 6. // bl will be 0 (since 6 & 2 = 0) // new n will be 3. // b2 will be 1 (since 3 & 2 = 1) // new n will be 1 // b3 will be 1 (since 1 & 2 = 1) // new n will be 0 (since 1 / 2 = 0) Sample Output: Enter a positive integer: 37 The number 37 in binary is 100101. Notice how we were essentially repeating divisions by 2 and updating the integer, n, containing the remaining bits in the number. The situation now is that we don't know ahead of time how many times we need to repeat this sequence of divisions, but we know were have finished when there are no more remaining bits to extract, i.e. when n becomes 0.
Question to ponder: The bits are produced in the reverse order. How can we encode this information into a single
decimal number for display? In the above example, we must create the number 1101 (one thousand one hundred and one)
to display the base 2 result.
Hints for constructing numbers or Strings that look like numbers
IMPORTANT: These samples appear in the PA 4 Unit. Use the debugger to trace them by setting
at breakpoint a as shown below and single stepping through the code paying attention to how the I
and total variables of Example 1 and the digitAs String and total variables in Example2
are changing with each step.
Example 1: (DecBin2_Example_1) A Numeric Approach
int total = 0;
for(int i=3; i < 10; i+=2)
{
The String
}
total total 10;
total += 1;
System.out.println( "Output 1: " + total);
Example 2: (DecBin2_Example_2) A String Approach
String total = "";
for(int i=3; i <10; i+=2)
{
}
String digitAsString = "" + 1;
total = total + digitAsString;
System.out.println( "Output 2: " + total);
Note that the statement digitAsString = "" + i does not perform numerical addition by rather joins
(concatenates) the String "" with the number contained in I and produces a String result. For example,
if i=5, then "" + 5"" + "5" (note how Java converted 5 to "5") → "5"
"" is called the empty string; it is a string that contains no characters.
Transcribed Image Text:Question to ponder: The bits are produced in the reverse order. How can we encode this information into a single decimal number for display? In the above example, we must create the number 1101 (one thousand one hundred and one) to display the base 2 result. Hints for constructing numbers or Strings that look like numbers IMPORTANT: These samples appear in the PA 4 Unit. Use the debugger to trace them by setting at breakpoint a as shown below and single stepping through the code paying attention to how the I and total variables of Example 1 and the digitAs String and total variables in Example2 are changing with each step. Example 1: (DecBin2_Example_1) A Numeric Approach int total = 0; for(int i=3; i < 10; i+=2) { The String } total total 10; total += 1; System.out.println( "Output 1: " + total); Example 2: (DecBin2_Example_2) A String Approach String total = ""; for(int i=3; i <10; i+=2) { } String digitAsString = "" + 1; total = total + digitAsString; System.out.println( "Output 2: " + total); Note that the statement digitAsString = "" + i does not perform numerical addition by rather joins (concatenates) the String "" with the number contained in I and produces a String result. For example, if i=5, then "" + 5"" + "5" (note how Java converted 5 to "5") → "5" "" is called the empty string; it is a string that contains no characters.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer