7初级 - Java的数据类型-练习

161 阅读4分钟

7-1

package com.github.hcsp.datatype;
public class MaxNumbers {
    // byte类型能存储的最大值是?
    // The maximum value a byte type can store
    public static byte maxByte = Byte.MAX_VALUE;

    // short类型能存储的最大值是?
    // The maximum value a short type can store
    public static short maxShort = Short.MAX_VALUE;

    // int类型能存储的最大值是?
    // The maximum value an int type can store
    public static int maxInt = Integer.MAX_VALUE;

    // long类型能存储的最大值是?
    // The maximum value an long type can store
    public static long maxLong = Long.MAX_VALUE;

    // float类型能存储的最大值是?
    // The maximum value an float type can store
    public static float maxFloat = Float.MAX_VALUE;

    // double类型能存储的最大值是?
    // The maximum value an double type can store
    public static double maxDouble = Double.MAX_VALUE;

    // char类型能存储的最大值是?
    // The maximum value an char type can store
    public static char maxChar = Character.MAX_VALUE;

    public static void main(String[] args) {
        System.out.println("maxByte=" + maxByte);
        System.out.println("maxByte+1=" + (maxByte + 1));
        System.out.println("maxShort=" + maxShort);
        System.out.println("maxShort+1=" + (maxShort + 1));
        System.out.println("maxInt=" + maxInt);
        System.out.println("maxInt+1=" + (maxInt + 1));
        System.out.println("maxLong=" + maxLong);
        System.out.println("maxLong+1=" + (maxLong + 1));
        System.out.println("maxFloat=" + maxFloat);
        System.out.println("maxFloat+1=" + (maxFloat + 1));
        System.out.println("maxDouble=" + maxDouble);
        System.out.println("maxDouble+1=" + (maxDouble + 1));
        System.out.println("maxChar=" + maxChar);
        System.out.println("maxChar+1=" + (maxChar + 1));
    }
}

7-2

package com.github.hcsp.datatype;

public class IntegerOverflow {
    // 修复这个方法里的问题,使得它正确输出 "i=3000000000"
    // Fix this method to make it output "i=3000000000"
    public static void main(String[] args) {
        int 十亿 = 10_0000_0000;
        long i = 0;
        i = i + 十亿;
        System.out.println("i=" + i);
        i = i + 十亿;
        System.out.println("i=" + i);
        i = i + 十亿;
        System.out.println("i=" + i);
    }
}

7-3

package com.github.hcsp.datatype;

public class Cast {
    // Cast an int to byte
    // 将int类型转换成byte
    public static byte int2byte(int i) {
        return (byte) i;
    }

    // Cast an int to short
    // 将int类型转换成short
    public static short int2short(int i) {
        return (short) i;
    }

    // Cast an int to char
    // 将int类型转换成char
    public static char int2char(int i) {
        return (char) i;
    }

    // Cast an int to String, e.g. 123 -> "123"
    // 将一个整数转换为字符串,例如,将123转换成字符串"123"
    public static String int2String(int i) {
        return Integer.toString(i);
    }

    // Cast an String to int, e.g. "123" -> 123
    // 将一个字符串转换成整数,例如,将字符串"123"转换成整数123
    public static int string2int(String s) {
        return Integer.parseInt(s);
    }

    // Cast an String to double, e.g. "1.23" -> 1.23
    // 将一个字符串转换成double类型,例如,将字符串"1.23"转换成1.23
    public static double string2double(String s) {
        return Double.parseDouble(s);
    }

    // Cast an Long to int
    // 将Long类型转换成int
    public static int Long2int(Long l) {
        return l.intValue();
    }

    // Cast an Double to long
    // 将Double类型转换成long
    public static long double2Long(Double d) {
        return d.longValue();
    }

}

7-4

package com.github.hcsp.datatype;

public class Main {
    // 我们希望实现一个除法,但是这里的结果有明显的错误
    // 例如 3/2 == 1.0
    // 请修复此问题,让3/2的结果为正确的1.5
    // 不要修改方法的参数类型
    public static double divide(int a, int b) {
        return 1.0 * a / b;
    }

    public static void main(String[] args) {
        System.out.println("3/2=" + divide(3, 2));
    }
}

7-5

package com.github.hcsp.datatype;

public class Main {
    public static void main(String[] args) {
        // 这里应该输出true,实际输出的却是false
        // 请查找、思考并修复doubleEquals方法中的问题
        System.out.println(doubleEquals(0.1 + 0.2, 0.3));
    }

    // 判断两个double是否相等
    public static boolean doubleEquals(double a, double b) {
        return Math.abs(b - a) < 0.00001;
    }
}

7-2-1

package com.github.hcsp.datatype;

public class Light {
    // 一盏灯可能有三种状态:
    // 亮 -> true
    // 灭 -> off
    // 未知 -> null
    Boolean on;

    public Light(Boolean on) {
        this.on = on;
    }

    // 当灯亮时返回true,灭和未知状态返回false
    public boolean isOn() {
        if (this.on == null) {
            return false;
        }
        return on;
    }

    public Boolean isOnRawValue() {
        return on;
    }
}

7-2-2

package com.github.hcsp.datatype;

public class Main {
    public static void main(String[] args) {
        System.out.println(add("123", "456"));
        System.out.println(add("123", null));
        System.out.println(add(null, null));
    }
    // 给出两个数字字符串a和b,返回其中的数字相加后的字符串结果。
    // 例如,给定a="123",b="456",返回"579",因为123+456=579
    // 注意,若参数为null,则当作0处理,即add("123", null)=="123", add(null, null)=="0"
    public static String add(String a, String b) {
        String a1;
        String b1;
        if (a == null) {
            a1 = "0";
        } else {
            a1 = a;
        }
        if (b == null) {
            b1 = "0";
        } else {
            b1 = b;
        }
        return String.valueOf(Integer.parseInt(a1) + Integer.parseInt(b1));
    }
}


7-3

package com.github.hcsp.datatype;

public class Main {
    // 修复compare方法,使得main方法不再抛出空指针异常
    public static void main(String[] args) {
        System.out.println(compare(123, 456));
        System.out.println(compare(123, 123));
        System.out.println(compare(123, null));
    }

    // 比较一个int和一个Integer是否相等
    // 当且仅当它们代表的整数相等时,此方法返回true
    // 不要修改本方法参数的类型
    public static boolean compare(int a, Integer b) {
        if (b == null) {
            return false;
        }
        return a == b;
    }
}

7-4

package com.github.hcsp.datatype;

public class Main {
    public static void main(String[] args) {
        // 这里应该输出两个true,实际输出的却是两个false
        // 请查找、思考并修复numberEquals方法中的问题
        System.out.println(numberEquals("1234", "+1234"));
        System.out.println(numberEquals("1234", "1234"));
    }

    // 判断两个字符串是否包含相等的数字
    // 例如a为"+1234",b为"1234",返回true
    public static boolean numberEquals(String a, String b) {
        return Integer.valueOf(a).equals(Integer.valueOf(b));
    }
}