當前位置:編程學習大全網 - 編程語言 - Java編程——求解幾何圖形的周長、面積的程序。

Java編程——求解幾何圖形的周長、面積的程序。

/**

* The Class PerimeterArea. This Class is to compute perimeter and area

*/

public class PerimeterArea {

/**

* The main method.

*

* @param args

* the arguments

*/

public static void main(String[] args) {

Shape TriangleObj = new Triangle(3, 4, 5);// 定義三邊為3,4,5的三角形

Shape RectangleObj = new Rectangle(5, 6);// 定義長為5,寬為6的矩形

Shape CircleObj = new Circle(5);// 定義半徑為5的對象

// 打印各個形狀的周長和面積

System.out.println(TriangleObj.toString());

System.out.println(RectangleObj.toString());

System.out.println(CircleObj.toString());

}

}

// 周長接口

interface perimeter {

public abstract double cutePerimeter();

}

// 面積接口

interface area {

public abstract double cuteArea();

}

// 抽象形狀類

abstract class Shape implements perimeter, area {

public abstract double cutePerimeter();

public abstract double cuteArea();

}

// 三角形

class Triangle extends Shape {

private int aSide;

private int bSide;

private int cSide;

public Triangle(){}

public Triangle(int aSide, int bSide, int cSide) {

this.aSide = aSide;

this.bSide = bSide;

this.cSide = cSide;

}

public double cutePerimeter() {

return aSide + bSide + cSide;

}

public double cuteArea() {

//面積公式

/*設三邊長分別為A、B、C,面積為S;周長的壹半P為(A+B+C)/2.

S=√[P(P-A)*(P-B)*(P-C)].

√為開二次方. */

int x = (aSide + bSide + cSide) / 2;

return Math.sqrt(x*(x-aSide)*(x-bSide)*(x-cSide));

}

public int getASide() {

return aSide;

}

public void setASide(int side) {

aSide = side;

}

public int getBSide() {

return bSide;

}

public void setBSide(int side) {

bSide = side;

}

public int getCSide() {

return cSide;

}

public void setCSide(int side) {

cSide = side;

}

public String toString() {

return "三角形的周長為:" + cutePerimeter() + "\n三角形的面積為:" + cuteArea() + "\n";

}

}

// 矩形

class Rectangle extends Shape {

private int longLength;

private int widthLength;

public Rectangle(){}

public Rectangle(int longLength, int widthLength) {

this.longLength = longLength;

this.widthLength = widthLength;

}

public double cutePerimeter() {

return 2 * (longLength + widthLength);

}

public double cuteArea() {

return longLength * widthLength;

}

public int getLongLength() {

return longLength;

}

public void setLongLength(int longLength) {

this.longLength = longLength;

}

public int getWidthLength() {

return widthLength;

}

public void setWidthLength(int widthLength) {

this.widthLength = widthLength;

}

public String toString() {

return "矩形的周長為:" + cutePerimeter() + "\n矩形的面積為:" + cuteArea() + "\n";

}

}

// 圓形

class Circle extends Shape {

public final double pi = 3.14;

private double radius;

public Circle(){}

public Circle(double radius) {

this.radius = radius;

}

public double cutePerimeter() {

return 2 * pi * radius;

}

public double cuteArea() {

return pi * radius * radius;

}

public double getRadius() {

return radius;

}

public void setRadius(double radius) {

this.radius = radius;

}

public String toString() {

return "圓形的周長為:" + cutePerimeter() + "\n圓形的面積為:" + cuteArea() + "\n";

}

}

三角形面積已經修正~謝謝樓上的兄弟~set和get方法只是為了拓展性考慮~不局限於new才能設置參數值

  • 上一篇:折刀編程
  • 下一篇:紅色研究可以做嗎?
  • copyright 2024編程學習大全網