Mathematical numbers in java
The task is taken from post:
http://forum.java.sun.com/thread.jspa?threadID=729292
But it is very interesting problem. So I would prefer to discuss it in this forum.
Define anArith interface giving the add, sub, mul, div, abs methods to be supported for any number type.
Define anInt class that implements the Arith interface and has an int field.
Define aReal class that implements the Arith interface and has a double field.
Define aRational class that implements the Arith interface and has 2 int fields, 1 for numerator, 1 for denumerator.
Define aComplex class that implements the Arith interface and has 2 double fields, 1 for real part, 1 for imaginery part.
I may be have a complicated program.
Does anybody know about another solution of task?
publicinterface ComplexNumber{
double getReal();
double getImage();
}
Real is Complex:
publicinterface RealNumberextends ComplexNumber{
double getReal();
}
Rational is Real:
publicinterface RationalNumberextends RealNumber{
int getNumerator();
int getDenominator();
}
Int is Rational:
publicinterface IntNumberextends RationalNumber{
int getInt();
}
Int has only add,sub and mul operations:
publicinterface IntOperation<ReturnType, ArgumentType>{
ReturnType add(ArgumentType n);
// add sub and mul operations here
}
Rational also has div operation:
publicinterface RationalOperation<ReturnType, ArgumentType>extends IntOperation<ReturnType, ArgumentType>{
ReturnType div(ArgumentType a);
}
So Int is:
publicinterface Intextends IntNumber, IntOperation<Int, IntNumber>{}
Rational is:
publicinterface Rationalextends RationalNumber, RationalOperation<Rational, RationalNumber>{}
and so on.
After we can implement Int, Rational, Real and Compplex.
For Example:
publicclass IntImplimplements Int{
privateint n;
public IntImpl(int n){ this.n = n;}// constructor
public Int add(IntNumber n){// add operation
returnnew IntImpl(this.n + n.getInt());
}
public IntNumber add(IntImpl n){// optimized add operation
returnnew IntImpl(this.n + n.n);
}
publicint getInt(){return n;}
publicint getNumerator(){return n;}// Int is Rational
publicint getDenominator(){return 1;}
publicdouble getReal(){return n;}// Int is Complex
publicdouble getImage(){return 0;}
}
publicclass RationalImplimplements Rational{
privateint numerator;
privateint denominator;
public RationalImpl(int numerator){
this( numerator, 1 );
}
public RationalImpl(int numerator,int denominator){
this.numerator = numerator;
this.denominator = denominator;
}
public Rational add(RationalNumber r){
int p = numerator * r.getDenominator() + denominator * r.getNumerator();
int q = denominator * r.getDenominator();
returnnew RationalImpl(p, q);
}
public Rational div(RationalNumber r){
int p = numerator * r.getDenominator();
int q = denominator * r.getNumerator();
returnnew RationalImpl(p, q);
}
public RationalNumber add(RationalImpl r){// optimized add operation
int p = numerator * r.denominator + denominator * r.numerator;
int q = denominator * r.denominator;
returnnew RationalImpl(p, q);
}
public RationalNumber div(RationalImpl r){// optimized div operation
int p = numerator * r.denominator;
int q = denominator * r.numerator;
returnnew RationalImpl(p, q);
}
publicint getNumerator(){return numerator;}
publicint getDenominator(){return denominator;}
publicdouble getReal(){return ((double ) numerator) / ((double ) denominator);}// Real is Complex
publicdouble getImage(){return 0;}
}
And you can run it:
publicclass Example{
publicstaticvoid main(String[] args){
Inttwo =new IntImpl(2);
Rational one =new RationalImpl(1);
Rational half = one.div(two);
System.out.println("Half = " + half.getReal());
}
}

