自学Java第15Day

学习目标:面向对象进阶


学习内容:常用API


学习时间: 3 点-下午 6 点


学习产出:

1.Math

方法名说明
public static int abs(int a)获取参数绝对值
public static double ceil(double a)向上取整
public static double floor(double a)向下取整
public static int  round(float a)四舍五入
public static int max(int a,int b)获取两个int值中的较大值
public static double pow(double a,double b)返回a的b次幂的值
public static double random()返回值为double的随机值,范围[p.0,1.0)
public class math11 {
    public static void main(String[] args) {

        //abs 获取参数绝对值
        System.out.println(Math.abs(88));//88
        System.out.println(Math.abs(-88));//88
        //bug:
        //以int类型为例,取值范围 :- 2147483648
        //如果没有正数与负数对应,那么传递负数结果有误
        //-2147483648 没有正数与之对应,所以abs结果产生bug
        //System.out.println(Math.abs(-2147483647)) ;//2147483647
        //System.out.println(Math.absExact(-2147483648));

        System.out.println(Math.ceil(12.34));//13.0
        System.out.println(Math.ceil(12.54));//13.0
        System.out.println(Math.ceil(-12.34));//-12.0
        System.out.println(Math.ceil(-12.54));//-12.0

        System.out.println(Math.floor(12.34));//12.0
        System.out.println(Math.floor(12.54));//12.0
        System.out.println(Math.floor(-12.34));//-13.0
        System.out.println(Math.floor(-12.54));//-13.0

        // 四舍五入
        System.out.println(Math.round(12.34));//12
        System.out.println(Math.round(12.54));//13
        System.out.println(Math.round(-12.34));//-12
        System.out.println(Math.round(-12.54));//-13

        // 获取两个整数的较大值
        System.out.println(Math.max(20, 30));//30
        // 获取两个整数的较小值
        System.out.println(Math.min(20, 30));//20
        //获取a的b次幂
        System.out.println(Math.pow(2, 3));//8
        //细节:
        //如果第二个参数 0~1之间的小数
        System.out.println(Math.pow(4,0.5));//2.0
        System.out.println(Math.pow(2,-2));//0.25
        
        //建议:
        //第二个参数:一般传递大于等于1的正整数。
        System.out.println(Math.sqrt(4));//2.0
        System.out.println(Math.cbrt(8));//2.0

        for (int i = 0; i < 10; i++) {
            System.out.println(Math.floor(Math.random() * 100) + 1);
            //Math.random() [0.0 1.0)
            //*100          [0.0 100.0)
            //floor         去掉了后面的小数
            //+1            [1 100.0]
        }
    }
}

 练习:算法水题一

        判断一个数是否为一个质数

public class math {
    public static void main(String[] args) {
        System.out.println(isPrime(997));

    }
    public static boolean isPrime(int number){
        for (int i = 2; i <Math.sqrt(number) ; i++) {
            if(number%i==0){
                return false;
            }
        }
        return true;
    }
}

 练习:算法水题二

        自幂数,一个n位自然数等于自身各个数位上数字的n次幂之和。

        举例1:三位数    1^3 + 5^3 + 3^3 = 153

        举例2:四位数    1^4 + 6^4 + 3^4 + 4^3 = 1634

                如果自幂数是一位数,也叫做:独身数

                三位自幂数:水仙花数            四位自幂数:四叶玫瑰数

                五位自幂数:五角星数            六位自幂数:六合数

                七位自幂数:北斗七星数         八位自幂数:八仙数

                九位自幂数:九九重阳数         十位自幂数:十全十美数

        要求:统计一共有多少个水仙花数。

public class math2 {
    public static void main(String[] args) throws IOException {
        int count=0;
        for (int i = 100; i <=999 ; i++) {
            int ge=i%10;
            int shi=i/10%10;
            int bai=i/100;
            double sum=Math.pow(ge,3)+Math.pow(shi,3)+Math.pow(bai,3);
            if(sum==i){
                count++;
            }
        }
        System.out.println(count);
        System.out.println(Runtime.getRuntime().availableProcessors());
        System.out.println(Runtime.getRuntime().freeMemory()/1024/1024);
        Runtime.getRuntime().exec("shutdown -a");
    }
}

