Reputation: 12684
I need a method that will accept String number as a parameter and return double if it has a remainder, or int if it is decimal.
I wrote such code:
private double convertToNumber(String number) {
double d = Double.parseDouble(number);
if (d % 1.0 == 0) {
return Integer.parseInt(number);
} else {
return d;
}
}
but as signature of this method is double it returns double in any case.
Please give me some hint.
Upvotes: 1
Views: 14974
Reputation: 9041
I'm not quire sure I understand the intention of returning a double
or and int
knowing that's not really possible for a single function to return different data types.
What I think I understand from your sample code, is that if a String (For example, "123.0" doesn't matter how many trailing zeros) is passed into the CalculatorEngine
, from the link you provided, you want it to be treated as an int
and for the math operator to perform calculations with an int
and a double
(since currentTotal is a double
).
Now after all the calculations are done, if currentTotal ends with a .0 then you probably want that treated as an int also and drop the point, otherwise keep the point.
If I'm understanding this correctly, here's what I think CalculatorEngine
might look like:
public static void main(String[] args) {
CalculatorEngine ce = new CalculatorEngine();
ce.equal("1.01");
ce.add("12");
System.out.println(ce.getTotalString());
ce.subtract("0.01");
System.out.println(ce.getTotalString());
ce.multiply("100.00000");
System.out.println(ce.getTotalString());
ce.divide("123");
System.out.println(ce.getTotalString());
}
public static class CalculatorEngine {
private enum Operator {
ADD, SUBTRACT, MULTIPLY, DIVIDE
}
private double currentTotal;
public CalculatorEngine() {
currentTotal = 0;
}
public String getTotalString() {
return currentTotal % 1.0 == 0
? Integer.toString((int)currentTotal)
: String.valueOf(currentTotal);
}
public void equal(String number) {
currentTotal = Double.parseDouble(number);
}
public void add(String number) {
convertToDouble(number, Operator.ADD);
}
public void subtract(String number) {
convertToDouble(number, Operator.SUBTRACT);
}
public void multiply(String number) {
convertToDouble(number, Operator.MULTIPLY);
}
public void divide(String number) {
convertToDouble(number, Operator.DIVIDE);
}
public void changeSign(String number) {
Double d = Double.parseDouble(number);
currentTotal = d * (-1);
}
public void dot(String number) {
// todo
}
private boolean isDouble(String number) {
double d = Double.parseDouble(number);
return d % 1.0 != 0;
}
private void convertToDouble(String number, Operator operator) {
double dblNumber = Double.parseDouble(number);
switch (operator) {
case ADD:
add(dblNumber);
break;
case SUBTRACT:
subtract(dblNumber);
break;
case MULTIPLY:
multiply(dblNumber);
break;
case DIVIDE:
divide(dblNumber);
break;
default:
throw new AssertionError(operator.name());
}
}
private void add(double number) {
currentTotal += number % 1.0 == 0 ? (int)number : number;
}
private void subtract(double number) {
currentTotal -= number % 1.0 == 0 ? (int)number : number;
}
private void multiply(double number) {
currentTotal *= number % 1.0 == 0 ? (int)number : number;
}
private void divide(double number) {
currentTotal /= number % 1.0 == 0 ? (int)number : number;
}
}
Results:
13.01
13
1300
10.56910569105691
Upvotes: 1
Reputation: 7956
Why not return a Number
from your method:
private Number convertToNumber(String number) {
double d = Double.parseDouble(number);
if (d % 1.0 == 0) {
return Integer.parseInt(number);
} else {
return d;
}
}
Both Integer
and Double
are subclasses of Number
.
Upvotes: 3
Reputation: 81429
You can't have two different return types or change the method's return type in Java. You can have different method signatures with different return types but then you're calling two separate methods which defeats the purpose of what you are trying to accomplish.
The best you can do in your case is create an object that you pass into the function in which you will set a flag that tells you which value to read. You will also populate either the int value or the double value.
When the method returns the boolean flag will tell the client code which value is the correct one to consume.
class MyConverter {
public String number;
public boolean isInt;
public double doubleVal;
public int intVal;
}
private void convertToNumber(MyConverter conv) {
double d = Double.parseDouble(conv.number);
if (d % 1.0 == 0) {
conv.isInt = true;
conv.intVal = Integer.parseInt(number);
} else {
conv.doubleVal = d;
}
}
Upvotes: 1
Reputation: 488
Best you can do is to create a base class and two classes which extends first class.
IntegerClass has an integer attribute and FloatClass a float atrribute. Then inside the method you instsntiate IntegerClass or FloatClass depending of the String, but you returns the BaseClass.
Then using instanceOf determine which is returned.
Upvotes: 0
Reputation: 63
The signature of your function can only be of one (1) type, either int or double, but not both. You can maybe try to redesign your function to return a boolean if the number has a fractional part (true) or (false) it doesn't.
Please remember that integers can't hold fractional parts, so operations like
int a = 3 / 2;
will set a
to 1
not to 1.5
Upvotes: 3