Ninth eighth experiment experiment +

Experimental nine: the exception is thrown, caught and handled

Experimental Procedure 

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("程序结束");

}
}

}

Experimental results

Please enter a point

1   2

x is 1 y is 2

End of program

 

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 ( "Enter a point:");
int shuru.nextInt H = ();
int shuru.nextInt A = ();

System.out.println ( "Please enter the rectangular length:");
int shuru.nextInt B = ();

System.out.println ( "Please enter the width of a rectangle:");
int shuru.nextInt C = ();
Point P1 Point new new = (H, A);
@SuppressWarnings ( "unused")
the Rectangle the Rectangle new new m = (P1 , B, C);
IF (length> = 0 && width> = 0) {
System.out.println ( "this is a length" + Rectangle.length + "width" + Rectangle.width + "rectangular");
}
the else {
the throw new new Exception () {
public String toString () {
return "invalid parameter";
}
};
}
}
the catch (Exception E) {
System.out.println (e.toString ());
}
the finally {
the System.out. println ( "end of program");

}
}

}

The results:

Please enter a point

1   2

Please enter long rectangular

4

Please enter wide rectangular

3

This is a width of a rectangular parallelepiped of length 4 3

End of program

 

 

 

import java.util.Scanner;

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

}
@SuppressWarnings ( "Serial")
public static void main (String [] args) {
@SuppressWarnings ( "Resource")
Scanner Scanner shuru new new = (the System.in);
System.out.println ( "Enter n");
n-shuru.nextInt = int ();
int Point [] = new new int [n-];
int I;
for (I = 0; I <n-; I ++) {
System.out.println ( "Please enter polygon" + i + "points");
int = h1 of shuru.nextInt ();
int shuru.nextInt A1 = ();
@SuppressWarnings ( "unused")
point P2 point new new = (h1 of, A1);

the try {
// point S new new Point = (-1,2);
IF (Point.length> =. 3)
{
System.out.println ( "which may constitute a polygon");
}
else {
throw new Exception() {
public String toString() {
return "Invalid parameter";
}
};
}
}
the catch (Exception E) {
System.out.println (e.toString ());
}
the finally {
System.out.println ( "End of program");

}
}
}

 The results:

Please enter n

4

Please input polygon point 0th

1  2

Please enter the polygon first point

1   6

Enter the second polygon point

2    5

Enter three points of the polygon

2    7

This may constitute a polygon

End of the experiment

 

 

 

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 ( "Enter a point:");
int shuru.nextInt H = ();
int shuru.nextInt A = ();
Point P1 Point new new = (H, A);

System.out.println ( "Please enter a point:");
int = h1 of shuru.nextInt ();
int shuru.nextInt A1 = ();
point P2 point new new = (h1 of, A1);

System.out.println ( "Please enter a point: ");
int shuru.nextInt H2 = ();
int shuru.nextInt A2 = ();
Point new new Point P3 = (H2, A2);
@SuppressWarnings (" unused ")
Triangle Triangle new new S = (P1, P2, P3 );
Double G = (h1 of-H) / (A1-A);
Double P = (h1 of-H2) / (A2-A1);
! IF (P = G) {
System.out.println ( "three points may form a triangle ");
}
the else {
the throw new new Exception () {
public String toString () {
return" invalid parameter ";
}
};
}
}
The catch (Exception E) {
System.out.println (e.toString ());
}
the finally {
System.out.println ( "End of program");

}
}

}

The results:

Please enter a point

2     3

Please enter a point

3     6

Please enter a point

3     5

This can form a triangle

Please enter a point

1   2

Please enter a point

 2    1

Please enter a point

3    3

This can form a triangle

End of program

 

Experimental experience:

1. thrown suggestive java.lang.illegalArguementException

2. thrown a means of testing procedures aborted

3. To make the program an exception thrown by the normal operation

 

 

Experiment Eight: interface class that implements the interface

Experimental Procedure

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();

二.实验心得         
By commissioning this experiment, so I clearly recognize that the interface is an abstract type in JAVA programming language, is a collection of abstract methods, the interface is usually interface to declare. Whereby a class to inherit the abstract methods of the interface by way of interface inheritance. And the interface is not a class, the way to write interfaces and classes are similar, but they belong to different concepts. Class attributes and methods described objects. Interface to be implemented for class contains. 

 

 

Guess you like

Origin www.cnblogs.com/baiyunchao/p/10930089.html