首页 >
> 详细

CS 570: Homework Assignment 1

1 Assignment Policies

Collaboration Policy. Homework will be done individually: each student must hand in their

own answers. It is acceptable for students to collaborate in understanding the material but not

in solving the problems or programming. Use of the Internet is allowed, but should not include

searching for existing solutions.

Under absolutely no circumstances code can be exchanged between students. Excerpts

of code presented in class can be used.

Your code must include a comment with your name.

2 Assignment

Define a class BinaryNumber 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 little-endian format. You may use bigendian

if you prefer; in that case you must state so at the beginning of your code as part of the

comment.

This assignment requests that a number of operations be supported. They are divided into

two groups. The first is a set of basic operations, the second is slightly more challenging and

addresses addition of binary numbers.

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.

2

• 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 a message should be printed

on the screen indicating this fact.

• An operation int toDecimal() for transforming a binary number to its decimal notation (cf.

the example given above).

• 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 amountToShift. The new digit should be

0. For example, the result of shifting “1011” 3 places to the right should yield “0001011”.

2.2 Addition of Binary Numbers

Here is an example of how two binary numbers of the same length are added1.

1 1 1 1 (carried digits)

1 0 1 1 0

+ 1 1 1 0 0

= 0 0 1 0 1 = 20

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 1 1 1 1 (carried digits)

1 0 1 1 0

+ 1 1 1 0 1

= 0 0 1 0 0 1 = 36

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.

1 Source: https://en.wikipedia.org/wiki/Binary_number

3

• 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 shiftR(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 file named BinaryNumber.java through Canvas. No report is required. Your

grade will be determined as follows:

• You will get 0 if your code does not compile.

• The code must implement the following UML diagram precisely.

• 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.

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 add(BinaryNumber aBinaryNumber)

public String toString() public int toDecimal()

public void clearOverflow()

联系我们

- QQ：99515681
- 邮箱：99515681@qq.com
- 工作时间：8:00-23:00
- 微信：codinghelp

- Cpslp程序语言代写、代做python编程设计、Program程序实验代写 2020-11-25
- Csci 1110作业代做、Data留学生编程代写、Java程序语言调试代做 2020-11-25
- 代写program程序、代做r课程编程、R程序实验代做代做留学生prolog 2020-11-25
- Be491留学生编程代做、代写java，Python/C++程序设计调试ma 2020-11-25
- 代写cmpt 214编程、代做programming语言、代写c/C++程序 2020-11-08
- 代写csci 2122课程、代做program编程实验、C++程序语言代写代 2020-11-08
- Fit5032语言编程代做、代写web程序实验、Web、Html程序语言代做 2020-11-08
- Com3503程序编程代做、Java，C++，Python留学生编程代写代写 2020-11-08
- 代写program程序课程、代写c++编程实验、C/C++编程语言代做 代做 2020-11-08
- Data留学生编程代做、代写python程序、Java，C++程序语言代写 2020-11-08
- 代写secj 1023实验编程、Programming程序代做、代写c++语 2020-11-08
- 代写cmpsc 465编程、代做java程序语言、Python，C++编程设 2020-11-07
- 代做mf 703语言编程、代写programming程序、Sql编程语言调试 2020-11-07
- 954246编程设计调试、代做programming程序、C++编程语言代写 2020-11-07
- Pstat 115程序实验代写、R编程语言调试、Data留学生程序代做 代写 2020-11-07
- Com1005课程编程代做、代写python程序、Java，C++程序语言调 2020-11-07
- Tcp留学生程序代写、Java程序设计调试、Java编程语言代写 帮做r语言 2020-11-07
- 代写program语言编程、代做data留学生程序、Python，Java编 2020-11-07
- 代做cosc2666编程、代写programming程序、C/C++程序语言 2020-11-07
- Digital编程设计代写、代做r程序实验、代写r留学生程序 调试matla 2020-11-07