实验九 异常的抛出捕获并处理
Posted jinnywang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实验九 异常的抛出捕获并处理相关的知识,希望对你有一定的参考价值。
一、
实验代码如下:
public class 图形 {
public static void main(String[] args) {
point p1=new point(0,0);
point p2=new point(1,0);
point p3=new point(0,1);
rectangle r=new rectangle(p1,5,6);
triangle t=new triangle(p1,p2,p3);
point[] point= {p1,p2};
new polygon(point);
point p1=new point(0,0);
point p2=new point(1,0);
point p3=new point(0,1);
rectangle r=new rectangle(p1,5,6);
triangle t=new triangle(p1,p2,p3);
point[] point= {p1,p2};
new polygon(point);
}
}
class point
{
int x;
int y;
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
{
int length;
int width;
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(Math.sqrt(Math.pow((point1.x-point2.x), 2)+Math.pow((point1.y-point2.y), 2))-Math.sqrt(Math.pow((point2.x-point3.x), 2)+Math.pow((point2.y-point3.y), 2))<Math.sqrt(Math.pow((point1.x-point3.x), 2)+Math.pow((point1.y-point3.y), 2)))
if(Math.sqrt(Math.pow((point1.x-point3.x), 2)+Math.pow((point1.y-point3.y), 2))-Math.sqrt(Math.pow((point2.x-point3.x), 2)+Math.pow((point2.y-point3.y), 2))<Math.sqrt(Math.pow((point1.x-point2.x), 2)+Math.pow((point1.y-point2.y), 2)))
if(Math.sqrt(Math.pow((point1.x-point2.x), 2)+Math.pow((point1.y-point2.y), 2))-Math.sqrt(Math.pow((point1.x-point3.x), 2)+Math.pow((point1.y-point3.y), 2))<Math.sqrt(Math.pow((point2.x-point3.x), 2)+Math.pow((point2.y-point3.y), 2)))
throw new IllegalArgumentException("无效参数");
}
}
class polygon extends point
{
public polygon(point[] points)throws IllegalArgumentException
{
int i;
i=points.length;
if(i<=2)
throw new IllegalArgumentException(无效参数");
}
}
}
class point
{
int x;
int y;
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
{
int length;
int width;
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(Math.sqrt(Math.pow((point1.x-point2.x), 2)+Math.pow((point1.y-point2.y), 2))-Math.sqrt(Math.pow((point2.x-point3.x), 2)+Math.pow((point2.y-point3.y), 2))<Math.sqrt(Math.pow((point1.x-point3.x), 2)+Math.pow((point1.y-point3.y), 2)))
if(Math.sqrt(Math.pow((point1.x-point3.x), 2)+Math.pow((point1.y-point3.y), 2))-Math.sqrt(Math.pow((point2.x-point3.x), 2)+Math.pow((point2.y-point3.y), 2))<Math.sqrt(Math.pow((point1.x-point2.x), 2)+Math.pow((point1.y-point2.y), 2)))
if(Math.sqrt(Math.pow((point1.x-point2.x), 2)+Math.pow((point1.y-point2.y), 2))-Math.sqrt(Math.pow((point1.x-point3.x), 2)+Math.pow((point1.y-point3.y), 2))<Math.sqrt(Math.pow((point2.x-point3.x), 2)+Math.pow((point2.y-point3.y), 2)))
throw new IllegalArgumentException("无效参数");
}
}
class polygon extends point
{
public polygon(point[] points)throws IllegalArgumentException
{
int i;
i=points.length;
if(i<=2)
throw new IllegalArgumentException(无效参数");
}
}
二、实验结果
不断改变point的值,实现异常的抛出、捕获并处理
改变point1的值,使之为负
改变rectangle中的的length值,使之为负
改变point1,point2,point3的值,使之构不成三角形
改变polygon中的point[]中的点的数量,使之小于三
以上是关于实验九 异常的抛出捕获并处理的主要内容,如果未能解决你的问题,请参考以下文章