2.System

方法名说明
public static void exit(int status)终止当前运行的 Java 虚拟机
public static long currentTimeMillis()返回当前系统的时间毫秒值形式
public static void arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数)贝拷组数

计算机中的时间原点

1970年1月1日   00:00:00

 原因:
        1969年8月,贝尔实验室的程序员肯汤普逊利用妻儿离开一个月的机会。开始着手创造一个全新的革命性的操作系统。他使用B编译语言在老旧的PDP-7机器上开发出了Unix的一个版本。随后,汤普逊和同事丹尼斯里奇改进了B语言,开发出了C语言,重写了UNIX。

1970年1月1日算C语言的生日 

 拷贝数组练习:

public class math11 {
    public static void main(String[] args) {

        //拷贝数组
        int[] arr1 = {1,2,3,4,5,6,7,8,9,10};
        int[] arr2 = new int[10];
//把arr1数组中的数据拷贝到arr2中
//参数一:数据源,要拷贝的数据从哪个数组而来
//参数二:从数据源数组中的第几个索引开始拷贝
//参数三:目的地,我要把数据拷贝到哪个数组中
//参数四:目的地数组的索引。
//参数五:拷贝的个数
//System.arraycopy(arr1,0, arr2, 0,5);

//arr2 : 0 0 0 0 1 2 3 0 0
        System.arraycopy(arr1, 0, arr2,  4, 3);

//验证
        for (int i = 0; i < arr2.length; i++) {
            System.out.print(arr2[i] + "");
        }
    }
}

运行结果:

0000123000

 

public class math11 {
    public static void main(String[] args) {
        //public static void arraycopy(数据源数组,起始索引,目的地数组,起始索引,拷贝个数)
        //细节:
        //1.如果数据源数组和目的地数组都是基本数据类型,那么两者的类型必须保持一致,否则会报错
        //2.在拷贝的时候需要考虑数组的长度,如果超出范围也会报错
        //3.如果数据源数组和目的地数组都是引用数据类型,那么子类类型可以赋值给父类类型

        //数组拷贝

        Student s1 = new Student("zhangsan",  23);
        Student s2 = new Student( "lisi",  24);
        Student s3 = new Student("wangwu", 25);

        Student[] arr1 = {s1, s2, s3};
        Person[] arr2 = new Person[3];
        //把arr1中对象的地址值赋值给arr2中
        System.arraycopy(arr1, 0, arr2, 0, 3);

        //遍历数组arr2
        for (int i = 0; i < arr2. length; i++){
            Student stu = (Student) arr2[i];
            System.out.println(stu.getName() + ", "+ stu.getAge());

        }
    }
}

运行结果:

zhangsan, 23
lisi, 24
wangwu, 25

Runtime

方法名说明
public static     Runtime getRuntime()当前系统的运行环境对象
public void     exit(int status)停止虚拟机
public int     availableProcessors()获得CPU的线程数
public long     maxMemory ()JVM能从系统中获取总内存大小(单位byte)
public long     totalMemory()JVM已经从系统中获取总内存大小(单位byte)
public long     freeMemory ()JVM剩余内存大小(单位byte)
public Process exec(String command)运行cmd命令
public class math11 {
    public static void main(String[] args) throws IOException {
        //1.获取Runtime的对象
        Runtime r1 = Runtime.getRuntime();

        //2.exit 停止虚拟机
        Runtime.getRuntime().exit(0);

        //3.获得CPU的线程数
        System.out.println(Runtime.getRuntime().availableProcessors());//8

        //4.总内存大小,单位byte字节
        System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024);//4064

        //5.已经获取的总内存大小,单位byte字节
        System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024);//254

        //6.剩余内存大小
        System.out.println(Runtime.getRuntime().freeMemory() / 1024 / 1024);//251

        //7.运行cmd命令
        //shutdown:关机
        //加上参数才能执行
        //-s:默认在1分钟之后关机
        //-s -t 指定时间:指定关机时间
        //-a:取消关机操作
        //-r: 关机并重启
        Runtime.getRuntime().exec(  "shutdown -s -t 3600");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值