Define a class Binary Number that represents binary numbers and a few simple operations on them,...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Define a class Binary Number that represents binary numbers and a few simple operations on them, as indicated below. An example of a binary number is 1011 Its length is 4. Note that its leftmost digit is the least significant one: it represents the decimal number 1*20 +0*21 +1*22 +1*23 = 13. This is called the little-endian format. For this assignment it is required that the little-endian format is used. You are asked to implement a number of operations. The operations are divided into two groups. The first is a set of basic operations, the second, a slightly more challenging method for adding binary numbers. Here are the signatures of the minimal set of methods to deliver: BinaryNumber private int data private boolean overflow public BinaryNumber(int length) public BinaryNumber(String str) public int getLength() public int getDigit(int index) public void shiftR(int amount) public void shiftL (int amount) public void add(BinaryNumber aBinaryNumber) public String to String() public int to Decimal() public void clearOverflow() 2.1 Basic operations The following operations should be supported: A constructor BinaryNumber(int length) for creating a binary number of length length and consisting only of zeros. A constructor BinaryNumber(String str) for creating a binary number given a string. For example, given the string "1011", the corresponding binary number should be created. For this exercise you will have to use some standard String operations. These are listed in the "Hints" section below. An operation int getLength() for determining the length of a binary number. An operation int getDigit(int index) for obtaining a digit of a binary number given an index. The starting index is 0. If the index is out of bounds, then an exception should be thrown, and an error message should be printed on the screen. An operation int toDecimal() for transforming a binary number to its decimal notation (cf. the example given above). Do not use ready-made methods that are available convert binary numbers to decimals. Calculate the value from your number's digits. An operation void shiftR (int amount) for shifting all digits in a binary number any number of places to the right, as indicated by a parameter amount (to shift). The new digits should be all Os. For example, the result of shifting "1011" 3 places to the right should yield "0001011". Implement also an analogous method void shiftL(int amount) 2.2 Addition of Binary Numbers Here is an example of how two binary numbers of the same length are added¹. + = + 1 1 1 1 = 0 Note that it is possible for the addition of two numbers to yield a result which has a larger length than the summands. In that case, this should be flagged by an appropriate boolean data field. Here is such an example. 1 0 1 1 1 1 1 0 1 1 0 1 1 0 0 1 1 1 0 1 0 1 = 20 1 1 0 0 1 1 1 0 0 1 0 1 (carried digits) = 36 (carried digits) This data field should be added to the data fields of the class BinaryNumber. Implement the following operations: void add (BinaryNumber aBinaryNumber) for adding two binary numbers, one is the binary number that receives the message and the other is given as a parameter. If the lengths of the binary numbers do not coincide, then a message should be printed on the screen indicating this fact. Otherwise, it modifies the receiving binary number with the result of the addition. An operation clearOverflow() that clears the overflow flag. An operation String toString() for transforming a binary number to a String. If the number is the result of an overflow, the string "Overflow" should be returned. 2.3 Hints For the BinaryNumber(String str) constructor, the following operations might come in handy: · char java.lang.String.charAt(int index), which returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing. - int java.lang.Character.getNumericValue(char ch), which returns the int value that the specified Unicode character represents. For the shiftL(int amount) operation, it might be useful to define an auxiliary private method reallocate, that makes room for a new digit. This operation would in turn use int[] java.util.Arrays.copyOf(int[] original, int newLength), which copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. 3 Submission instructions Submit a single zip file named hw1_FirstInitial_Lastname.zip (like hw1_P_Collins.zip) that contains file named BinaryNumber.java through Canvas. No report is required. Please note: You will get 0 if your code does not compile. The code must implement the signatures in the above yellow box exactly. We will try to feed erroneous and inconsistent inputs to all methods. All arguments should be checked. Partial credit: may be given for style, comments, and readability. Define a class Binary Number that represents binary numbers and a few simple operations on them, as indicated below. An example of a binary number is 1011 Its length is 4. Note that its leftmost digit is the least significant one: it represents the decimal number 1*20 +0*21 +1*22 +1*23 = 13. This is called the little-endian format. For this assignment it is required that the little-endian format is used. You are asked to implement a number of operations. The operations are divided into two groups. The first is a set of basic operations, the second, a slightly more challenging method for adding binary numbers. Here are the signatures of the minimal set of methods to deliver: BinaryNumber private int data private boolean overflow public BinaryNumber(int length) public BinaryNumber(String str) public int getLength() public int getDigit(int index) public void shiftR(int amount) public void shiftL (int amount) public void add(BinaryNumber aBinaryNumber) public String to String() public int to Decimal() public void clearOverflow() 2.1 Basic operations The following operations should be supported: A constructor BinaryNumber(int length) for creating a binary number of length length and consisting only of zeros. A constructor BinaryNumber(String str) for creating a binary number given a string. For example, given the string "1011", the corresponding binary number should be created. For this exercise you will have to use some standard String operations. These are listed in the "Hints" section below. An operation int getLength() for determining the length of a binary number. An operation int getDigit(int index) for obtaining a digit of a binary number given an index. The starting index is 0. If the index is out of bounds, then an exception should be thrown, and an error message should be printed on the screen. An operation int toDecimal() for transforming a binary number to its decimal notation (cf. the example given above). Do not use ready-made methods that are available convert binary numbers to decimals. Calculate the value from your number's digits. An operation void shiftR (int amount) for shifting all digits in a binary number any number of places to the right, as indicated by a parameter amount (to shift). The new digits should be all Os. For example, the result of shifting "1011" 3 places to the right should yield "0001011". Implement also an analogous method void shiftL(int amount) 2.2 Addition of Binary Numbers Here is an example of how two binary numbers of the same length are added¹. + = + 1 1 1 1 = 0 Note that it is possible for the addition of two numbers to yield a result which has a larger length than the summands. In that case, this should be flagged by an appropriate boolean data field. Here is such an example. 1 0 1 1 1 1 1 0 1 1 0 1 1 0 0 1 1 1 0 1 0 1 = 20 1 1 0 0 1 1 1 0 0 1 0 1 (carried digits) = 36 (carried digits) This data field should be added to the data fields of the class BinaryNumber. Implement the following operations: void add (BinaryNumber aBinaryNumber) for adding two binary numbers, one is the binary number that receives the message and the other is given as a parameter. If the lengths of the binary numbers do not coincide, then a message should be printed on the screen indicating this fact. Otherwise, it modifies the receiving binary number with the result of the addition. An operation clearOverflow() that clears the overflow flag. An operation String toString() for transforming a binary number to a String. If the number is the result of an overflow, the string "Overflow" should be returned. 2.3 Hints For the BinaryNumber(String str) constructor, the following operations might come in handy: · char java.lang.String.charAt(int index), which returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing. - int java.lang.Character.getNumericValue(char ch), which returns the int value that the specified Unicode character represents. For the shiftL(int amount) operation, it might be useful to define an auxiliary private method reallocate, that makes room for a new digit. This operation would in turn use int[] java.util.Arrays.copyOf(int[] original, int newLength), which copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. 3 Submission instructions Submit a single zip file named hw1_FirstInitial_Lastname.zip (like hw1_P_Collins.zip) that contains file named BinaryNumber.java through Canvas. No report is required. Please note: You will get 0 if your code does not compile. The code must implement the signatures in the above yellow box exactly. We will try to feed erroneous and inconsistent inputs to all methods. All arguments should be checked. Partial credit: may be given for style, comments, and readability.
Expert Answer:
Answer rating: 100% (QA)
Step 1 Programming in Java The littleendian format is used in this program The leftmost digit is the least significant one a binary number is 1011 it represents the decimal number 12 0 12 1 02 2 12 3 ... View the full answer
Related Book For
Essentials Of Statistics For The Behavioral Sciences
ISBN: 9781133956570
8th Edition
Authors: Frederick J Gravetter, Larry B. Wallnau
Posted Date:
Students also viewed these programming questions
-
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...
-
123 Compare the purely graphical properties of these two notations, and the ways in which the graphical properties of each display correspond to the information structure being defined. Describe...
-
At its headquarters in Ventura, California, Patagonia's office space feels more like a national park lodge than the main office of a $400 million retailer. It has a Douglas fir staircase and a...
-
Seles Hardware Limited reported the following amounts for its cost of goods sold and merchandise inventory: Seles made two errors: (1) ending inventory for 2012 was overstated by $2,000 and (2)...
-
In Exercises 1623, let f(x) = x 2 - x - 4 and g(x) = 2x - 6. Find f(-x). Is f even, odd, or neither?
-
Based on the design, briefly discuss the data collection procedures to be used. Be sure to include the area rea of focus and targeted sample as part of these procedures. Develop a hypothetical...
-
McDonald Company shows the following condensed income statement information for the current year: Required Calculate the degree of financialleverage. 3,500,000 (1.700,000) Revenue from sales Cost of...
-
6. A carver begins work on a block of granite that measures 20 cm by 10 cm by 5 cm. Granite has a density of 2.7 g/cm. What is the mass of the piece of granite? a. What do you need to calculate...
-
A cat is running north for 25 m to get away from the dog. Then the cat turns down an alley at a 25 S of W and runs faster for 10.0 m. Finally, the cat makes one last turn and goes 16 m south and lost...
-
We are asked to determine how secure a software lock is. It makes use of the English alphabet of 26 letters. The key to open the lock is FOUR letters long. Upper and lower case are considered a...
-
What is on your mind? What do you want to share? What are you curious about? What do you want the instructor to explain in more detail? What insights do you want to put out there? What crazy,...
-
Assume that a daytime 30-second commercial on Nickelodeon's "Inspector Gadget" costs $9,000 in a particular market. The show has a total target audience of 45,000 children ages ages 6-12. There are...
-
Write a new patient encounter report explaining the concepts below and how you would communicate to the client and perhaps her daughter. Address the following: Explain the importance of autonomy,...
-
GAAP (Generally Accepted Accounting Principles): 1. What is GAAP and why is it important in the field of accounting? 2. What are the core principles of GAAP? 3. How does GAAP impact financial...
-
Absence of Fructose in semen analysis suggests: a. Ejaculatory duct obstruction b. Testicular Failure c. Vas deferens obstruction d. Prostatic urethral obstruction
-
Cleaning Service Company's Trial Balance on December 31, 2020 is as follows: Account name Debit Credit Cash 700 Supplies Pre-paid insurance Pre-paid office rent Equipment Accumulated depreciation -...
-
Research indicates that people who volunteer to participate in research studies tend to have higher intelligence than nonvolunteers. To test this phenomenon, a researcher obtains a sample of 200 high...
-
To evaluate the effect of a treatment, a sample is obtained from a population with a mean of = 30, and the treatment is administered to the individuals in the sample. After treatment, the sample...
-
The following data are from a two-factor study examining the effects of two treatment conditions on males and females. a. Use an ANOVA with a = .05 for all tests to evaluate the significance of the...
-
Based on the photographs in Figure 26.13, in which segment(s) is the Antp gene normally expressed? Figure 26.13: (a) Normal fly (b) Antennapedia mutant
-
The bush baby, a small African mammal, is a remarkable jumper. Although only about 8 inches long, it can jump, from a standing start, straight up to a height of over 7 feet! Use the particle model to...
-
Hicham El Guerrouj of Morocco holds the world record in the 1500 m running race. He ran the final 400 m in a time of 51.9 s. What was his average speed in mph over the last 400 m? A. 14.2 mph B. 15.5...
Study smarter with the SolutionInn App