实验代码:
package 实验九;
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("程序结束");
}
}
}
实验结果:
package 实验九;
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("程序结束");
}
}
}
实验结果:
package 实验九;
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("程序结束");
}
}
}
实验结果:
package 实验九;
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("程序结束");
}
}
}
实验结果:
实验心得:
通过此次实验,我懂得了Java中错误如何通过捕捉,抛出的,其中finally语句是一定会执行的。