java 实验2 函数与重载

4 篇文章 0 订阅
4 篇文章 0 订阅

教学目的和要求

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));
	}
	
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值