Java编程——求解几何图形的周长、面积的程序。

编写求解几何图形(包括三角形、矩形、圆形)的周长、面积的程序,
要求尽量用到继承、接口,以及通过创建不同的实例对象实现类的多态性。

小弟的选修课,考试题,哪位大大帮帮忙。答案准确可以再追加10分

/**
* 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才能设置参数值
温馨提示:答案为网友推荐,仅供参考
第1个回答  2023-10-25
关于使用Java编程求解几何图形的周长和面积,我将为您提供相关信息。

Java是一种功能强大的编程语言,可以用于解决各种问题,包括求解几何图形的周长和面积。下面是一个简单的示例程序,展示了如何使用Java编程计算矩形的周长和面积:更系统全面的学习资料,点击查看


```java
import java.util.Scanner;

public class GeometryCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.println("请输入矩形的长度:");
double length = input.nextDouble();

System.out.println("请输入矩形的宽度:");
double width = input.nextDouble();

double perimeter = 2 * (length + width);
double area = length * width;

System.out.println("矩形的周长为:" + perimeter);
System.out.println("矩形的面积为:" + area);

input.close();
}
}
```

在这个示例程序中,我们首先使用`Scanner`类获取用户输入的矩形的长度和宽度。然后,我们根据周长和面积的公式进行计算,并将结果输出到控制台上。

千锋教育作为专业的IT互联网技术培训机构,我们提供全面的Java开发和培训课程。我们的Java课程涵盖了从基础知识到高级编程技术的内容。无论您是初学者还是有一定经验的开发者,我们都将根据您的需求提供个性化的培训服务。

通过千锋教育的Java培训,您将学习到Java语言的核心概念和编程技巧。我们的课程不仅注重理论知识的传授,还强调实践能力的培养。您将通过编写实际的程序案例来巩固所学知识,并获得解决实际问题的能力。

请访问千锋教育官方网站或咨询我们的客服人员了解更多详情。相信通过千锋教育的Java培训,您将获得优秀的编程能力和职业发展机会! 千锋IT培训机构,热门IT课程试听名额限时领取官网在线咨询千叶育儿
2010-03-05·育儿、诗词分享,每天进步一点点!千叶育儿采纳数:1524获赞数:5378
向TA提问私信TA/**
* 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才能设置参数值已赞过已踩过<你对这个回答的评价是?评论收起激活痛
2010-03-08知道答主回答量:采纳率:0%帮助的人:我也去答题访问个人页平面图形
名称 符号 周长C和面积S
正方形 a—边长 C=4a
S=a2
长方形 a和b-边长 C=2(a+b)
S=ab
三角形 a,b,c-三边长
h-a边上的高
s-周长的一半
A,B,C-内角
其中s=(a+b+c)/2 S=ah/2
=ab/2·sinC
=[s(s-a)(s-b)(s-c)]1/2
=a2sinBsinC/(2sinA)

四边形 d,D-对角线长
α-对角线夹角 S=dD/2·sinα
平行四边形 a,b-边长
h-a边的高
α-两边夹角 S=ah
=absinα
菱形 a-边长
α-夹角
D-长对角线长
d-短对角线长 S=Dd/2
=a2sinα
梯形 a和b-上、下底长
h-高
m-中位线长 S=(a+b)h/2
=mh
圆 r-半径
d-直径 C=πd=2πr
S=πr2
=πd2/4
扇形 r—扇形半径
a—圆心角度数
C=2r+2πr×(a/360)
S=πr2×(a/360)
弓形 l-弧长
b-弦长
h-矢高
r-半径
α-圆心角的度数 S=r2/2·(πα/180-sinα)
=r2arccos[(r-h)/r] - (r-h)(2rh-h2)1/2
=παr2/360 - b/2·[r2-(b/2)2]1/2
=r(l-b)/2 + bh/2
≈2bh/3
圆环 R-外圆半径
r-内圆半径
D-外圆直径
d-内圆直径 S=π(R2-r2)
=π(D2-d2)/4
椭圆 D-长轴
d-短轴 S=πDd/4
立方图形
名称 符号 面积S和体积V
正方体 a-边长 S=6a2
V=a3
长方体 a-长
b-宽
c-高 S=2(ab+ac+bc)
V=abc
棱柱 S-底面积
h-高 V=Sh
棱锥 S-底面积
h-高 V=Sh/3
棱台 S1和S2-上、下底面积
h-高 V=h[S1+S2+(S1S1)1/2]/3
拟柱体 S1-上底面积
S2-下底面积
S0-中截面积
h-高 V=h(S1+S2+4S0)/6
圆柱 r-底半径
h-高
C—底面周长
S底—底面积
S侧—侧面积
S表—表面积 C=2πr
S底=πr2
S侧=Ch
S表=Ch+2S底
V=S底h
=πr2h

空心圆柱 R-外圆半径
r-内圆半径
h-高 V=πh(R2-r2)
直圆锥 r-底半径
h-高 V=πr2h/3
圆台 r-上底半径
R-下底半径
h-高 V=πh(R2+Rr+r2)/3
球 r-半径
d-直径 V=4/3πr3=πd2/6
球缺 h-球缺高
r-球半径
a-球缺底半径 V=πh(3a2+h2)/6
=πh2(3r-h)/3
a2=h(2r-h)
球台 r1和r2-球台上、下底半径
h-高 V=πh[3(r12+r22)+h2]/6
圆环体 R-环体半径
D-环体直径
r-环体截面半径
d-环体截面直径 V=2π2Rr2
=π2Dd2/4
桶状体 D-桶腹直径
d-桶底直径
h-桶高 V=πh(2D2+d2)/12
(母线是圆弧形,圆心是桶的中心)
V=πh(2D2+Dd+3d2/4)/15
第2个回答  2010-03-11
a
as
aas
第3个回答  推荐于2017-11-23
//Dynamic.java
interface MyShape{
public double area();
public double circum();
}

class MyRectangle implements MyShape{
private double height;
private double width;

public MyRectangle(double height,double width){
this.height = height;
this.width = width;
}

public double area(){
return height * width;
}

public double circum(){
return 2 * (height + width);
}
}

class MyCircle implements MyShape{
private double radius;

public MyCircle(double radius){
this.radius = radius;
}

public double area(){
return Math.PI * radius * radius;
}

public double circum(){
return 2 * Math.PI * radius;
}
}

class MyTriangle implements MyShape{
private double a;
private double b;
private double c;

public MyTriangle(double a,double b,double c){
this.a = a;
this.b = b;
this.c = c;
}

public double area(){
return Math.sqrt((a + b + c) * (a + b - c) * (a + c - b) *
(b + c - a)) / 4;
}

public double circum(){
return a + b + c;
}
}

public class Dynamic{
public static void main(String[] args){
MyShape myShape;
if(args.length == 1)
myShape = new MyCircle(Double.parseDouble(args[0]));
else if(args.length == 2)
myShape = new MyRectangle(Double.parseDouble(args[0]),
Double.parseDouble(args[1]));
else if(args.length == 3)
myShape = new MyTriangle(Double.parseDouble(args[0]),
Double.parseDouble(args[1]),Double.parseDouble(args[2]));
else{
System.out.println("运行出错,应该以1个或两个或三个的命令行参数" +
"来运行程序");
return;
}

System.out.println(myShape.area());
System.out.println(myShape.circum());
}
}
向楼上的很多set get 方法我都没写。
并且 我的三角形面积百分百准确,他那个不行本回答被提问者采纳
相似回答