Difference between revisions of "Maximum Tutorial"

From Progzoo
Jump to: navigation, search
(Commission Charges)
(Commission Charges)
 
Line 59: Line 59:
 
{
 
{
 
   System.out.printf("amount:%7.2f\tcommission:%7.2f\n",i,commission(i));
 
   System.out.printf("amount:%7.2f\tcommission:%7.2f\n",i,commission(i));
 +
}
 
}
 
}
 
QcQ
 
QcQ

Latest revision as of 11:26, 25 September 2012

The max function takes two numbers and returns the largest.

int x = Math.max(3,5);

In the above example x will be set to 5.

Of course there is also a function Math.min

Taxi charges

A taxi ride costs £2.00 per mile, but there is a minimum charge of £3.00 for short distances.



[Font] [Default] [Show] [Resize] [History] [Profile]

Commission Charges

The commission charged for converting currency is 10% of the amount or £5, which ever is the larger.

We can use the function Math.max to solve this problem. The function, which is part of the core will return the larger of two numbers.

We do not need to use the if statement here (but you can if you want to)



[Font] [Default] [Show] [Resize] [History] [Profile]