新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
我来写一下吧:
创新互联公司坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的余干网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
abstract class Shape{
private double c;
private double s;
public abstract void girth();
public abstract void area();
public void setGirth(double c){
this.c = c;
}
public void setArea(double s){
this.s = s;
}
public double getGirth(){
return c;
}
public double getArea(){
return s;
}
public void outInfo(){}
}
class Circle extends Shape{
private static final double PI = 3.1415926;
private double r;
//定义一个构造函数
public Circle(double r){
this.r = r;
}
//重写抽象方法
public void girth() {
double a =2*PI*r;
super.setGirth(a);
}
public void area() {
double b =PI*r*r;
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求圆形周长为:"+super.getGirth());
System.out.println("所求圆形面积为:"+super.getArea());
}
}
class Rectangle extends Shape{
private double height;
private double width;
//定义一个构造函数
public Rectangle(double height,double width){
this.height = height;
this.width = width;
}
//重写抽象方法
public void girth() {
double a =2*(height+width);
super.setGirth(a);
}
public void area() {
double b =(height*width);
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求矩形周长为:"+super.getGirth());
System.out.println("所求矩形面积为:"+super.getArea());
}
}
class Triangle extends Shape{
private double lengthA;
private double lengthB;
private double lengthC;
//定义一个构造函数
public Triangle(double lengthA,double lengthB,double lengthC){
this.lengthA = lengthA;
this.lengthB = lengthB;
this.lengthC = lengthC;
}
//重写抽象方法
public void girth() {
double a =(lengthA+lengthB+lengthC);
super.setGirth(a);
}
public void area() {
if((lengthA+lengthB lengthC) || (lengthA + lengthC lengthB) || (lengthB+lengthC lengthA)) {
System.out.println("两边之和必须大于第三个边");
System.exit(0);
}
double p = (lengthA+lengthB+lengthC)/2;
double b = Math.sqrt(p*(p-lengthA)*(p-lengthB)*(p-lengthC));
super.setArea(b);
}
public void outInfo() {
this.girth();
this.area();
System.out.println("所求三角形周长为:"+super.getGirth());
System.out.println("所求三角形面积为:"+super.getArea());
}
}
public class ShapeTest {
public static void main (String [] args){
Shape circle = new Circle(3.0);
Shape rectangle = new Rectangle(8.0,7.0);
Shape triangle = new Triangle(3.0,4.0,5.0);
circle.outInfo();
rectangle.outInfo();
triangle.outInfo();
}
}
楼主是不是想利用Java求shape文件中 面的面积,也就是polygon或者multipolygon的面积。实际上就是不规则多边形的面积,如果不用什么函数库(geotools)的话,还是有现成的公式的,非是通过定积分推倒了一个公式而已。\x0d\x0a需要注意的是:\x0d\x0a点要按照逆时针或者顺时针的顺序添加进list\x0d\x0apackage geodemo;\x0d\x0aimport java.awt.Point;\x0d\x0aimport java.util.ArrayList;\x0d\x0aimport java.util.List;\x0d\x0aimport org.opengis.feature.simple.SimpleFeature;\x0d\x0aimport com.vividsolutions.jts.geom.Geometry;\x0d\x0apublic class GetArea{\x0d\x0apublic static void main(String args[]){\x0d\x0aPoint p1 = new Point(1,0);\x0d\x0aPoint p2 = new Point(12,0);\x0d\x0aPoint p3 = new Point(10,10);\x0d\x0aPoint p4 = new Point(0,10);\x0d\x0aPoint p5= new Point(3,3);\x0d\x0aList list = new ArrayList();//泛型\x0d\x0alist.add(p1);\x0d\x0alist.add(p2);\x0d\x0alist.add(p3);\x0d\x0alist.add(p4);\x0d\x0alist.add(p5);\x0d\x0aGetArea t = new GetArea();\x0d\x0adouble area = t.getArea(list);\x0d\x0aSystem.out.println(area);\x0d\x0a}\x0d\x0apublic double getArea(List list)\x0d\x0a{\x0d\x0a//S = 0.5 * ( (x0*y1-x1*y0) + (x1*y2-x2*y1) + ... + (xn*y0-x0*yn) )\x0d\x0adouble area = 0.00;\x0d\x0afor(int i = 0;i
回答于 2022-11-16
public class Circle implements Shape{
public static void main(String[] args) {
Circle circle = new Circle();
float r = 5; //半径
System.out.println("圆面积:" + circle.getArea(r));
System.out.println("圆周长:" + circle.getCircumference(r));
}
@Override
public float getArea(float a) {
float pi = (float) Math.PI;
return pi * a * a;
}
@Override
public float getCircumference(float a) {
float pi = (float) Math.PI;
return 2 * pi * a;
}
}
public interface Shape {
float getArea(float a);
float getCircumference (float a);
}