$29.99
Number System
Learning Objectives
• Understand the basics of defining a class
• Defining accessors, mutators and constructors
• Implementing some special-purpose instance methods
Many computer science applications involve working with numbers that are
binary (base 2), octal (base 8), decimal (base 10) or hexadecimal (base 16).
These applications include encoding colors, representing a machine’s instruction set, encoding characters and the list goes on. In this project, you will
implement a class that performs binary arithmetic by converting the operands
to base 10 numbers, do the relevant operation in base 10, and then converting
the answer back to the desired base.
Definition 1. A real number can be represented using any integer number
as a base (sometimes also called a radix or scale). The choice of a base yields
a representation of numbers known as a number system.
1 Valid Numbers
In base b, the digits 0, 1, . . . , |b − 1| are used (where, by convention, for
bases larger than 10, the symbols A, B, C, ... are generally used as symbols
representing the decimal numbers 10, 11, 12, . . .). If any digit in a number
is greater than or equal to the base in which a number is written then the
number is not well-formed.
Converting Non Base-10 to Base-10 Numbers
To convert a number Nb to a T10 number, we apply the following calculation:
If Nb = dkdk−1dk−2dk−3 . . . d2d1d0.d−1d−2d−3 · · · dj+3dj+2dj+1dj
, where the
dis are the digits in the number, then its base 10 equivalent is
T10=dj b
j + dj+1b
j+1 + · · · + d−1b
−1 + d0b
0 + d1b
1 + d2b
2 + · · · + dkb
k
. (1)
Example: 2011.258 to its base 10 equivalent.
2011.258 = 5×8
−2 +2×8
−1 +1×8
0 + 1×8
1 + 0×8
2 + 2×8
3 = 1033.328125
Converting Base-10 To Non Base-10 Numbers
One way to convert a base-10 number X.Y to a base b number is by performing repeated division on X, while keeping track of the quotients and
remainders and then performing repeat multiplication on 0.Y , while keeping
track of the whole and fractional parts of the product. This approach is
illustrated in the example below.
Example: Convert 29.37510 to its base 3 equivalent. First, we convert 29
to base 3 using repeated division.
b = 3
Q0 = 29
d0 = Q0 mod b = 29 mod 3 = 2
Q1 = Q0 div b = 29 div 3 = 9
d1 = Q1 mod b = 9 mod 3 = 0
Q2 = Q1 div b = 9 div 3 = 3
d2 = Q2 mod b = 3 mod 3 = 0
Q3 = Q2 div b = 3 div 3 = 1
d3 = Q3 mod b = 1 mod 3 = 1
Q4 = Q3 div b = 1 div 3 = 0
T he equivalent base 3 number is d3d2d1d0 = 10023
Duncan 2 Fall 2015
Number System CSc 1351: Programming Project # 1
Next, we convert 0.375 to base 3 using repeated multiplication.
b = 3
f0 = 0.375
f0 × b = 0.375 × 3 = 1.125 = 1 + 0.125
d−1 = 1
f1 = 0.125
f1 × b = 0.125 × 3 = 0.375 = 0 + 0.375
d−2 = 0
f2 = 0.375
f2 × b = 0.375 × 3 = 1.125= 1 + 0.125
d−3 = 1
f3 = 0.125
f3 × b = 0.125 × 3 = 0.375 = 0 + 0.375
d−4 = 0
f4 = 0.375
· · ·
T he equivalent base 3 number is .d−1d−2d−3d−4 · · · = .1010 · · ·3
We continue this computation until the fractional part becomes 0 or we
terminate when we have the desired precision (a specified number of decimal
places). Thus, 29.37510 ≈ 1002.1010 · · ·3
Some Useful Methods
Read the online API documentation for details on these methods. They may
come in handy while doing this project.
1. The length() method from the String class.
2. The charAt(int) method from the String class.
3. The indexOf(char) method from the String class.
4. The toUpperCase() method from the String class.
5. The replaceAll(String,String) method from the String class.
6. The Double.parseDouble(String) method from the Double class.
7. The Double.toString(double) method from the Double class.
Also, review the use of + to concatenate Strings.
2 The Number Class
Define a class called Number that represents a number in a certain base. This
class consists of three instance variables: sign, a character whose value is ’-’
for a negative number and null (’\0’), otherwise, value, a String representing
the number and base, an integer representing the radix. This class will consist
of several constructors and instance methods as well as auxiliary methods
whose access specifiers must be private. Read the file containing starter code
for API documentation of the methods that you are to implement.
3 The NumberDemo Class
This class will consist of only the main method. Your program should perform
that calculations below.
1. 12.258 + 13.758 = X8
2. (ABC.7516 − 18.5F16) / 2.F B16 = X16
3. 3.459(32.259 + 3.059) = X9
4. (10111.112 − 1100110.012) / − 1012 = X2
5. −58 × 58 = X8
6. −0.58 × −0.58 = X8
Additional Requirements
Use appropriate methods to compute the results for each expression given
above. X denotes the answer you get when the expression is evaluated. Display the original expression and the answer. I suggest that you add additional
test cases to exhaustively test the program but be sure to remove any additional test cases from your main method before you submit your project. We
have not covered exception-handling so the program will not work correctly
with an invalid number.
I have starter code for Number.java and NumberDemo.java on the course
Moodle page. In the starter code, we have followed the Javadoc-style documentation discussed in class. Download the files and write code where
Number System CSc 1351: Programming Project # 1
indicated. Do not define any additional methods. Also, do not change the
signature of any method or rewrite a method that I have already written.
Write header comments for each class using the following Javadoc documentation template:
/**
* Explain the purpose of this class; what it does <br
* CSC 1351 Project # 1
* @author YOUR NAME
* @since DATE THE CLASS WAS WRITTEN
* @version 1
*/
For the NumberDemo class, after the @since line, add the following Javadoc:
* @see Number
Add missing Javadoc documentation for each method in the Number class.
Run the Javadoc utility to make sure that it generates documentation for the
Number class. Locate your source files, Number.java and NumberDemo.java,
and enclose them in a zip file, YOURPAWSID proj01.zip, and submit your
programming project for grading using the digital dropbox set up for this
purpose. Here is a sample program interaction, where X denotes the answer
that is obtained when the expression is evaluated using relevant methods
from the class:
Listing 1: Sample Run.
1 12.25[8] + 13.75[8] = X [8]
2 ( ABC .75[16] - 18.5 F [16]) / 2. FB [16] = X [16]
3 3.45[9](32.25[9] + 3.05[9]) = X [9]
4 (10111.11[2] - 1100110.01[2]) / -101[2] = X [2]
5 -5[8] x 5[8] = X [8]
6 -0.5[8] x -0.5[8] = X [8]