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