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());

}

}

[9894 byte] By [raindropa] at [2007-10-2 18:11:14]
# 1

> 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.

Why in this forum, an why discuss it in two places, and not just the first one where it was started?

The discussion is still going on there. Duffy and Jos posted some valuable contributions to it over there.

prometheuzza at 2007-7-13 19:31:02 > top of Java-index,Other Topics,Algorithms...
# 2

> Why in this forum, an why discuss it in two places,

> and not just the first one where it was started?

> The discussion is still going on there. Duffy and Jos

> posted some valuable contributions to it over there.

They discuss some aspects of java language, like

how to implement equals method from Object class.

But I want to consider some mathematical aspects of

number implementation. For example Int should

have behaviour as Complex, but it has no div operations.

It is not topic for New To Java Technology forum.

raindropa at 2007-7-13 19:31:02 > top of Java-index,Other Topics,Algorithms...
# 3

> They discuss some aspects of java language, like

> how to implement equals method from

> Object class.

They were discussing more than that.

> But I want to consider some mathematical aspects of

> number implementation. For example Int should

> have behaviour as Complex, but it has no div

> operations.

> It is not topic for New To Java Technology

> forum.

If you say so.

prometheuzza at 2007-7-13 19:31:02 > top of Java-index,Other Topics,Algorithms...
# 4

Another reason to NOT discuss your question here is that your question is not really a question about algorithms. It is an implementation question.

You have no doubt that the way you multiply a real times an integer is to convert the integer to a real and to multiply the two together. The algorithm is solved.

Similarly, all the other cases you have to deal with are also trivial. That is why the problem was assigned. It involves dealing with a matric of possibilities all of them essentially trivail. You got 4 operations, 4 types of arguments, 2 operands for any operator, Holy shite batman! 32 cases. An implementation nightmare!

What the best way to deal with those 32 cases is may be a codeing challenge but (yawn) it doesn't much interest us algorithms types.

marlin314a at 2007-7-13 19:31:02 > top of Java-index,Other Topics,Algorithms...
# 5

> > Why in this forum, an why discuss it in two

> places,

> > and not just the first one where it was started?

> > The discussion is still going on there. Duffy and

> Jos

> > posted some valuable contributions to it over

> there.

>

> They discuss some aspects of java language, like

> how to implement equals method from

> Object class.

> But I want to consider some mathematical aspects of

> number implementation. For example Int should

> have behaviour as Complex, but it has no div

> operations.

Really? Why not? You can't divide two Ints?

I can't understand what you mean. This code takes in two ints, divides them, and returns an int value:

package math;

public class IntDivTest

{

public static void main(String [] args)

{

try

{

int x = ((args.length > 0) ? Integer.parseInt(args[0]) : 0);

int y = ((args.length > 1) ? Integer.parseInt(args[1]) : 1);

System.out.println("x= " + x);

System.out.println("y= " + y);

System.out.println("x/y = " + x/y);

System.out.println("x%y = " + x%y);

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

Perfectly legit. Run it with 33 and 7 as the command line args and see what you get.

> It is not topic for New To Java Technology forum.

No, but you should be spending more time there.

%

duffymoa at 2007-7-13 19:31:02 > top of Java-index,Other Topics,Algorithms...
# 6

> Another reason to NOT discuss your question here is

> that your question is not really a question about

> algorithms. It is an implementation question.

I agree.

> You have no doubt that the way you multiply a real

> times an integer is to convert the integer to a real

> and to multiply the two together. The algorithm is

> solved.

>

> Similarly, all the other cases you have to deal with

> are also trivial. That is why the problem was

> assigned. It involves dealing with a matric of

> possibilities all of them essentially trivail. You

> got 4 operations, 4 types of arguments, 2 operands

> for any operator, Holy shite batman! 32 cases. An

> implementation nightmare!

It's even easier than that, because of symmetry. Once you figure out how to add an Int and a Real, you've also figured out how to add a Real to an Int.

Actually, I think it's more on the order of 40 cases, but the point is correct: It's an undergraduate exercise, with a few admitted subtleties if you think hard about it. It's not a particularly difficult algorithm, just a subtle design of implementation.

> What the best way to deal with those 32 cases is may

> be a codeing challenge but (yawn) it doesn't much

> interest us algorithms types.

Agreed.

%

duffymoa at 2007-7-13 19:31:02 > top of Java-index,Other Topics,Algorithms...
# 7
> You can't divide two Ints?Depends on the field.
YAT_Archivista at 2007-7-13 19:31:02 > top of Java-index,Other Topics,Algorithms...
# 8

> > You can't divide two Ints?

>

> Depends on the field.

I can see the OP's point (and perhaps yours, YAT), in the sense that dividing two Ints might result in a non-Int result.

But it's easy to see that dividing two Ints results in a perfectly good Rational, which is a number. So even if you don't want a Real result it's still within the bounds of the Number classes we've created here to end up with Rational result for Int division.

%

duffymoa at 2007-7-13 19:31:02 > top of Java-index,Other Topics,Algorithms...