大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java的shape代码 编写shape类

java编写图形抽象类(Shape)

我来写一下吧:

十多年的化隆网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整化隆建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联公司从事“化隆网站设计”,“化隆网站推广”以来,每个客户项目都认真落实执行。

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抽象类,具有求面积、求周长、显示输出功能?

楼主是不是想利用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

JAVA程序编写。 定义一个抽象类shape ,用来表示一般图形。

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;

/**

 * 构造方法

 * @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

Java实验,代码怎么写?

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

}

}

上面是我写的代码,下图是执行结果,麻烦看一下,是否可以。

JAVA编写一个名为Shape的接口类型?

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

}


文章题目:java的shape代码 编写shape类
本文地址:http://dzwzjz.com/article/dooggsj.html
在线咨询
服务热线
服务热线:028-86922220
TOP