Depending on desired size, integral numbers can be represented in many ways. For
ID: 3591141 • Letter: D
Question
Depending on desired size, integral numbers can be represented in many ways. For example, the Java programming language already offers Short, Integer, Long, BigInteger. In order to avoid code duplication, many algorithms could be expressed on an abstract level (i.e., in form of an abstract class IntegralNumber, where the implementation of basic operations is postponed until we know the numeric representation.)
Consider the following abstract class IntegralNumber and its implementation of the method factorial. The method factorial relies on basic operations isPositive, copy, toNumber, multiply, decrement. The basic methods will be implemented by the concrete subclasses.
abstract public class IntegralNumber
{
/**
* computes this!
*
* @return a new object (same type as this) representing this!
*/
public IntegralNumber factorial()
{
IntegralNumber factor = this.copy();
IntegralNumber result = this.toNumber(1);
while (factor.isPositive())
{
result = result.multiply(factor);
factor = factor.decrement();
}
return result;
}
// list of basic methods
/**
* tests if this represents a positive value.
*
* @return true, iff this > 0; false, otherwise.
*/
abstract boolean isPositive();
/**
* returns a copy of this.
*
* @return copy of this, where the return value and this are NOT identical.
*
* obj = this.copy();
* assert obj != this; // <--- assertion must hold
*/
abstract IntegralNumber copy();
/**
* Creates a new object with the same dynamic type as this representing the value i.
*/
abstract IntegralNumber toNumber(int i);
/**
* Multiplies this with that.
*
* @param that another object that has the same dynamic type as this
* @return a new object representing this*that
*/
abstract IntegralNumber multiply(IntegralNumber that);
/**
* returns this-1.
*
* @return a new object that is 1 less than this.
*/
abstract IntegralNumber decrement();
}
1. Implement a new subclass Int (that uses int as internal representation). Besides the basic operations Int should also implement toString.
2. Use your class Int to compute all factorials from 0 to 9. What is your output?
Explanation / Answer
Please find my implementation.
public class Int extends IntegralNumber{
private int number;
public Int() {
number = 0;
}
public Int(int x) {
number = x;
}
@Override
boolean isPositive() {
if(number > 0)
return true;
else
return false;
}
@Override
IntegralNumber copy() {
IntegralNumber obj = new Int(number);
return obj;
}
@Override
IntegralNumber toNumber(int i) {
IntegralNumber obj = new Int(i);
return obj;
}
@Override
IntegralNumber multiply(IntegralNumber that) {
IntegralNumber obj = new Int(((Int)that).number*number);
return obj;
}
@Override
IntegralNumber decrement() {
IntegralNumber obj = new Int(number-1);
return obj;
}
@Override
public String toString() {
return Integer.toString(number);
}
public static void main(String[] args) {
for(int i=0; i<=9; i++) {
IntegralNumber obj = new Int(i);
System.out.println("Factorial("+i+"): "+obj.factorial().toString());
}
}
}
/*
Sample run:
Factorial(0): 1
Factorial(1): 1
Factorial(2): 2
Factorial(3): 6
Factorial(4): 24
Factorial(5): 120
Factorial(6): 720
Factorial(7): 5040
Factorial(8): 40320
Factorial(9): 362880
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.