Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Program #3. Binary 4BitInt This Program Can Be Thought Of As A Real Object Oriented Version Of The Previous One Because The Class Binary4Bit Int
Program #3. Binary 4BitInt This Program Can Be Thought Of As A Real Object Oriented Version Of The Previous One Because The Class Binary4Bit Int Defined Here Has: (1) Non-Static Class-Variables And Functions, (2) A Constructor (And Also A ToString-Function), And (3) Functions Which Create Objects Of Type Binary4Bit Int And Apply Other Functions In The Class
Program #3. Binary4BitInt This program can be thought of as a real object oriented version of the previous one because the class Binary4Bit Int defined here has: (1) non-static class-variables and functions, (2) a constructor (and also a toString-function), and (3) functions which create objects of type Binary4Bit Int and apply other functions in the class to those objects. Create a class public class Binary4Bit Int with the non-static class-variables int b3, b2, b1, b0 repre- senting the bits of a 4-bit integer and the following functions. 1. A constructor public Binary4BitInt (int b3, int b2, int b, int b0) to initialize each class- variable by the corresponding parameter value. 2. A function public String toString() to return the string of the (values of) class-variables b3 to bo sepa- rated by a blank. 3. A function public int binaryToDecimal () to return the decimal integer (2-8 and 7) corresponding to the 4-bit binary integer given by the class-variables b3 to b0. (See a similar function in Binary4Bit Add-class, which has four parameters.) 4. A function public Binary4BitInt add (int b3, int b2, int b1, int b0) to return the bits of the binary sum of the 4-bit binary integer given by the parameters and the 4-bit binary integer given by the class-vari- ables. Use one local variable int carry. Shown below is a skeleton-code of this function. (This function is a vari- ation of the add-function in the class Binary4Bit Int in the ebook and also a variation of addAndPrint-func- tion in class Binary4BitAdd in Program #2; it has a different signature than both of those functions.) Use one statement to compute b3 and do not compute the related carry. b0+this.b0; //add class-variable to parameter bo carry b0/ 2; b02; //the rightmost bit of the sum blcarry + this.bl; return (new Binary4BitInt(...)); 4. A main-function to give suitable prompts, to read the keyboard inputs for two 4-bit binary integers (one at a time; see lines 1 and 4 in the sample run), and do other operations as indicated below. Use the local variables Binary4BitInt first, second, sum, in addition to Scanner scan and int b3, b2, b1, b0. Keep these variable declarations before the for-statement in line 03 of the skeleton code shown below to avoid repeated memory allocation for them. After the first prompt (see line 1 in the sample run below), it reads the first 4 input bits, calls the constructor to create the object first, and then creates the output line 2 in the sample run by calling first.toString() and first.binaryToDecimal () in the println-statement in line 08 of the skele- ton code. The process is repeated with the 2nd prompt (see lines 3-5 in the sample run), this time creating the object second. Finally, it computes and prints the binary sum (see line 6 in the sample run). The lines 04-13 in the skele- ton code below process one pair of 4-bit input binary integers. 01. public static void main(String[] args) 02. //prompt to enter two 4-bit binary integers, etc 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. for (int i=0; i < 5; i++) //process all 5 input pairs in one run { System.out.print (" Enter..."); b3 scan.nextInt(); ... //code to read values of b2, bl, bo first new Binary4Bit Int (b3, b2, bl, b0); System.out.println(...); //create line 2 in the sample run System.out.print (" Enter..."); ... //repeat code corresponding to lines 05-08 sum first.add(second.b3, ...); //create line 6 in the sample run 14. ) Sample run (line numbers are not program output and note the blank lines 3 and 7; user inputs are shown in bold): 1. Enter 1st 4-bit binary integer (with a space between the bits): 0101
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started