1.先创建一个Point类,然后定义Trianglele类。在Trianglele类中定义三个Point的实体来表示一个三角形的三个点,再定义一个方法setTri对这三个点进行初始化,然后定义两个方法求三角形的周长、面积。在main()中创建一个对象,求给定三点的三角形的周长、面积。
2.将上题的方法setTri改用构造方法实现初始化。
3.编写JAVA程序求园柱体的表面积和体积,已知底面圆心p为(0,0),半径r为10,圆柱体高5。
第一题:
public class Trianglele {
Point p1 = new Point();
Point p2 = new Point();
Point p3 = new Point();
void setTri(double x1, double y1, double x2, double y2, double x3, double y3) {
this.p1.x = x1;
this.p1.y = y1;
this.p2.x = x1;
this.p1.y = y2;
this.p3.x = x3;
this.p3.y = y3;
}
double Circumference(Trianglele t) {
double line1, line2, line3;
line1 = Math.hypot((t.p1.x - t.p2.x), (t.p1.y - t.p2.y));
line2 = Math.hypot((t.p2.x - t.p3.x), (t.p2.y - t.p3.y));
line3 = Math.hypot((t.p1.x - t.p3.x), (t.p1.y - t.p3.y));
return line1 + line2 + line3;
}
double Area(Trianglele t) {
double line1, line2, line3;
line1 = Math.hypot((t.p1.x - t.p2.x), (t.p1.y - t.p2.y));
line2 = Math.hypot((t.p2.x - t.p3.x), (t.p2.y - t.p3.y));
line3 = Math.hypot((t.p1.x - t.p3.x), (t.p1.y - t.p3.y));
double p = Circumference(t)/2;
return Math.sqrt((p-line1)*(p-line2)*(p-line3)*p);
}
public static void main(String[] args) {
Trianglele t =new Trianglele();
t.setTri(0, 0, 0, 3, 4, 0);
System.out.println("三角形周长:"+t.Circumference(t));
System.out.println("三角形面积:"+t.Area(t));
}
}
class Point {
double x, y;
}
第二题:
public class Trianglele {
Point p1 = new Point();
Point p2 = new Point();
Point p3 = new Point();
Trianglele(double x1, double y1, double x2, double y2, double x3, double y3) {
this.p1.x = x1;
this.p1.y = y1;
this.p2.x = x1;
this.p1.y = y2;
this.p3.x = x3;
this.p3.y = y3;
}
double Circumference(Trianglele t) {
double line1, line2, line3;
line1 = Math.hypot((t.p1.x - t.p2.x), (t.p1.y - t.p2.y));
line2 = Math.hypot((t.p2.x - t.p3.x), (t.p2.y - t.p3.y));
line3 = Math.hypot((t.p1.x - t.p3.x), (t.p1.y - t.p3.y));
return line1 + line2 + line3;
}
double Area(Trianglele t) {
double line1, line2, line3;
line1 = Math.hypot((t.p1.x - t.p2.x), (t.p1.y - t.p2.y));
line2 = Math.hypot((t.p2.x - t.p3.x), (t.p2.y - t.p3.y));
line3 = Math.hypot((t.p1.x - t.p3.x), (t.p1.y - t.p3.y));
double p = Circumference(t)/2;
return Math.sqrt((p-line1)*(p-line2)*(p-line3)*p);
}
public static void main(String[] args) {
Trianglele t =new Trianglele(0, 0, 0, 3, 4, 0);
System.out.println("三角形周长:"+t.Circumference(t));
System.out.println("三角形面积:"+t.Area(t));
}
}
class Point {
double x, y;
}
第三题:
public class Cylinder {
double radius,height;
public static void main(String[] args) {
Cylinder c = new Cylinder();
System.out.println("圆柱体体积:"+c.Volume(10, 5));
}
double Volume(double r,double h) {
return 3.14*r*r*h;
}
}