感谢大家对IT十八掌大数据的支持,今天的作业如下


  1. 利用白富美接口案例,土豪征婚使用匿名内部类对象实现。

2.定义三角形类Trianle,里面包含三个int类型属性,分别表示三条边的长度,

  构造三角形对象时,任意两边之和是否大于第三边,如若不成立,抛出自定义异常。

3.Person类中增加birthday属性,对setBirthday(int ,int , int )方法进行异常处理,

  要求年有效、月有效、日有效、年月日指定的具体日期有效,对不同情况分别抛出不同的异常。

4.将类定义到指定的包下。com.it18zhang,编译之后,打成jar文件。

5.相互之间使用jar包,放置cp下,对class进行重用。

6.设计程序,考查修饰符。public -> protected -> default -> private(选做题)

7.预习多线程。








-------------------------------------------------------------------------------------------

1.利用白富美接口案例,土豪征婚使用匿名内部类对象实现。

答:


//白

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 marry(IWhite i) {

System.out.println("白好");

i.white();

}


public void marry(IRich i) {

System.out.println("富很好");

i.rich();

}


public void marry(IBeauti i) {

System.out.println("美非常好");

i.beauti();

}


public void marry(IWRB i) {

System.out.println("白富美最好不过");

i.white();

i.rich();

i.beauti();

}

}


class MarriyDemo {

public static void main(String[] args) {

RichMan rm = new RichMan();

rm.marry(new IWhite() {

public void white() {

System.out.println("我很白");

}

});

rm.marry(new IRich() {

public void rich() {

System.out.println("我很富");

}

});

rm.marry(new IBeauti() {

public void beauti() {

System.out.println("我很美");

}

});

rm.marry(new IWRB() {

public void white() {

System.out.print("我白");

}


public void rich() {

System.out.print("我富");

}


public void beauti() {

System.out.println("我美");

}

});


}

}



2.定义三角形类Trianle,里面包含三个int类型属性,分别表示三条边的长度,

  构造三角形对象时,任意两边之和是否大于第三边,如若不成立,抛出自定义异常。

答:

class EdgeTooSmallException extends Exception { // 这个异常是用来判断边小于或等于0的

private String info;


public EdgeTooSmallException() {

}


public EdgeTooSmallException(String info) {

this.info = info;

}


public String getInfo() {

return info;

}


public void setEdgeInfo(String info) {

this.info = info;

}

}


class EdgeNotMatchException extends Exception { // 这个异常是用来判断三角形的

private String info;


public EdgeNotMatchException() {


}


public EdgeNotMatchException(String info) {

this.info = info;

}


public String getInfo() {

return info;

}


public void setEdgeNotMatchException(String info) {

this.info = info;

}

}


class Triangle {

//三条边

private int edgea;

private int edgeb;

private int edgec;


// edgea的get/set方法

public int getEdgea() {

return edgea;

}


public void setEdgea(int edgea) throws EdgeTooSmallException {

if (edgea > 0) {

this.edgea = edgea;

} else {

throw new EdgeTooSmallException("边长A不能小于或等于0");

}

}


// edgeb的get/set方法

public int getEdgeb() {

return edgeb;

}


public void setEdgeb(int edgeb) throws EdgeTooSmallException {

if (edgeb > 0) {

this.edgeb = edgeb;

} else {

throw new EdgeTooSmallException("边长B不能小于或等于0");

}

}


// edgec的get/set方法

public int getEdgec() {

return edgec;

}


public void setEdgec(int edgec) throws EdgeTooSmallException {

if (edgec > 0) {

this.edgec = edgec;

} else {

throw new EdgeTooSmallException("边长C不能小于或等于0");

}

}


// 判断是不是三角形

public static void Checking(int x, int y, int z)

throws EdgeNotMatchException {

if (x + y <= z || x + z <= y || y + z <= x) {


throw new EdgeNotMatchException("这不是三角形");

} else {

System.out.println("这是个三角形");

}

}

}


class TriangleDemo {

public static void main(String[] args) {

// 构造三角形

Triangle t1 = new Triangle();


// 设定三条边,找出异常

try {

t1.setEdgea(4);

t1.setEdgeb(5);

t1.setEdgec(3);

t1.Checking(t1.getEdgea(), t1.getEdgeb(), t1.getEdgec());

} catch (EdgeTooSmallException e) {

System.out.println(e.getInfo());

System.exit(-1);

} catch (EdgeNotMatchException e) {

System.out.println(e.getInfo());

System.exit(-1);

}


System.out.println("边长为" + t1.getEdgea() + "," + t1.getEdgeb() + ","

+ t1.getEdgec());


}

}


