Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For more control, try the basic OOP concept of <strong>overloading methods</strong> like</p> <pre><code>public float addition(float numa, float numb) { // will return float return numa + numb; } public int addition(int numa, float numb) { // explicitly cast to int return numa + (int) numb; } public int addition(float numa, int numb) { // explicitly cast to int return (int) numa + numb; } public int addition(int numa, int numb) { // will return int return numa + numb; } </code></pre> <p>To examin your in put, try something like this...</p> <pre><code>public void examineInput(String input1, String input2) { // For both are float if (input1.indexOf(".") != -1 &amp;&amp; input2.indexOf(".") != -1) { float numa = Float.parseFloat(input1); float numb = Float.parseFloat(input2); float ans = addition(numa, numb); Log.i(TAG, String.format("%f + %f = %f", numa, numb, ans)); } // for first to be int and second to be float else if (input1.indexOf(".") == -1 &amp;&amp; input2.indexOf(".") != -1) { int numa = Integer.parseInt(input1); float numb = Float.parseFloat(input2); int ans = addition(numa, numb); Log.i(TAG, String.format("%d + %f = %d", numa, numb, ans)); } // for first to be float and second to be int else if (input1.indexOf(".") != -1 &amp;&amp; input2.indexOf(".") == -1) { float numa = Float.parseFloat(input1); int numb = Integer.parseInt(input2); int ans = addition(numa, numb); Log.i(TAG, String.format("%f + %d = %d", numa, numb, ans)); } // for both to be int else if (input1.indexOf(".") == -1 &amp;&amp; input2.indexOf(".") == -1) { int numa = Integer.parseInt(input1); int numb = Integer.parseInt(input2); int ans = addition(numa, numb); Log.i(TAG, String.format("%d + %d = %d", numa, numb, ans)); } } </code></pre> <p>And the is the input to test this code, with output</p> <pre><code>examineInput("5.2", "6.2"); // 5.200000 + 6.200000 = 11.400000 examineInput("5", "3.6"); // 5 + 3.600000 = 8 examineInput("1.6", "5"); // 1.600000 + 5 = 6 examineInput("5", "5"); // 5 + 5 = 10 </code></pre> <p><strong>Note:</strong> you need to verify that examineInput always get valid numbers, not strings of non numaric characters...</p> <p>Hope this helps to improve OOP concepts as well..:)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload