写作初衷:由于日常开发经常需要用到很多工具类,经常根据需求自己写也比较麻烦 网上好了一些工具类例如commom.lang3或者hutool或者Jodd这样的开源工具,但是 发现他们之中虽然设计不错,但是如果我想要使用,就必须要引入依赖并且去维护依赖,有些 甚至会有存在版本编译不通过问题,故此想要写作一个每个类都可以作为独立工具类使用 每个使用者只需要复制该类,到任何项目当中都可以使用,所以需要尊从以下两个原则才能 做到.在此诚邀各位大佬参与.可以把各自用过的工具,整合成只依赖JDK,每个类都能够单独 使用的工具.每个人当遇到业务需求需要使用的时候,只需要到这里单独拷贝一个即可使用. 抛弃传统的需要引入依赖的烦恼.让大家一起来解决你所面临的业务问题吧!
遵从两大原则
package *;
import java.text.DecimalFormat;
/**
* @program: simple_tools
* @description: 四边形工具类
* @author: ChenWenLong
* @create: 2019-06-03 09:52
**/
public class QuadrangleUtils {
public static final DecimalFormat ROUNDING_OFF = new DecimalFormat("#.00");
/**
* 功能描述:
* 〈获取四舍五入的结果值,并且取得绝对值,解决负数情况〉
*
* @params : [value]
* @return : double
* @author : cwl
* @date : 2019/6/3 15:50
*/
public static double roundValue(double value){
return Math.abs(Double.valueOf(ROUNDING_OFF.format(value)));
}
/**
* 功能描述:
* 〈判断是否是矩形〉
*
* @params : [length, wideth]
* @return : boolean
* @author : cwl
* @date : 2019/6/3 10:25
*/
public static boolean isRectangle(double length,double wideth){
if(length == 0 || wideth == 0){
throw new IllegalArgumentException("This isn't Rectangle");
}
return true;
}
/**
* 功能描述:
* 〈判断是否是矩形 ==> 正方形〉
*
* @params : [side]
* @return : boolean
* @author : cwl
* @date : 2019/6/3 10:28
*/
public static boolean isRectangle(double side){
if(side == 0){
throw new IllegalArgumentException("This isn't Rectangle");
}
return true;
}
/**
* 功能描述:
* 〈判断是否是正方形〉
*
* @params : [length, wideth]
* @return : boolean
* @author : cwl
* @date : 2019/6/3 10:26
*/
public static boolean isSquare(double length,double wideth){
isRectangle(length,wideth);
if(length == wideth){
return true;
}
return false;
}
/**
* 功能描述:
* 〈获取长方形周长〉
*
* @params : [length, wideth]
* @return : double
* @author : cwl
* @date : 2019/6/3 10:00
*/
public static double getPerimeter(double length,double wideth){
isRectangle(length,wideth);
double perimeter = (length + wideth) * 2;
return roundValue(perimeter);
}
/**
* 功能描述:
* 〈获取长方形面积〉
*
* @params : [length, wideth]
* @return : double
* @author : cwl
* @date : 2019/6/3 10:03
*/
public static double getArea(double length,double wideth){
isRectangle(length,wideth);
double area = length * wideth;
return roundValue(area);
}
/**
* 功能描述:
* 〈获取正方形周长〉
*
* @params : [side]
* @return : double
* @author : cwl
* @date : 2019/6/3 10:16
*/
public static double getPerimeter(double side){
isRectangle(side);
double perimeter = side * 4;
return roundValue(perimeter);
}
/**
* 功能描述:
* 〈获取正方形面积〉
*
* @params : [side]
* @return : double
* @author : cwl
* @date : 2019/6/3 10:29
*/
public static double getArea(double side){
isRectangle(side);
double area = side * side;
return roundValue(area);
}
/**
* 功能描述:
* 〈获得矩形内部对角线分割成的三角形周长〉
*
* @params : []
* @return : double
* @author : cwl
* @date : 2019/6/3 10:32
*/
public static double diagonalTrianglePerimeter(double length,double wideth){
isRectangle(length,wideth);
//获取斜边
double hypotenuse = Math.sqrt(Math.pow(length,2)+Math.pow(wideth,2));
double result = hypotenuse + length + wideth;
return roundValue(result);
}
/**
* 功能描述:
* 〈获得正方形对角线对应的三角形周长〉
*
* @params : [side]
* @return : double
* @author : cwl
* @date : 2019/6/3 10:41
*/
public static double diagonalTrianglePerimeter(double side){
isRectangle(side);
//获取斜边
double hypotenuse = Math.sqrt(2)*side;
double result = hypotenuse + side * 2;
return roundValue(result);
}
/**
* 功能描述:
* 〈获取矩形内部对角线分割成的三角形的面积〉
*
* @params : [length, wideth]
* @return : double
* @author : cwl
* @date : 2019/6/3 10:43
*/
public static double diagonalTrianglArea(double length,double wideth){
return getArea(length, wideth)/2;
}
/**
* 功能描述:
* 〈获取正方形内部对角线分割成的三角形的面积〉
*
* @params : [length, wideth]
* @return : double
* @author : cwl
* @date : 2019/6/3 10:45
*/
public static double diagonalTrianglArea(double side){
return getArea(side)/2;
}
/**
* 功能描述:
* 〈获得梯形面积〉
*
* @params : [upSide(上边长), lowSide(下边长), high(垂直高)]
* @return : double
* @author : cwl
* @date : 2019/6/3 10:53
*/
public static double getTrapezoidArea(double upSide,double lowSide,double high){
if(upSide == 0 || lowSide == 0){
throw new IllegalArgumentException("This isn't Trapezoid");
}
return roundValue(((upSide + lowSide) * high) / 2);
}
/**
* 功能描述:
* 〈获取矩形内切圆周长〉
*
* @params : [length, wideth]
* @return : double
* @author : cwl
* @date : 2019/6/3 17:39
*/
public static double getIncirclePerimeter(double length,double wideth){
//取比较短的一边作为内切圆的直径 则perimeter = diameter * π
if(Math.abs(length) >= Math.abs(wideth)){
return roundValue(wideth*Math.PI);
}
return roundValue(length*Math.PI);
}
/**
* 功能描述:
* 〈获取矩形内切圆面积〉
*
* @params : [length, wideth]
* @return : double
* @author : cwl
* @date : 2019/6/3 17:50
*/
public static double getIncircleArea(double length,double wideth){
//取比较短的一边作为内切圆的直径 则perimeter = diameter * π
double r;
if(Math.abs(length) >= Math.abs(wideth)){
r = wideth/2;
return roundValue(r*r*Math.PI);
}
r = length/2;
return roundValue(r*r*Math.PI);
}
}