Ask Programming Language Expert

Program: Two's Complement

Overview: The real point of this assignment is to give you a chance to work with exceptions. But you can't do anything with exceptions unless there's something that can go horribly wrong, so we need something with plenty of potential for error . . .

Most computer systems and languages store integer values using two's complement representation. In two's complement, positive values are represented as ordinary binary values (ex: 610 = 01102; I'll use a simple four- bit representation for these examples). Representing a negative value, such as that of -610, is more involved. Begin with the binary representation of the absolute value of the number (0110). Invert the bit values (1001). Finally, add 1 (1010). The two's complement representation of -610  is 10102.

Why bother with this special representation of negative values? With it, we can perform subtraction using addition. In decimal, you know that 6 - 2 = 6 + (-2) = 4. Using the two's complement version of -2 (which is 1110, because 0010 → 1101 + 0001 = 1110), we can compute 610 + (-210) = 01102 + 11102 = (1)01002 = 410.

That leading 1-bit is lost, because we have only the four bits available to store values, leaving just 0100 as the result.

Observe that whenever the leftmost bit is a 0, the value is positive or is zero. When that bit is a 1, the value is always negative. Way back when, computers sometimes used representations that permitted the "value" -0 to exist. You can imagine the confusion that caused!

Assignment: Write a Java class (completely documented according to the class documentation guidelines, of course) named BinaryNumber that represents integer values in two's complement form. Here are the construc- tors and methods that you are to  implement:

  • BinaryNumber() - sets the new object's value to 0, using an 8-bit capacity as the default.
  • BinaryNumber(String) - sets the new object's value to the binary value provided as a Java String. Ex: An argument of "1011010" would force the creation of the 7-bit (not 8!) binary value 10110102Similarly, "00000" is a 5-bit value. Yes, leading zeroes count when determining the capacity for this constructor.

The object's capacity is determined by the number of binary digits in the string, except that the smallest acceptable representation is four bits. If the constructor is given a string shorter than four digits, say "10", append leading 0's to pad the size to four (0010, in this case).

If the string contains characters other than 0 and 1, the Java exception IllegalArgumentException is to be thrown.

  • setValue(String) - Changes the value held by the current object to that of the given String. It works as does the second constructor, except that the representation size doesn't change to match the length of the argument.

If the length of the argument exceeds that of the current object, throw the exception IllegalArgumentException.

  • toString() - Return the value of the object as a string containing the characters '0' and/or '1'. The length of the string is to match the number of bits stored by the object.
  • equal(BinaryNumber) - returns true if the value of this object matches that of the argument. Note that the sizes need not match for the values to match. For example, 01101 = 0001101.
  • incrementBy(BinaryNumber) - Add the argument's value to the value currently held by this object. The result is to replace this object's previous value.

If this object's storage can't hold the result of the addition (e.g., a 4-bit value is asked to add a big 6-bit value to itself), throw a BufferOverflowException and leave the value of this object unchanged.

If both objects are holding positive values and the result would be interpreted to be negative (because of a leftmost bit of 1), throw an ArithmeticException and leave the value of this object unchanged. Other combinations of values (+/-, -/+, and -/-) are to be added and stored with no exceptions thrown. (I'm not saying that this is necessarily the best way to handle these situations; I'm just saying that I think there's already enough in this assignment for you to worry about.)

  • decrementBy(BinaryNumber) - Subtract the argument's value from the value held by this object. If the argument is positive, then it should be converted to a negative two's complement value and added to the value of this object. If the given argument is already a negative value, the effect should be that of adding the corresponding positive value.

As with incrementBy(), the result may be a BufferOverflowException or an ArithmeticException, and an unchanged value for this object.

Data: For this assignment, you need to create your own testing program, named Program2.java, that you will submit for grading along with your BinaryNumber() class. We will not be providing a testing program; you can use the testing programs from Program #1 and the Section #1 activity as guides for writing your own. Be sure that your BinaryNumber class is in a separate file (BinaryNumber.java) from your testing program, so as not to interfere with our testing program.

Because we will be grading your testing programs, sharing testing programs between students is not allowed. You may, however, share test cases. That is, you can post to Piazza, "Remember to test your decrementBy() method with an argument of zero!", but you can't post code that does that test.

By the way, be sure that your testing program checks for the exceptions that the BinaryNumber methods should be throwing when things go wrong. You can bet that our testing program will be looking for them.

Output: As with Program #1, the class you're creating should not generate any output of its own. Because your testing program and its output will be unique, there is no required output or output format for this assignment.

Turn In: Use 'turnin' to electronically submit your well-tested, completely-documented, and well-structured (BinaryNumber.java file and your equally-well tested, documented and structured Program2.java testing program to the cs127bsXp02 directory (replace the 'X' with your section's letter) at any time before the stated due date and  time.

Want to Learn More?

  • There are other complement methods that can be used with number systems other than binary. See

http://mathforum.org/library/drmath/view/55949.html for examples.

Other Requirements and Hints:

  • We aren't requiring that you use a particular internal representation for your objects' current values. You'll hear me say this a lot this semester: Choose the representation that best supports the operations. (Why? It makes the code easier to write!) You can use arrays, or you can use strings. Think about what your methods need to do, and then which of those two representations will make those actions easiest to code.
  • Put your external block comment for this assignment at the top of the Program2.java file. Of course, your BinaryNumber class is expected to have a class block comment ahead of it, and all other rules for documentation and coding style are to be followed.

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M91411415
  • Price:- $65

Priced at Now at $65, Verified Solution

Have any Question?


Related Questions in Programming Language

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

Assignment task -q1 a the fibonacci numbers are the numbers

Assignment Task - Q1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As