我來寫一下吧:
十多年的化隆網(wǎng)站建設經(jīng)驗,針對設計、前端、開發(fā)、售后、文案、推廣等六對一服務,響應快,48小時及時工作處理。網(wǎng)絡營銷推廣的優(yōu)勢是能夠根據(jù)用戶設備顯示端的尺寸不同,自動調(diào)整化隆建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設計,從而大程度地提升瀏覽體驗。成都創(chuàng)新互聯(lián)公司從事“化隆網(wǎng)站設計”,“化隆網(wǎng)站推廣”以來,每個客戶項目都認真落實執(zhí)行。
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;
//定義一個構(gòu)造函數(shù)
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;
//定義一個構(gòu)造函數(shù)
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;
//定義一個構(gòu)造函數(shù)
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的面積。實際上就是不規(guī)則多邊形的面積,如果不用什么函數(shù)庫(geotools)的話,還是有現(xiàn)成的公式的,非是通過定積分推倒了一個公式而已。\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
Java程序:
public?class?Main?{
public?static?void?main(String[]?args)?{
Shape?s?=?null;
s?=?new?Circle(3);
System.out.println("圓的面積:"?+?s.area());
System.out.println("圓的周長:"?+?s.perimeter());
}
}
/**
*?形狀類:抽象類
*?@author?developer
*?@version?2017.05.23
*/
abstract?class?Shape?{
/**
?*?計算形狀的面積
?*?@return?形狀的面積
?*/
abstract?double?area();
/**
?*?計算形狀的周長
?*?@return?形狀的周長
?*/
abstract?double?perimeter();
}
/**
*?圓類
*?@author?developer
*?@version?2017.05.23
*/
class?Circle?extends?Shape?{
/**
?*?半徑
?*/
protected?double?radius;
/**
?*?構(gòu)造方法
?*?@param?radius?半徑
?*/
public?Circle(double?radius)?{
this.radius?=?radius;
}
@Override
double?area()?{
return?Math.PI?*?radius?*?radius;
}
@Override
double?perimeter()?{
return?2?*?Math.PI?*?radius;
}
}
運行測試:
圓的面積:28.274333882308138
圓的周長:18.84955592153876
Shape.java接口代碼
public interface Shape {
public static final double PI = 3.14d;
public double area();
}
Circle.java圓類代碼
public class Circle implements Shape {
private double radius;
public Circle(double radius) {
? this.radius = radius;
}
@Override
public double area() {
? return PI * this.radius * this.radius;
}
public double perimeter() {
? return 2 * PI * this.radius;
}
}
Cylinder.java圓柱體類代碼
public class Cylinder extends Circle {
private double height;
public Cylinder(double radius, double height) {
? super(radius);
? this.height = height;
}
public double area() {
? return 2 * super.area() + super.perimeter() * this.height;
}
public double volume() {
? return super.area() * this.height;
}
}
X5_3_6.java主類代碼
public class X5_3_6 {
public static void main(String[] args) {
? Circle cir1 = new Circle(5);
? System.out.println("圓的面積為:" + cir1.area());
? System.out.println("圓的周長為:" + cir1.perimeter());
? Cylinder cy1 = new Cylinder(10, 15);
? System.out.println("圓柱體的表面積為:" + cy1.area());
? System.out.println("圓柱體的體積為:" + cy1.volume());
}
}
上面是我寫的代碼,下圖是執(zhí)行結(jié)果,麻煩看一下,是否可以。
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);
}