Sunteți pe pagina 1din 4

A More Robust Modular Integer

CSc 1351: Programming Project # 2

An Enhanced Modular Integer Class


Out: 9/15
Due: 10/5 by 11:50 PM
Maximum Points: 25

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

A More Robust Modular Integer

CSc 1351: Programming Project # 2

The ModInt Class


Make the following modifications to the ModInt class:
1. Add a private static method gCD that computes the GCD of two integers.
See laboratory exercise # 2 for the Euclidean GCD algorithm.
2. Delete all the methods for the arithmetic operations - add, subtract, multiply
and divide.
3. Delete the body of the parameterized constructor,
public ModInt(int n, int b).
4. Modify the signature of the ModInt class so that it implements the Arithmetic Interface and the standard Java API Comparable interface.

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

A More Robust Modular Integer

CSc 1351: Programming Project # 2

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.

The ModIntDemo Class


In addition to the calculations done in Project #1, this class will perform
two new ones:
1. Using the equals method, determine whether n1 (n2 n3 ) is equal to
n1 n2 n1 n3 .
2. Finally, using the compareTo method, determine whether n1 /(n2 + n3 )
is less than, greater than or equal to n1 /n2 n1 /n3 .

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

A More Robust Modular Integer

CSc 1351: Programming Project # 2

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

modular a r i t h m e t i c base -> 5


first number -> 2
second number -> 4
third number -> 3
fourth number -> 1

n1 = 2( mod 5) , n2 = 4( mod 5) , n3 = 3( mod 5) and n4 = 1( mod 5)


n1 + n3 = 0( mod 5)
n3 x ( n4 - n2 ) / ( n3 - n4 ) = 3( mod 5)
n3 ^6 = 4( mod 5)
( n1 / n2 ) / ( n2 + n3 ) = 4( mod 5)
( n1 - n3 ) / ( n2 x n4 ) = 1( mod 5)
Is n1 ( n2 - n3 ) equal to n1 x n2 - n1 x n3 ? true
n1 /( n2 + n3 ) is less than n1 / n2 - n1 / n3

Duncan

Fall 2016

S-ar putea să vă placă și