第九次实验+第八次实验

实验九:异常的抛出、捕获并处理

实验程序 

import java.util.Scanner;

public class Point {
static int x;
static int y;
Point(int x,int y){
Point.x=x;
Point.y=y;
}
@SuppressWarnings("serial")
public static void main(String[] args) {
try {
@SuppressWarnings({ "unused", "resource" })
Scanner shuru=new Scanner(System.in);
System.out.println("请输入一个点:");
int h=shuru.nextInt();
int a=shuru.nextInt();

@SuppressWarnings("unused")
Point s=new Point(h,a);
if(x>=0&&y>=0) {
System.out.println("x为:"+Point.x+"y为:"+Point.y);
}
else {
throw new Exception() {
public String toString() {
return "无效参数";
}
};
}
}
catch(Exception e){
System.out.println(e.toString());
}
finally {
System.out.println("程序结束");

}
}

}

实验结果

请输入一个点

1   2

x为1     y为2

程序结束

 

import java.util.Scanner;

public class Rectangle {
static int length;
static int width;
Rectangle(Point point1,int length,int width){
Rectangle.length=length;
Rectangle.width=width;
}
@SuppressWarnings("serial")
public static void main(String[] args) {
try {
@SuppressWarnings("resource")
Scanner shuru=new Scanner(System.in);

System.out.println("请输入一个点:");
int h=shuru.nextInt();
int a=shuru.nextInt();

System.out.println("请输入长方形的长:");
int b=shuru.nextInt();

System.out.println("请输入长方形的宽:");
int c=shuru.nextInt();
Point p1=new Point(h,a);
@SuppressWarnings("unused")
Rectangle m=new Rectangle(p1,b,c);
if(length>=0&&width>=0) {
System.out.println("这是一个长为"+Rectangle.length+"宽为"+Rectangle.width+"的长方形" );
}
else {
throw new Exception() {
public String toString() {
return "无效参数";
}
};
}
}
catch(Exception e){
System.out.println(e.toString());
}
finally {
System.out.println("程序结束");

}
}

}

实验结果:

请输入一个点

1   2

请输入长方形的长

4

请输入长方形的宽

3

这是一个长为4宽为3的长方体

程序结束

 

 

 

import java.util.Scanner;

public class Polygon {
Polygon(Point[] points) {

}
@SuppressWarnings("serial")
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner shuru=new Scanner(System.in);
System.out.println("请输入n");
int n=shuru.nextInt();
int Point[]=new int[n];
int i;
for(i=0;i<n;i++) {
System.out.println("请输入多边形的第"+i+"个点");
int h1=shuru.nextInt();
int a1=shuru.nextInt();
@SuppressWarnings("unused")
Point p2 = new Point(h1,a1);

try {
// Point s=new Point(-1,2);
if(Point.length>=3)
{
System.out.println("这可以构成一个多边形");
}
else {
throw new Exception() {
public String toString() {
return "无效参数";
}
};
}
}
catch(Exception e){
System.out.println(e.toString());
}
finally {
System.out.println("程序结束");

}
}
}

 实验结果:

请输入n

4

请输入多边形的第0个点

1  2

请输入多边形的第1个点

1   6

请输入多边形的第2个点

2    5

请输入多边形的第3个点

2    7

这个可以构成多边形

实验结束

 

 

 

import java.util.Scanner;

public class Triangle {
@SuppressWarnings("unused")
private static final Point Point = null;
public Point point2,point3;
protected double a,b,c;
public Triangle(Point p1,Point p2,Point p3) {
//super("三角形",p1);
this.point2=p2;
this.point3=p3;

}
public Triangle() {
}
@SuppressWarnings("serial")
public static void main(String[] args) {

try {
@SuppressWarnings("resource")
Scanner shuru=new Scanner(System.in);

System.out.println("请输入一个点:");
int h=shuru.nextInt();
int a=shuru.nextInt();
Point p1 = new Point(h,a);

System.out.println("请输入一个点:");
int h1=shuru.nextInt();
int a1=shuru.nextInt();
Point p2 = new Point(h1,a1);

System.out.println("请输入一个点:");
int h2=shuru.nextInt();
int a2=shuru.nextInt();
Point p3 = new Point(h2,a2);
@SuppressWarnings("unused")
Triangle s = new Triangle(p1, p2, p3);
double g=(h1-h)/(a1-a);
double p=(h2-h1)/(a2-a1);
if(g!=p) {
System.out.println("这三个点可以构成一个三角形");
}
else{
throw new Exception() {
public String toString() {
return "无效参数";
}
};
}
}
catch(Exception e){
System.out.println(e.toString());
}
finally {
System.out.println("程序结束");

}
}

}

实验结果:

请输入一个点

2     3

请输入一个点

3     6

请输入一个点

3     5

这个可以构成三角形

请输入一个点

1   2

请输入一个点

 2    1

请输入一个点

3    3

这个可以构成三角形

程序结束

 

实验心得:

1.抛出异常提示为java.lang.illegalArguementException

2.抛出异常是一种检验程序异常终止的手段

3.通过抛出异常使程序正常运行

 

 

实验八:接口与实现接口的类

实验程序

public class interface_shiyan {  

public static void main (String[] arges){  

yuanzhui a=new yuanzhui(2,5,6);  

yuanzhui b=new yuanzhui(1,3,3);  

System.out.println(a.Area());

 System.out.println(b.Area());  

System.out.println(a.volume());  

System.out.println(b.volume());  

System.out.println("体积较大的是:"+Math.max(a.volume(), b.volume()));  

}

}

class yuanzhui implements Volume,Area{  

protected double r;

 protected double l;  

protected double h;  

public yuanzhui(double r,double l,double h){

 this.r=r;

 this.l=l;  

this.h=h;

 }  

public double volume(){  

return Math.PI*Math.pow(r,2)*h/3;

 }  

public double Area(){  

return Math.PI*this.r*this.r+this.r*this.l;

 } }

interface Volume{

 public double volume();

}

interface Area{

 public double Area();

二.实验心得        
通过本次实验的调试,使我清楚的认识到接口在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明。一个类通过继承接口的方式从而来继承接口的抽象方法。并且接口并不是类,编写接口的方式和类很相似,但是它们属于不同的概念。类描述对象的属性和方法。接口则包含类要实现的方法。 

 

 

posted @ 2019-05-27 12:51  嗯嗯¥  阅读(91)  评论(0编辑  收藏  举报