Documente Academic
Documente Profesional
Documente Cultură
Learning Objectives
Understanding the Basics of Exception-handling
Defining a Subclass of a Standard Java Library Exception Class
Implementing Interfaces
Overriding Methods
In Project # 1, you completed the implementation for a class, ModInt, that
describes modular numbers. The code in Project # 1 has some design flaws.
We can enhance the robustness of the code by using some of the programming
features that we studied in chapter 9.
Arithmetic Interface
Define and provide Javadoc-style documentation for the Arithmetic interface
with signatures of the methods below:
1. Object add(Object obj)
2. Object subtract(Object obj)
3. Object multiply(Object obj)
4. Object divide(Object obj)
NotANumberException Class
Define a subclass, NotANumberException, of the standard Java library class
ArithmeticException. See the example discussed in class.
Duncan
Fall 2016
Rewrite the parameterized constructor so that it has exception-handling capabilities. This constructor should throw a NotANumberException when its
second explicit parameter, b, is less than 1 or its first explicit parameter n
is negative or greater than or equal to b. Otherwise, it creates a ModInt
object using its parameters. Also, your auxiliary gCD method should throw
the standard Java library IllegalArgumentException when its parameters are
both 0. In addition to the toString() method which you overrode in Project
# 1, override these methods:
1. public int hashcode(): This method returns an integer such that modular integers that are equal have the same hashcode. Automatically
generate the hashcode method in Netbeans, as demonstrated in class.
2. public boolean equals(Objects obj): This methods returns false if the
obj reference does not hold a ModInt object. Additionally, if both
operands, ModInt objects, are equal, this method returns true; otherwise, false. Two modular integers are equal if both numbers are equal
and they have the same base.
3. public int compareTo(Objects obj): This methods throws an IllegalArgumentException if the obj reference does not hold a ModInt object or
it holds a ModInt object whose base differs from the base of the implicit parameter. When its first operand is greater than the second,
the method returns 1; if they are equal, 0; otherwise, -1. This method
should not mutate either operand.
Duncan
Fall 2016
Write implementations for all the methods defined in the Arithmetic interface
in the class. You will need some type-casting between ModInt and Object
types. These methods should work as described below:
1. If the explicit parameter is not a ModInt object or it is a ModInt object
but its base differs from the base of the implicit parameter, the method
throws an IllegalArgumentException.
2. Additionally, for the divide method, when the divisor is 0 or the GCD
of the divisor and its base are not relatively prime - that is, their GCD
is not equals to 1 - an ArithmeticException should be thrown.
Additional Requirements
Use appropriate methods to compute the results for each expression given
above. 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. When throwing an exception,
provide a brief message indicating why the exception occurred.
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
*/
Duncan
Fall 2016
For the ModIntDemo class, after the @since line, add the following Javadoc:
* @see ModInt
For the ModInt class, after the @since line, add the following Javadoc:
* @see Arithmetic, NotANumberException
Add missing Javadoc documentation for each method in the ModInt class.
Be sure to also document all the methods in the Arithmetic Interface and
the NotANumberException classes. Run the Javadoc utility to make sure
that it generates documentation for the ModInt and NotANumberException
classes and the Arithmetic Interface. Also, remove all auto-generated Netbeans comments. Locate your source files, ModInt.java, Arithmetic.java, NotANumberException and ModIntDemo.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. See a sample program
interaction in Listing 1. Run the program three more times keeping all other
inputs the same, while using bases 6, 7 and 8 and observe what happens in
each case.
Listing 1: Sample Run.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Enter
Enter
Enter
Enter
Enter
the
the
the
the
the
Duncan
Fall 2016