大數據Java基礎第七天作業

第一題:
interface IWhite{
    public void white();
}

interface IRich{
    public void rich();
}

interface IBeauti{
    public void beauti();
}

interface IWRB extends IWhite,IRich,IBeauti{};

class RichMan{
    public void toMarry(IWRB ws){
        ws.white();
        ws.rich();
        ws.beauti();
    }
}

class Marry{
    public static void main(String[] args){
        RichMan rm = new RichMan();
        rm.toMarry(new IWRB(){
            public void white(){
                System.out.println("I'm white !");
            }

            public void rich(){
                System.out.println("I'm rich !");
            }

            public void beauti(){
                System.out.println("I'm beauti !");
            }
        });
    }
}
第二題:
class Trianle{
    private int a;
    private int b;
    private int c;

    public void setEdgeA(int edge){
        this.a = edge;
    }

    public void setEdgeB(int edge){
        this.b = edge;
    }

    public void setEdgeC(int edge){
        this.c = edge;
    }

    public void check() throws Exception{
        int max;
        int other;
        if(a >= b && a >= c){
            max = a;
            other = b + c;
        }else if(b >= a && b >= c){
            max = b;
            other = a + c;
        }else{
            max = c;
            other = a + b;
        }
        if(max > other){
            throw new Exception("三角形邊的長度設置錯誤!");
        }else{
            System.out.println("是三角形!");
        }
    }
}

class ThrowDemo{
    public static void main(String[] args){
        try{
            Trianle trianle = new Trianle();
            trianle.setEdgeA(20);
            trianle.setEdgeA(200);
            trianle.setEdgeC(200);
            trianle.check();
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
第三題:
class InvalidParamExcetion extends Exception{
    public InvalidParamExcetion(String msg){
        super(msg);
        System.out.println(msg);
    }
}

class Person{
    private String birthday;
    
    public String getBirthday(){
        return birthday;
    }

    public void setBirthday(int year,int month,int day) throws InvalidParamExcetion{
        if(year < 1900 || year > 2016){
            throw new InvalidParamExcetion("年份不合適,不存在" + year + "月");
        }
        if(month < 1 || month > 12){
            throw new InvalidParamExcetion("月份不合適,不存在" + month + "月");
        }
        if(day < 1 || day > 31){
            throw new InvalidParamExcetion("日期不合適,不存在" + day + "日");
        }else{
            Boolean monday = (month == 1 || month == 3 || month == 5 || month == 8 || month == 10 || month == 12);
            if(monday && day != 31){
                throw new InvalidParamExcetion("日期不合適,不存在" + day + "日");
            }else if(month == 2 && day > 29){
                throw new InvalidParamExcetion("日期不合適,不存在" + day + "日");
            }else if(month == 2 && year % 4 != 0 && day == 29){
                throw new InvalidParamExcetion("日期不合適,不存在" + day + "日");
            }
        }
        birthday = "生日爲:" + year + "年" + month + "月" + day + "日";
    }
}

class ThrowDemo{
    public static void main(String[] args){
        try{
            Person person = new Person();
            person.setBirthday(2016,2,10);
            System.out.println(person.getBirthday());
        }catch(InvalidParamExcetion e){
            e.printStackTrace();
        }
    }
}
第四題:
當前是在07目錄下
1、創建pack.java文件
package a.b.c.d;
class Pack{
    public static void main(String[] args){
        System.out.println("hello world...");
    }
}
2、創建classes目錄
3、編譯 javac -d classes/ pack.java
4、打包 jar cvfe pack.jar a.b.c.d.Pack -C classes/ .
5、執行 java -jar pack.jar a.b.c.d.Pack
第五題:
1、創建08文件夾
2、cmd進入08文件下執行,java -jar pack.jar a.b.c.d.Pack


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章