codewars034: TDD Area Calculations
发布时间:2020-12-14 02:00:49 所属栏目:百科 来源:网络整理
导读:Source package codewars;import java.math.BigDecimal;public class Calculator{ public double getTotalArea(Shape shape){ return new BigDecimal(Double.toString(shape.getArea())).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); } public doub
Source package codewars; import java.math.BigDecimal; public class Calculator{ public double getTotalArea(Shape shape){ return new BigDecimal(Double.toString(shape.getArea())).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); } public double getTotalArea(Shape... shapes){ double total = 0; for(Shape x : shapes){ total += x.getArea(); } return new BigDecimal(Double.toString(total)).setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); } } interface Shape{ public double getArea(); } class Square implements Shape{ private double side; public Square(double side){ this.side = side; } @Override public double getArea(){ return side * side; } } class Rectangle implements Shape{ private double height; private double width; public Rectangle(double height,double width){ this.height = height; this.width = width; } @Override public double getArea(){ return height * width; } } class Circle implements Shape{ private double radius; public Circle(double radius){ this.radius = radius; } @Override public double getArea(){ return Mathi.PI * radius * radius; } } class Triangle implements Shape{ private double base; private double height; public Triangle(double base,double height){ this.base = base; this.height = height; } @Override public double getArea(){ return base * height / 2; } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- vue 新版本 webpack 代理 跨域设置
- 使用条件中的条件的Makefile问题在命令的命令部分中使用
- objective-c – $(ARCHS_STANDARD_32_BIT)vs. armv6,armv7
- Swift中的ios – base64EncodedStringWithOptions在编译错误
- 各类正则表达式
- vb.net 教程 5-13 图像处理之像素处理 8-2
- 迈向flex 的第二步 ----FLEX 与 VS 2008 利用 FluorineFx 开
- Using SQLite in your Windows 8 Metro style applications
- objective-c – 在外部抽头上关闭模态视图表单控制器
- 在C应用程序中嵌入JavascriptCore