Reputation: 9
Required output: testing default constructor\n \n \n -----------------------------\n \n ==>Testing default constructor/toString()\n 0/1\n
My output: testing default constructor\n \n \n -----------------------------\n \n ==>Testing default constructor/toString()\n Fraction@55f96302\n
public class Fraction {
private int num, demonator;
public Fraction() {
num = 0;
demonator = 1;
}
public Fraction(int input) {
num = input;
demonator = 1;
}
public Fraction(int input2, int input) {
if (num == 0)
throw new IllegalArgumentException("num = 0");
num = input2;
demonator = input;
}
public String ToString(int i) {
return num + "/" + demonator;
}
public double evaluate() {
return num / demonator;
}
public boolean isImproper() {
return (num > demonator);
}
public Fraction multiplyBy(Fraction another)
{
Fraction f = new Fraction(this.num*another.num, this.demonator*another.demonator); //you finish call to constructor
return f;
}
public void invert() {
if (num == 0)
throw new IllegalStateException("num = 0");
int temp = this.num;
num = demonator;
demonator = temp;
}
}
Upvotes: 0
Views: 97
Reputation: 914
You have typo in your code. In Java methods are camelCase, so the method that is used in conversion is toString
. But you implemented ToString
. Since the language is case-sensitive it's considered different method and therefor not used. So the basic method from object
is used. Also you should use @override
before methods that override methods in base class and such method is toString
.
Upvotes: 1