失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 【Code】Java 打印基本图形(圆形 梯形 椭圆 三角形 矩形)的面积和周长

【Code】Java 打印基本图形(圆形 梯形 椭圆 三角形 矩形)的面积和周长

时间:2021-04-26 23:13:47

相关推荐

【Code】Java 打印基本图形(圆形 梯形 椭圆 三角形 矩形)的面积和周长

cmd调用的时候仍有乱码,不知道为什么,也查不到怎么解决(羞

public class ShapeTest {public static void main(String[] args) {Shape[]shape = {new Rectangle(6, 8),new Triangle(3, 4, 5),new Circle(3),new Square(5),new Ellipse(2,1),new Trapezoid(3,5,2)};for(int i=0; i<shape.length; i++) {System.out.println(shape[i]);shape[i].area();shape[i].perimeter();}}}// 形状接口, 规定了计算面积和周长的方法public interface Shape {voidarea(); //面积voidperimeter();//周长}public class Circle implements Shape {private double radius;// 半径public Circle() {}public Circle(double radius){this.radius= radius;}public double getRadius() {returnradius;}public void setRadius(double radius) {this.radius= radius;}public String toString() {Strings = "圆\t半径: " +radius;returns;}public void area() {// 计算面积, 实现接口方法Strings = "\t面积= " + (Math.PI * radius * radius);System.out.println(s);}public void perimeter() {// 计算周长, 实现接口方法Strings = "\t周长= " + (2 * Math.PI * radius);System.out.println(s);}}public class Ellipse implements Shape {privatedouble a;//短轴privatedouble b;//长轴publicEllipse() {}publicEllipse(double a, double b){this.a=a;this.b=b;}publicdouble getA() {returna;}publicdouble getB() {return b;}publicvoid setA(double a) {this.a= a;}publicvoid setB(double b) {this.b= b;}public String toString() {Strings = "椭圆\t短轴&长轴: " + a+b;returns;}publicvoid area() {// 计算面积, 实现接口方法intn=100000; //蒙特卡洛方法求椭圆面积intcount=0;for(inti=1;i<n;i++) {doublex=Math.random();doubley=Math.random();if(b*b*x*x+a*a*y*y<a*a*b*b) {count++;}}doublemianji=(double)4*count/n;Strings = "\t面积= " + mianji;System.out.println(s);}public void perimeter() {// 计算周长, 实现接口方法Strings = "\t周长= " + (2 * 3.14*b+4*(a-b));//圆周率Π近似用3.14System.out.println(s);}}public class Triangle implements Shape {private double a, b, c;// 三条边public Triangle() {}public Triangle(double a, double b, double c){this.a= a;this.b= b;this.c= c;}public void setEdge(double a, double b, doublec) {this.a= a;this.b= b;this.c= c;}publicString toString() {Strings = "三角形\ta: " + a + "\tb: " + b +"\tc: " + c;returns;}publicvoid area() {// 计算面积, 实现接口方法doublep = (a + b + c) / 2;Strings = "\t面积= " + Math.sqrt(p * (p - a) * (p -b) * (p - c));System.out.println(s);}publicvoid perimeter() {// 计算周长, 实现接口方法Strings = "\t周长= " + (a + b + c);System.out.println(s);}}public class Rectangle implements Shape {privateint length;// 长privateint width; // 宽publicRectangle() {}publicRectangle(int length, int width){this.length= length;this.width= width;}publicint getLength() {returnlength;}publicint getWidth() {returnwidth;}publicvoid setLength(int length) {this.length= length;}publicvoid setWidth(int width) {this.width= width;}public String toString() {Strings = "长方形\t长: " +length + "\t宽: " + width;returns;}publicvoid area() {// 计算面积, 实现接口方法Strings = "\t面积= " + length * width;System.out.println(s);}publicvoid perimeter() {// 计算周长, 实现接口方法Strings = "\t周长= " + (length*2 + width*2);System.out.println(s);}}public class Square extends Rectangle {public Square() {}public Square(int edge) {super(edge,edge);}public int getEdge() {returngetLength();}public void setEdge(int edge) {setLength(edge);setWidth(edge);}public String toString() {Strings = "正方形\t边长: " +getLength();returns;}}public class Trapezoid implements Shape{privatedouble sd,xd,h;//上底,下底,高publicTrapezoid() {}publicTrapezoid(double sd, double xd, double h) {this.sd= sd;this.xd= xd;this.h= h;}publicvoid setEdge(double sd, double xd, double h) {this.sd= sd;this.xd= xd;this.h= h;}publicString toString() {Strings = "梯形\t上底: " + sd +"\t下底: " + xd + "\t高: " + h;returns;}publicvoid area() {// 计算面积, 实现接口方法doublep = (sd+xd)*h / 2;Strings = "\t面积= " + p;System.out.println(s);}publicvoid perimeter() {// 计算周长, 实现接口方法Strings = "\t周长= " + (sd+xd+h);System.out.println(s);}}

如果觉得《【Code】Java 打印基本图形(圆形 梯形 椭圆 三角形 矩形)的面积和周长》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。