3.Person类中增加birthday属性,对setBirthday(int ,int , int )方法进行异常处理,

  要求年有效、月有效、日有效、年月日指定的具体日期有效,对不同情况分别抛出不同的异常。

答:

/**

 * Person类中增加birthday属性,对setBirthday(int ,int , int )方法进行异常处理,

 * 要求年有效、月有效、日有效、年月日指定的具体日期有效,对不同情况分别抛出不同的异常。

 * 

 */

public class Brithday {

private String birthday;


public String getBirthday() {

return birthday;

}


public void setBirthday(int year, int month, int date)

throws InvalidParamExcetion {

// 判断年份

if (year < 1900 || year > 2016) {

throw new InvalidParamExcetion("年份不合适,请传入1900年到2016年之间的年份");

}

// 判断月份

if (month <= 0 || month > 12) {

throw new InvalidParamExcetion("月份不合适,不存在" + month + "月");

}

// 判断日期

if (date > 31 || date <= 0) {

throw new InvalidParamExcetion("日期不合适,不存在" + date + "日");

}

boolean isThirdOne = date == 1 || date == 3 || date == 5 || date == 7

|| date == 8 || date == 10 || date == 12;

if (!isThirdOne && date == 31) {

throw new InvalidParamExcetion("日期不合适," + month + "月不存在" + date

+ "日");

}

if (month == 2 && date > 29) {

throw new InvalidParamExcetion("日期不合适,2月不存在" + date + "日");

}

if (year % 4 != 0 && month == 2 && date == 29) {

throw new InvalidParamExcetion("日期不合适," + year + "不是闰年,所以2月不存在"

+ date + "日");

}


System.out.println("生日为:" + year + "年" + month + "月  " + date + "日 ");

}


public static void main(String[] args) throws InvalidParamExcetion {

Brithday person = new Brithday();

try {

person.setBirthday(2015, 12, 5);

person.setBirthday(2016, 2, 29);

person.setBirthday(2015, 2, 29);

// 因为上一句异常了,所以不会执行这一句

person.setBirthday(2015, 3, 5);

} catch (InvalidParamExcetion e) {

}


}


}


/**

 * 非法参数异常

 * 

 */

class InvalidParamExcetion extends Exception {


public InvalidParamExcetion(String msg) {

super(msg);

System.out.println(msg);

}


}



4.将类定义到指定的包下。com.it18zhang,编译之后,打成jar文件。

答:

打包java库文件。

[将类路径下的类打成jar文件]

jar cvf myjar.jar -C classes/ .

jar cvfe myjar.jar com.it18zhang.A -C classes/ .//e指定的入口点.


[使用java -jar参数来运行程序]

java -jar myjar.jar//执行jar文件

java -jar myjar.jarcom.it18zhang.A//执行jar文件指定入口点。


5.相互之间使用jar包,放置cp下,对class进行重用。

答:答案略

6.设计程序,考查修饰符。public -> protected -> default -> private(选做题)

答:

package com.it18zhang.job.demo;


public class Person {

protected String protectName;

public void method_public() {

System.out.println("Person method_public");

}

protected void method_protect(){

System.out.println("Person method_protect");

}

void method_defult(){

System.out.println("Person method_defult");

}

private void method_private(){

System.out.println("Person method_private");

}


}



package com.it18zhang.job.demo;


public class People {

public static void main(String[] args) {

System.out.println("同包中的类修饰符权限测试");

Person person = new Person();

person.method_public();

person.method_protect();

person.method_defult();

//无法调用private

//person.method_private();

}


}



package com.it18zhang.job.demo2;


import com.it18zhang.job.demo.Person;




public class Student extends Person{


public static void main(String[] args) {

System.out.println("子类修饰符权限测试");

Student student = new Student();

student.method_public();

student.method_protect();


//无法调用defult、private

//student.method_private();

//student.method_defult();

}


}




package com.it18zhang.job.demo2;


import com.it18zhang.job.demo.Person;


public class Other {


public static void main(String[] args) {

System.out.println("其他包中类修饰符权限测试");

Person person = new Person();

person.method_public();

//无法调用其他包中类的protect,default和private方法

//person.method_protect();

//person.method_defult();

//person.method_private();

}


}



7.预习多线程。