Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help me build this Java program. Program #3: Binary4Bitint This program can be thought of as a real object oriented version of the previous

Please help me build this Java program.

image text in transcribedimage text in transcribed
Program #3: Binary4Bitint This program can be thought of as a real object oriented version of the previous one because the class Bi - nary4BitInt 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 Binary4BitInt and apply other func- tions in the class to those objects. Create a class public class Binary4BitInt with the non-static class-variables int b3, b2, b1, bo representing the bits of a 4-bit integer and the following functions. 1. A constructor public Binary4BitInt (int b3, int b2, int bl, int bo) 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 separated by a blank. 3. A function public int binaryToDecimal () to return the decimal integer (2 - 8 and = 7) cor- responding to the 4-bit binary integer given by the class-variables b3 to bo. (See a similar function in Binary4BitAdd-class, which has four parameters.) 4. A function public Binary4BitInt add (int b3, int b2, int bl, int bo) 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-variables. Use one local variable int carry. Shown below is a skeleton-code of this function. (This function is a variation of the add-function in the class Binary4BitInt in the ebook and also a variation of addAndPrint-function in class Binary4BitAdd in Program #2; it has a dif- ferent signature than both of those functions.) Use one statement to compute b3 and do not compute the related carry. bo += this.b0; / /add class-variable to parameter b0 carry = b0 / 2; bo := 2; //the rightmost bit of the sum bi += carry + this.bl; . . . return (new Binary4BitInt (. . .) ) ; 5. 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, bo. 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 . binary- ToDecimal () in the println-statement in line 08 of the skeleton 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 output line 6 in the sample run). The lines 04-13 in the skeleton code below process one pair of 4-bit input binary integers. 01. public static void main (String args) 02. { ... //declaration of first, second, sum, b3, b2, etc 03. for (int i = 0; i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Unity From Zero To Proficiency Beginner A Step By Step Guide To Coding Your First Game

Authors: Patrick Felicia

1st Edition

1091872023, 978-1091872028

More Books

Students also viewed these Programming questions