Program #2: Binary4BitAdd This program allows you to choose two 4-bit binary integers and to see...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Program #2: Binary4BitAdd This program allows you to choose two 4-bit binary integers and to see the result of their addition (sum) in both the binary form and the usual decimal form. The three new features in this program are the use of: (1) several static functions (in addition to the main-function), some with parameters and others without pa- rameters, (2) several class-variables, and (3) a better technique (than duplicating parts of the code) to process multiple cases of inputs in a single run of the program. Create a class public class Binary4BitAdd with the class-variables static int b13, b12, b11, b10, b23, b22, b21, b20 (where bij = (j+ 1)th bit from the right for the ith 4-bit binary integer) and the following functions. The variables bij are static because they are used in static function readIn- put (), for example. As usual, give a blank line before each function-definition if it follows the end of another function-definition or the declaration of class-variables. 1. The function public static void readInput () to give a prompt using one print-statement (see lines 1-2 in the sample run below) and to read the keyboard inputs. Use a local variables Scanner scan and use scan.nextInt () 4 times to read the values of the first 4 input bits b13 to b10 in that order. Then, use scan. next() to skip (i.e., read without saving) the part "+" separating those bits from the next 4 bits. Next, use scan.nextInt () 4 times to read the values of the second 4 input bits b23 to b20 in that order. (For the prompt in lines 1-2 below, use one long print-statement of the form System.out.print("\nEnter... \" + \"" + "\n and ...: "), spread over two lines (note the 4 blanks before "and"). 2. The function public static int binaryToDecimal (int b3, int b2, int bl, int b0) to compute the decimal integer (2-8 and ≤ 7) represented by the bits b3 to bo (left to right), and return that integer. The return-value is 4*b2 + 2*b1 + b0 if b3 = 0 and is -8 + 4*b2 + 2*bl + bo, otherwise. Do not use an if-statement to compute it. (Note the ordering of the parameters; we read and write the bits of a binary integer from left to right, as in the case of the digits of a decimal integer.) Shown below is a skeleton code for this function; it does not need a local variable. public static int binaryToDecimal (int b3, int b2, int bl, int b0) { return (...); 3. The function public static void addAnd Print () to compute the bits b3, b2, b1, and bo of the 4-bit binary sum of the 4-bit binary integers b13b12b11b10 and b23b22b21b20 and print the resulting sum-bits and the decimal integer corresponding to the sum (see line 5 in the sample run be- low). Use the local integer variables b3, b2, b1, b0, and carry. Shown below is part of the com- putation of the sum-bits and the successive values of the carry-bit (you need not keep the comments in your code). Avoid unnecessary computation of the last (4th) carry-bit and use only one statement to compute b3. b0b10+ b20; //initial value; b0 = 0, 1, 2 carry= b0 / 2; //carry = 1 if b0 = 2 and = 0 otherwise b0%= 2; //the rightmost bit of the binary form of sum b1 = bil + b21+ carry; //initial value carry=b1 / 2; ... //similarly compute the bits bl, b2 and b3; 4. The main-function, which calls the above functions as shown below in a skeleton code. Lines 3-8 in the skeleton code process one pair of 4-bit input integers. The for-statement starting in line 2 (after '{') and ending in line 8 avoids the repetition of the code in lines 3-8 and yet process the five cases of input-pairs of binary integers. 1. public static void main(String[] args) 2. { for (int i = 0; i < 5; i++) //process all 5 cases of inputs in one run 3. { readInput (); //creates lines 1-2 in the sample run int first = binaryToDecimal (b13, b12, bll, b10), 4. 5. second binaryToDecimal (b23, b22, b21, b20); 6. //two println-statements for lines 3-4 in the sample run. addAndPrint (); //creates line 5 in the sample run 7. 8. 9. } } Sample run (line numbers are not program-output and note the blank line 6; the user's keyboard-inputs are shown in bold. Note that we have shown here only a small part of the required output.): 1. Enter the bits of two 4-bit binary integers separated by " + " 2. and a space between the bits: 0 1 0 1 + 1 0 0 1 3. 1st 4-bit binary input integer: 0 1 0 1 = 5 4. 2nd 4-bit binary input integer: 1001 = -7 5. Binary sum: 1 1 1 0 = -2 6. 7. Enter two Submit: The program source-code, the outputs for the five input-cases shown below, and the error-reports (if any). See programming-style-and-submission guidelines. (a) 0101 +1001 (d) 1001+0101 (b) 0101+0101 (e) 0101+0001 b, (bit 0/1). b₂ (bit 0/1). b, (bit 0/1) bo (bit 0/1). Notes on input/output-variables of binaryToDecimal-function • The input-variables are the bits b3, b2, bl, and b0 (from left to right) of a 4-bit binary integer. They are the parameters of the function and are shown in the block-diagram below in the order they appear as parameters (instead of the usual alphabetical order). • The output-variable is the corresponding decimal integer -8 and ≤ 7. This is the return-value of the function. (c) 1001+1001 Compute the decimal integer ≥-8 and ≤7 corresponding to a 4-bit binary integer b.b₂b,bo, where each b = 0 or 1. the decimal integer corresponding to the 4-bit binary integer b₂b₂b₁b₁ Program #2: Binary4BitAdd This program allows you to choose two 4-bit binary integers and to see the result of their addition (sum) in both the binary form and the usual decimal form. The three new features in this program are the use of: (1) several static functions (in addition to the main-function), some with parameters and others without pa- rameters, (2) several class-variables, and (3) a better technique (than duplicating parts of the code) to process multiple cases of inputs in a single run of the program. Create a class public class Binary4BitAdd with the class-variables static int b13, b12, b11, b10, b23, b22, b21, b20 (where bij = (j+ 1)th bit from the right for the ith 4-bit binary integer) and the following functions. The variables bij are static because they are used in static function readIn- put (), for example. As usual, give a blank line before each function-definition if it follows the end of another function-definition or the declaration of class-variables. 1. The function public static void readInput () to give a prompt using one print-statement (see lines 1-2 in the sample run below) and to read the keyboard inputs. Use a local variables Scanner scan and use scan.nextInt () 4 times to read the values of the first 4 input bits b13 to b10 in that order. Then, use scan. next() to skip (i.e., read without saving) the part "+" separating those bits from the next 4 bits. Next, use scan.nextInt () 4 times to read the values of the second 4 input bits b23 to b20 in that order. (For the prompt in lines 1-2 below, use one long print-statement of the form System.out.print("\nEnter... \" + \"" + "\n and ...: "), spread over two lines (note the 4 blanks before "and"). 2. The function public static int binaryToDecimal (int b3, int b2, int bl, int b0) to compute the decimal integer (2-8 and ≤ 7) represented by the bits b3 to bo (left to right), and return that integer. The return-value is 4*b2 + 2*b1 + b0 if b3 = 0 and is -8 + 4*b2 + 2*bl + bo, otherwise. Do not use an if-statement to compute it. (Note the ordering of the parameters; we read and write the bits of a binary integer from left to right, as in the case of the digits of a decimal integer.) Shown below is a skeleton code for this function; it does not need a local variable. public static int binaryToDecimal (int b3, int b2, int bl, int b0) { return (...); 3. The function public static void addAnd Print () to compute the bits b3, b2, b1, and bo of the 4-bit binary sum of the 4-bit binary integers b13b12b11b10 and b23b22b21b20 and print the resulting sum-bits and the decimal integer corresponding to the sum (see line 5 in the sample run be- low). Use the local integer variables b3, b2, b1, b0, and carry. Shown below is part of the com- putation of the sum-bits and the successive values of the carry-bit (you need not keep the comments in your code). Avoid unnecessary computation of the last (4th) carry-bit and use only one statement to compute b3. b0b10+ b20; //initial value; b0 = 0, 1, 2 carry= b0 / 2; //carry = 1 if b0 = 2 and = 0 otherwise b0%= 2; //the rightmost bit of the binary form of sum b1 = bil + b21+ carry; //initial value carry=b1 / 2; ... //similarly compute the bits bl, b2 and b3; 4. The main-function, which calls the above functions as shown below in a skeleton code. Lines 3-8 in the skeleton code process one pair of 4-bit input integers. The for-statement starting in line 2 (after '{') and ending in line 8 avoids the repetition of the code in lines 3-8 and yet process the five cases of input-pairs of binary integers. 1. public static void main(String[] args) 2. { for (int i = 0; i < 5; i++) //process all 5 cases of inputs in one run 3. { readInput (); //creates lines 1-2 in the sample run int first = binaryToDecimal (b13, b12, bll, b10), 4. 5. second binaryToDecimal (b23, b22, b21, b20); 6. //two println-statements for lines 3-4 in the sample run. addAndPrint (); //creates line 5 in the sample run 7. 8. 9. } } Sample run (line numbers are not program-output and note the blank line 6; the user's keyboard-inputs are shown in bold. Note that we have shown here only a small part of the required output.): 1. Enter the bits of two 4-bit binary integers separated by " + " 2. and a space between the bits: 0 1 0 1 + 1 0 0 1 3. 1st 4-bit binary input integer: 0 1 0 1 = 5 4. 2nd 4-bit binary input integer: 1001 = -7 5. Binary sum: 1 1 1 0 = -2 6. 7. Enter two Submit: The program source-code, the outputs for the five input-cases shown below, and the error-reports (if any). See programming-style-and-submission guidelines. (a) 0101 +1001 (d) 1001+0101 (b) 0101+0101 (e) 0101+0001 b, (bit 0/1). b₂ (bit 0/1). b, (bit 0/1) bo (bit 0/1). Notes on input/output-variables of binaryToDecimal-function • The input-variables are the bits b3, b2, bl, and b0 (from left to right) of a 4-bit binary integer. They are the parameters of the function and are shown in the block-diagram below in the order they appear as parameters (instead of the usual alphabetical order). • The output-variable is the corresponding decimal integer -8 and ≤ 7. This is the return-value of the function. (c) 1001+1001 Compute the decimal integer ≥-8 and ≤7 corresponding to a 4-bit binary integer b.b₂b,bo, where each b = 0 or 1. the decimal integer corresponding to the 4-bit binary integer b₂b₂b₁b₁
Expert Answer:
Answer rating: 100% (QA)
Below is the Java program for Binary4BitAdd based on the provided specifications java import javauti... View the full answer
Related Book For
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill
Posted Date:
Students also viewed these programming questions
-
Design a Java class that represents a cache with a fixed size. It should support operations like add, retrieve, and remove, and it should evict the least recently used item when it reaches capacity.
-
can someone solve this Modern workstations typically have memory systems that incorporate two or three levels of caching. Explain why they are designed like this. [4 marks] In order to investigate...
-
Label all the bars in the truss of Figure 4.13 that are unstressed when the 60-kip load acts. Figure 4.13 60 kips B 0 MOL K J D 180 kips E - F G H 120 kips
-
The fiscal year-end for the Northwest Distribution Corporation is December 31. The company's 2018 financial statements were issued on March 15, 2019. The following events occurred between December...
-
The annual report is considered by some to be the single most important printed document that companies produce. In recent years, annual reports have become large documents. They now include such...
-
What are the minimum energies of the two oppositely directed gamma rays in a PET procedure?
-
On January 1, 2012, Mitta Corporation acquires a 60% interest (12,000 shares) in Train Company for $156,000. Train stockholders equity on the purchase date is as follows: Common stock ($5 par)...
-
You are a small employer with 90 employees. Recruiting and retention factors lead you to conclude that you should offer some type of retirement plan for your employees. Identify and briefly describe...
-
The City of Castletons General Fund had the following post-closing trial balance at June 30, 2022, the end of its fiscal year: Debits Credits Cash $ 265,120 Sales Taxes Receivable 11,000 Taxes...
-
Write a unix command that will show (print) the pathname for your current working directory. Name a Unix command would you use to change file permissions?
-
The yield-to-maturity of a 10-year bond selling is 6%, while the coupon is 8% annually. The current price is $1,148. What is the approximate duration of this bond?
-
Describe two major differences between Medicare and Medicaid. Could the federal government run the Medicaid program by itself? Why or why not? Discuss the cases studied in this area to support your...
-
Create a presentation consisting of (1) a slide presentation and (2) an accompanying 1000-word script to address the following scenario: You work at a psychology consultancy specialising in equality...
-
What is the presumption if the creditor accepts the debtor's payment of a later installment without making any reservation as to prior installments?
-
If f(x) = 5x-4e", find: f'(x) = f'(5)= f" (x) = "(5) =
-
Clearwater Hospital has never used standard costs. By searching industry literature, however, you have determined the following nationwide averages for hospital labs: Three plates are required per...
-
Perform the operation by first converting the numerator and denominator to scientific notation. Write the answer in scientific notation. 7,200,00/0.000009
-
In 2012, Lou has a salary of $54,000 from her job. She also has interest income of $1,700. Lou is single and has no dependents. During the year, Lou sold silver coins held as an investment for a...
-
Olive Corporation was formed and began operations on January 1, 2012. The corporation's income statement for the year and the balance sheet at year-end are presented below. The corporation made...
-
Sally hires a maid to work in her home for $250 per month. The maid is 25 years old and not related to Sally. During 2012, the maid worked 10 months for Sally. a. What is the amount of Social...
-
Which mechanism of bacterial genetic transfer does not require recombination with the bacterial chromosome?
-
Explain why small deletions and duplications are less likely than large ones to have a detrimental effect on an individuals phenotype. If a small deletion within a single chromosome happens to have a...
-
Explain why inversions and reciprocal translocations do not usually cause a phenotypic effect. Then explain how they can do so in certain cases.
Study smarter with the SolutionInn App