java - Rational class writing add and multiply methods -
this continuation of question posted last week i'm still having trouble with. i'm trying write rational class containing 2 private instance variables num , den datatype biginteger. rational constructor takes 2 int's parameters.
problem whenever write add or multiply method in rational class eclipse gives me error. don't know why give me error. understand it, operators +, -, *, / can used on primitive datatypes , since private instance variables biginteger's nonpprimitive datatypes , addition or multiplication have done through add or multiply method.
code below gives me error don't know why. don't understand what's conceptually wrong code. reason error conceptual using this.num/this.den/r.num/r.den in methods or error have related syntax? error add , multiply methods same , says "the constructor rational(biginteger,biginteger) undefined".
public class rational{ public rational(int x, int y) { num = biginteger.valueof(x); den = biginteger.valueof(y); } public rational add(rational r) { return new rational(this.num.multiply(r.den).add(r.num.multiply(this.den)), this.den.multiply(r.den)); } public rational multiply(rational r) { return new rational(this.num.multiply(r.num), this.den.multiply(r.den)); } private biginteger num; private biginteger den }
the constructor have defined, takes 2 int
s arguments.
public rational(int x, int y) { num = biginteger.valueof(x); den = biginteger.valueof(y); }
and compiler complains because passing 2 bigintegers
.
have new overloaded constructor
public rational(biginteger x, biginteger y) { num = x; den = y; }
Comments
Post a Comment