Starting from:

$29

Project # 2 An Enhanced Number System


Learning Objectives
• Understanding the Basics of Exception-handling
• Defining a User-defined Exception Class
• Implementing Interfaces
• Overriding Methods
In Project # 1, you completed the implementation for a class, Number, that
describes a number system. In Laboratory Exercise # 4, you defined a class
that implemented an interface. The code in Project # 1 has some design
flaws. We can enhance the robustness of the code by using some of the
programming constructs that we studied in chapter 9. In this programming
project, you will employ some of those constructs.
Arithmetic Interface
Define an Arithmetic interface as described in Lab # 4. See Lab # 4 handout
for the specifications for this interface.
InvalidNumberException Class
Define a subclass, InvalidNumberException, of the standard Java library class
NumberFormatException. See the example discussed in class.

The Number Class
Make the following modifications to the Number class:
1. Delete all the methods for the arithmetic operations - add, subtract,
multiply and divide.
2. Delete the equals method.
3. Delete the body of the paramterized constructor,
public Number(String num, int radix).
4. Modify the signature of the Number class so that it implements the
Arithmetic Interface and the standard Java API Comparable interface.
Rewrite the paramterized constructor so that it has exception-handling capabilities. This constructor should report an exception when any of the
following happens:
1. If the radix is less than 1 or greater than 35, this method reports an
InvalidNumberException with an appropriate message.
2. If the string representation of the number contains two or more periods
or two or more dashes, an InvalidNumberException should be reported.
3. If the string representation of the number contains a dash and the
dash is not the first character, an InvalidNumberException should be
reported. Also, if the string representation of the number contains a
period and the period is the last character, an InvalidNumberException
should be reported.
4. Next for every character in the string representation of the number,
if the character is not ‘0’-’9’, ’A’-’Z’, ’.’ or ’-’, report an InvalidNumberException.
5. For every character in the string that is not ’.’ or ’-’, report an InvalidNumberException if the value of the digit is greater than or equals to
the base.
If none of the conditions for an exception is met, create the Number as done
in Project # 1.
In addition to the toString() method which you overrode in Project # 1,
override these methods:

1. public boolean equals(Objects obj): This methods returns false if the
obj reference does not hold a Number object. Additionally, if both
operands, Number objects, when converted to doubles are equal, this
method returns true; otherwise, false.
2. public int compareTo(Objects obj): This methods throws an IllegalArgumentException if the obj reference does not hold a Number object.
When the operands are converted to double, if the first operand is less
than the second, the method returns -1; if the first operand is greater
than the second, the method returns 1; otherwise, 0.
Write implementation for all the methods defined in the Arithmetic interface
in the class. You will need some type-casting between Number and Object
types. These methods should work as described below:
1. If the explicit parameter is not a Number object, the method reports
an IllegalArgumentException.
2. If the base of the explicit parameter is different from the base of the
implicit parameter, the method reports an IllegalArgumentException.
3. Additionally, for the divide method, only, when the explicit parameter
is converted to a double and its equivalent value is 0, an ArithmeticException should be reported.
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 trim() 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.

The NumberDemo Class
In addition to the calculations done in Project #1, this class will perform
two additional ones:
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
7. Using the equals method, determine whether −112.23 is equal to −34.36.
8. Finally, using the compareTo method, determine whether 10008 is less
than, greater than or equal to 10006.
Additional Requirements
Use appropriate methods to compute the results for each expression given
above. X denotes the answer Do not define any additional methods other
than those described in this handout. Also, do not change the signature of
any method or rewrite methods unless indicated in this handout.
Write header comments for each class and interface using the following
Javadoc documentation template:
/**
* Explain the purpose of this class; what it does <br
* CSC 1351 Project # 2
* @author YOUR NAME
* @since DATE THE CLASS WAS WRITTEN
* @version 2
*/
For the NumberDemo class, after the @since line, add the following Javadoc:
* @see Number

For the Number class, after the @since line, add the following Javadoc:
* @see Arithmetic, InvalidNumberException
Add missing Javadoc documentation for each method in the Number class.
Be sure to also document all the methods in the Arithmetic Interface and
the InvalidNumberException classes. Run the Javadoc utility to make sure
that it generates documentation for the Number and InvalidNumberException classes and the Arithmetic Interface. Also, remove all auto-generated
Nebeans comments. Locate your source files, Number.java, Arithmetic.java,
InvalidNumberException and NumberDemo.java, and enclose them in a zip
file, YOURPAWSID proj02.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]
7 -112.2[3] and -34.3[6] are equal
8 1000[8] is greater than 1000[6]

More products