-3

Is there a way to print two different overridden toString messages given a particular condition? For example, the following is a simple example where I need to print two different error messages if check value is 0. I have overridden the toString to get the "Error: Check = 0" to print but I am having difficulty displaying the "Error: Double value not precise".

public class test{
public static void main(String[] args)
{
System.out.println(new Set(1,0)); // Should print "Error: Check = 0"
System.out.println(new Set(1,0).toDouble()); // Should print "Error: Double value not precise"
}
}

// ------------------------------------------
public class Set{
int a;
int check;

Fraction(int a, int check)
{
this.a = a;
this.check = check;
}
// ------------------------------------------
public String toString()
{
if(this.check == 0)
  return "Error: Check = 0";

return (this.a + ":" + this.check);
}
// ------------------------------------------
public double toDouble()
{
double number;

if(this.check != 0)
  number = ((a * check) + (1/3));
else
  number = 0;

return number;
}
// ------------------------------------------
public String toString(double d)
{
return ("" + d);
}
}
Samuel
  • 395
  • 2
  • 5
  • 20
  • Why not use a custom Exception, though! – Am_I_Helpful Apr 13 '16 at 03:48
  • 1
    Where is the overridden toString() with "Error: Double value not precise" located? – djs Apr 13 '16 at 03:50
  • You can not do this because your tostring will not be called in case of second function because it is returning double – Alok Apr 13 '16 at 03:54
  • I tried to add a String toString(Double d) method but it does not get called in the println execution. (btw, what's the downvote for?) – Samuel Apr 13 '16 at 04:03

3 Answers3

0

There could be several answers to your problem. One of them is to use your "check" flag as an identifier of the error. E.g. you are already interpreting a value of 0 as "Error: check is 0". Similarly, you can assign a value of -1 or something (assuming check is just a flag and has no business importance. Otherwise you can define a dedicated flag variable to do this job). You can assign this value of -1 to check in the toDouble method based on your particular condition. Then, in the toString method, first check if check has a value of -1. If it is -1, display the "Double value not precise" error. Else if it is 0, display the "Check is 0" error. Let me know if I am not explaining this very well.

VHS
  • 9,534
  • 3
  • 19
  • 43
  • I tried to implement this kind of thinking by adding a Boolean double_check variable to the class Set and I tripped the variable when inside the toDouble method. But I could not get it to work because the system does not call my overridden toString method for the println statement. It does not even call my overrideen toString(double d) method. – Samuel Apr 13 '16 at 04:12
  • Oh I see. Can you try changing the return type of your toDouble method from double to Set? The reason why your toString method is not called is because the operand object in this case is Double and not Set. – VHS Apr 13 '16 at 04:20
  • Did you try it yet? Just return a new instance of Set from your toDouble method of the Set class. In the new instance, set -1 for check. And then in the toString, if check is -1 display the "Double is not precise" error. – VHS Apr 13 '16 at 04:32
  • I need the zero value and cannot have another value as a flag. – Samuel Apr 13 '16 at 04:41
0

Check this

public double toDouble()
{
double number;

if(this.check != 0)
  number = ((a * check) + (1/3));
else
{number=0;
    System.out.println("Error: Double value not precise");}

return number;
}
Neji Soltani
  • 1,522
  • 4
  • 22
  • 41
0

The toString() you're trying to use is an instance method. Each class can declare its own instance methods. Those instance methods can't be overridden at will by anything else. In your case, since toDouble() returns a double and Java will "auto-box" this to a Double, your println will call the toString() method defined in Double. There's no way in Java to tell it "replace Double's toString() method with one of my choosing". Declaring a toString() with a double parameter definitely won't do it.

The only way to override an instance method, such as the toString() method here, is in a subclass. If Double weren't a final class, you could define your own MyDouble subclass that extends Double, and override the toString() method in MyDouble. But you'd also have to make toDouble() return a MyDouble instead of a Double or double. Then your println would call the MyDouble toString, since it has a MyDouble object. The only way to get an overriding instance method is with an object of a subclass, like this. This overriding would not change toString() for any other Double. (However, Double is marked as final, so you can't actually define your own subclass.)

ajb
  • 31,309
  • 3
  • 58
  • 84
  • OK this makes sense and I think that is the issue I am running into. I am just a little confused when you say "there's no way in Java [to do this]" but then you sort of give instructions in the second paragraph about a way to do this. – Samuel Apr 13 '16 at 04:27
  • There's no way to override `Double`'s `toString()` from inside your `Set` class. – ajb Apr 13 '16 at 04:30