Difference between revisions of "Maximum Tutorial"

From Progzoo
Jump to: navigation, search
(New page: ==Commission Charges== <question className="P" title="Commission Charges"> <blurb> <p> The commission charged for converting currency is 10% of the amount or £5, which ever is the larger....)
 
(Commission Charges)
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
==Commission Charges==
+
{{Numeric Functions Tutorial}}
<question className="P" title="Commission Charges">
+
The max function takes two numbers and returns the largest.
<blurb>
+
int x = Math.max(3,5);
<p>
+
In the above example x will be set to 5.
The commission charged for converting currency is 10% of the amount
+
 
or £5, which ever is the larger.
+
Of course there is also a function Math.min
</p>
 
<p>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.
 
</p>
 
<p>We do not need to use the if statement here (but you can if you want to)
 
</p>
 
</blurb>
 
<shell lang="java" className="P">
 
public class P{
 
public static void main(String [] argv){
 
double [] ol = {20.0, 50.0, 100.0};
 
for (double i: ol)
 
System.out.printf("amount:%7.2f\tcommission:%7.2f\n",i,commission(i));
 
}
 
QcQ
 
}</shell>
 
<prog lang="java">
 
static double commission(double amount){
 
  return Math.max(10*amount/100,7.0);
 
}
 
</prog>
 
<answer lang="java">
 
static double commission(double amount){
 
  return Math.max(amount*0.1,5.0);
 
}
 
</answer>
 
</question>
 
 
==Taxi charges==
 
==Taxi charges==
 
<question className="P" title="Taxi charges">
 
<question className="P" title="Taxi charges">
Line 44: Line 16:
 
public class P{
 
public class P{
 
QcQ
 
QcQ
public static void main(String [] argv){
+
public static void main(String [] argv)
 +
{
 
double [] ol = {1.0, 2.0, 3.0, 4.0, 5.0};
 
double [] ol = {1.0, 2.0, 3.0, 4.0, 5.0};
 
for (double i: ol)
 
for (double i: ol)
Line 51: Line 24:
 
}</shell>
 
}</shell>
 
<prog lang="java">
 
<prog lang="java">
static double fare(double miles){
+
static double fare(double miles)
 +
{
 
   return Math.max(miles,2.0);
 
   return Math.max(miles,2.0);
 
}
 
}
 
</prog>
 
</prog>
 
<answer lang="java">
 
<answer lang="java">
static double fare(double miles){
+
static double fare(double miles)
 +
{
 
   return Math.max(miles*2.0,3.0);
 
   return Math.max(miles*2.0,3.0);
 
}
 
}
 
</answer>
 
</answer>
 
</question>
 
</question>
==Wicked Employers==
+
 
<question className="P" title="Wicked Employers">
+
==Commission Charges==
 +
<question className="P" title="Commission Charges">
 
<blurb>
 
<blurb>
 
<p>
 
<p>
Academics are offered a pay ride of 3% or the rate of inflation,
+
The commission charged for converting currency is 10% of the amount
which ever is the lower.
+
or £5, which ever is the larger.
 +
</p>
 +
<p>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.
 
</p>
 
</p>
<p class="imper">Give the miserly salary of <code>salary</code>
+
<p>We do not need to use the if statement here (but you can if you want to)
and in inflation rate of <code>rpi</code>; calculate the pay rise.
 
 
</p>
 
</p>
 
</blurb>
 
</blurb>
 
<shell lang="java" className="P">
 
<shell lang="java" className="P">
 
public class P{
 
public class P{
QcQ
+
public static void main(String [] argv)
public static void main(String [] argv){
+
{
double [] ol = {1.0, 2.0, 3.0, 4.0, 5.0};
+
double [] ol = {20.0, 50.0, 100.0};
double s=30000.0;
 
 
for (double i: ol)
 
for (double i: ol)
System.out.printf("Salary: &amp;#163;%,9.2f RPI:%4.1f%% Pay rise:%7.2f\n",
+
{
s,i,payRise(s,i));
+
  System.out.printf("amount:%7.2f\tcommission:%7.2f\n",i,commission(i));
 +
}
 
}
 
}
 +
QcQ
 
}</shell>
 
}</shell>
 
<prog lang="java">
 
<prog lang="java">
static double payRise(double salary, double rpi){
+
static double commission(double amount)
   return Math.min(rpi,3.0);
+
{
 +
   return Math.max(10*amount/100,7.0);
 
}
 
}
 
</prog>
 
</prog>
 
<answer lang="java">
 
<answer lang="java">
static double payRise(double salary,double rpi){
+
static double commission(double amount)
   return salary*Math.min(rpi,3.0)/100;
+
{
}</answer>
+
   return Math.max(amount*0.1,5.0);
 +
}
 +
</answer>
 
</question>
 
</question>

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]