实验九

package yichang;

public class ShiYan {


public static void main(String[] args) {
point p=new point(1,3);
point p1=new point(1,2);
point p2=new point(1,1);
rectangle r=new rectangle(p,5,6);
triangle t=new triangle(p,p1,p2);
}

}
class point {
public int x,y;
public point() {}
public point(int x,int y)throws IllegalArgumentException
{
this.x=x;
this.y=y;

if(x<0||y<0)
throw new IllegalArgumentException("无效参数");
}
}
class rectangle extends point{
public int width,length;
//public point point1(3,6);
public rectangle(point point1,int length,int width)throws IllegalArgumentException
{

this.length=length;
this.width=width;
if(length<0||width<0)
throw new IllegalArgumentException("参数无效");
}
}
class triangle extends point{
public triangle(point point1,point point2,point point3)throws IllegalArgumentException
{
if(((point1.x-point2.y)-(point2.x-point1.y))+((point2.x-point3.y)-(point3.x-point2.y))+((point3.x-point1.y)-(point3.y-point1.x))==0)
throw new IllegalArgumentException("无效的参数");
}
}

 

 

 

实验结果:

 

 实验心得:

通过本次实验,我熟悉掌握了异常的抛出、捕获、处理的方法,但在过程中遇到了一些麻烦,在同学的帮助下解决了这些问题。

posted @ 2019-05-26 23:11  菜包子666  阅读(130)  评论(1编辑  收藏  举报