【教学目的和要求】
1、掌握函数的定义和调用方法
2、掌握重载函数的使用
3、掌握缺省参数的函数使用
4、了解系统函数的使用
【教学组织】
1、编写一个函数把华氏温度转换为摄氏温度,转换公式为:C=(F-32)*5/9。
2、编写重载函数max1可分别求取两个整数,三个整数,两个双精度数,三个双精度数的最大值。
3、给求取两个整数最大值的重载函数max1赋缺省参数值0,并利用该函数来判断某个整数是否大于0,比如max1(-6)。
4、使用系统函数pow(x,y)计算xy的值。
package co.cn.com;
public class shiyan2 {
public void tran(double f){
double c=(f-32)*5/9;
System.out.println(c);
}
void max1(int m,int n){
System.out.println(m>n?m:n);
}
void max1(int m,int n,int s){
int t=0;
t=m>n?m:n;
t=t>s?t:s;
System.out.println(t);
}
void max1(double m,double n){
System.out.println(m>n?m:n);
}
void max1(double m,double n,double s){
double t=0;
t=m>n?m:n;
t=t>s?t:s;
System.out.println(t);
}
/*void max1(int m,int n=0){
System.out.println(m>n?m:n);
}*/
public long pow(int m, int n) {
// TODO Auto-generated method stub
int i=0;
long s=1;
for(;i<n;i++)
{
s=s*m;
}
return s;
}
public static void main(String args[]){
shiyan2 s=new shiyan2();
s.max1(2, 3);
s.max1(2, 4, 3);
s.max1(2.5, 8.7, 7.8);
System.out.println(s.pow(2,20));
System.out.println(Math.pow(2, 20));
}
}