梁勇——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));


    }
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值