栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

梁勇——Java语言程序设计第十章编程练习题10-1&10-3

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

梁勇——Java语言程序设计第十章编程练习题10-1&10-3

10-1 Time类

public class Time {
    private long hour;
    private long minute;
    private long second;

    public Time(){
        long totalMilliseconds = System.currentTimeMillis();

        long totalSeconds = totalMilliseconds/1000;
        long currentSeconds = totalSeconds%60;
        this.second = currentSeconds;

        long totalMinutes = totalSeconds/60;
        long currentMinutes = totalMinutes%60;
        this.minute  = currentMinutes;

        long totalHours = totalMinutes/60;
        long currentHours = totalHours%24;
        this.hour = currentHours;
    }
    public Time(long elapseTime){
        long totalSeconds = elapseTime/1000;

        long currentSeconds = totalSeconds%60;
        this.second = currentSeconds;

        long totalMinutes = totalSeconds/60;
        long currentMinutes = totalMinutes%60;
        this.minute  = currentMinutes;

        long totalHours = totalMinutes/60;
        long currentHours = totalHours%24;
        this.hour = currentHours;
    }

    public Time(long hour, long minute, long second){
        this.hour = hour;
        this.minute = minute;
        this.second = second;
    }

    public long getHour(){
        return hour;
    }

    public long getMinute(){
        return minute;
    }

    public long getSecond(){
        return second;
    }

    public void setTime(long elapseTime){
        long totalSeconds = elapseTime/1000;

        long currentSeconds = totalSeconds%60;
        this.second = currentSeconds;

        long totalMinutes = totalSeconds/60;
        long currentMinutes = totalMinutes%60;
        this.minute  = currentMinutes;

        long totalHours = totalMinutes/60;
        long currentHours = totalHours%24;
        this.hour = currentHours;
    }
}

10.1测试

package chaper_10;

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

        Time object1=new Time();
        Time object2=new Time(555550000);

        System.out.println("object1's Current time is "
                +object1.getHour()+":"+object1.getMinute()+":"+object1.getSecond()+" GMT");

        System.out.println("object2's Current time is "
                +object2.getHour()+":"+object2.getMinute()+":"+object2.getSecond()+" GMT");

    }

}

10.3MyInteger类

package chaper_10;

public class MyInteger {
    private int value;

    public MyInteger(int value) {
        this.value = value;
    }

    public int getValue() {
        return this.value;
    }

    public boolean isEven() {
        if (getValue() % 2 == 0)
            return true;
        else
            return false;
    }

    public boolean isOdd() {
        if (getValue() % 2 != 0) {
            return true;
        } else
            return false;
    }

    public boolean isPrime() {
        for (int detect = 2; detect <= getValue() / 2; detect++) {
            if (getValue() % detect == 0) {
                return false;
            }
        }
        return true;
    }

    public static boolean isEvn(int value) {
        if (value % 2 == 0) {
            return true;
        } else
            return false;
    }

    public static boolean isOdd(int value) {
        if (value % 2 != 0) {
            return true;
        } else
            return false;
    }

    public static boolean isPrime(int value) {
        for (int detect = 2; detect <= value / 2; detect++) {
            if (value % detect == 0) {
                return false;
            }
        }
        return true;
    }

    public boolean isEven(MyInteger myInt) {
        if (myInt.value % 2 == 0)
            return true;
        else
            return false;
    }

    public boolean isOdd(MyInteger myInt) {
        if (myInt.value % 2 % 2 != 0) {
            return true;
        } else
            return false;
    }

    public boolean isPrime(MyInteger myInt) {
        for (int detect = 2; detect <= getValue() / 2; detect++) {
            if (myInt.value % 2 % detect == 0) {
                return false;
            }
        }
        return true;
    }

    public boolean equals(int mode) {
        if (mode == this.value)
            return true;
        else
            return false;
    }

    public boolean equals(MyInteger mode){
        if(mode.value == this.value)
            return true;
        else
            return false;
    }

    public static int parseInt(char[] ch){
        return Integer.valueOf(new String(ch));
    }

    public static  int parseInt(String str){
        return Integer.valueOf(str);
    }
}

10.3测试

package chaper_10;

public class TestForMyInteger {
    public static void main(String[] args){
        MyInteger myInteger = new MyInteger(17);
        MyInteger myInteger1 = new MyInteger(18);

        System.out.println("myInteger is "+myInteger.getValue());

        System.out.println("isEven? " + myInteger.isEven());
        System.out.println("isOdd? " + myInteger.isOdd());
        System.out.println("isPrime? " + myInteger.isPrime());

        System.out.println("isEven? " + myInteger.isEven(myInteger));
        System.out.println("isOdd? " + myInteger.isOdd(myInteger));
        System.out.println("isPrime? " + myInteger.isPrime(myInteger));

        //注意静态方法的调用方式
        System.out.println("isEven? " + MyInteger.isEvn(8));

        System.out.println("change " + MyInteger.parseInt("18"));
        System.out.println("equals? " + myInteger.equals(18));
        System.out.println("equals? " + myInteger.equals(myInteger1));


    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/854658.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号