+ 2
How do you perform a switch statement with percentages and calculations?
if you were to have methods in the subclass called medical allowance that categorizes 0 child - 5% , 1 child - 8% , 2 children - 10% , 3 children - 12% and more than 3 children - 15% ,all of them must be calculated with the monthly basic salary which is 5500 ,how does one perform such calculation in the switch statement?
2 Answers
+ 9
switch (numChild):
case 0:
return 5; // 5%
case 1:
return 8; // 8%
etc...
then take the percentage and do the calculations you want.
Though, there's a faster method as you've developed the following formula:
if(numChild > 3)
return 15; // 15%
return (1/6)*(++numChild^3 - 9*numChild^2 + 38*numChild--);
That would return the percent you're looking for (same as the switch statement, but mathematically).
I incremented numChild for the equation to start at 1, decrementing it back encase of wanted results. If the equation is to overwhelming just forget it and refer back to the switch statements.
+ 3
wow it works, thanks a million