Java工具集-數學(一次函數)
- 2019 年 10 月 26 日
- 筆記
簡單工具類
寫作初衷:由於日常開發經常需要用到很多工具類,經常根據需求自己寫也比較麻煩 網上好了一些工具類例如commom.lang3或者hutool或者Jodd這樣的開源工具,但是 發現他們之中雖然設計不錯,但是如果我想要使用,就必須要引入依賴並且去維護依賴,有些 甚至會有存在版本編譯不通過問題,故此想要寫作一個每個類都可以作為獨立工具類使用 每個使用者只需要複製該類,到任何項目當中都可以使用,所以需要尊從以下兩個原則才能 做到.在此誠邀各位大佬參與.可以把各自用過的工具,整合成只依賴JDK,每個類都能夠單獨 使用的工具.每個人當遇到業務需求需要使用的時候,只需要到這裡單獨拷貝一個即可使用. 拋棄傳統的需要引入依賴的煩惱.讓大家一起來解決你所面臨的業務問題吧!
介紹
遵從兩大原則
- 1.絕不依賴JDK以外的源碼
- 2.犧牲程式碼復用性,每個類都必須是單獨的組件,絕不互相引用,做到完全解耦
package *; import java.math.BigDecimal; import java.math.RoundingMode; /** * @program: simple_tools * @description: 數學-一次函數 * @author: ChenWenLong * @create: 2019-10-24 10:48 **/ public class LinearFunction { // 默認對結果都進行四捨五入操作 // 自變數: independent 因變數: dependent // 斜率 private BigDecimal slope = BigDecimal.ONE; //與Y軸的交點 private BigDecimal point = BigDecimal.ZERO; public static LinearFunction instance; // 公式 : y=kx+b //私有化構造器 private LinearFunction(){} private LinearFunction(BigDecimal slope,BigDecimal point){ this.point = point; this.slope = slope; } //默認創建一個正比例函數 static { if(instance == null){ synchronized (LinearFunction.class){ if(instance == null){ instance = new LinearFunction(); } } } } /** * 功能描述: * 〈創建一次函數〉 * * @params : [slope, point] * @return : com.simple.util.math.LinearFunction * @author : cwl * @date : 2019/10/24 11:02 */ public static void init(double slope,double point){ if(slope == 0){ throw new RuntimeException("slope is not be zero"); } instance.setPoint(new BigDecimal(point)); instance.setSlope(new BigDecimal(slope)); } /** * 功能描述: * 〈判斷點是否在線上〉 * * @params : [x, y] * @return : boolean * @author : cwl * @date : 2019/10/24 11:03 */ public static boolean isOnline(double x ,double y){ return new BigDecimal(y).setScale(2,RoundingMode.HALF_UP). equals(instance.getSlope().multiply(new BigDecimal(x)).add(new BigDecimal(y)).setScale(2,RoundingMode.HALF_UP)); } /** * 功能描述: * 〈通過y求x的值〉 * * @params : [y] * @return : java.math.BigDecimal * @author : cwl * @date : 2019/10/24 11:35 */ public static BigDecimal getX(double y){ BigDecimal dependent = new BigDecimal(y); return (dependent.subtract(instance.getPoint())).divide(instance.getSlope(), 2, RoundingMode.HALF_UP); } /** * 功能描述: * 〈根據x求y〉 * * @params : [x] * @return : java.math.BigDecimal * @author : cwl * @date : 2019/10/24 11:44 */ public static BigDecimal getY(double x){ BigDecimal independent = new BigDecimal(x); return independent.multiply(instance.slope).add(instance.getPoint()).setScale(2,RoundingMode.HALF_UP); } /** * 功能描述: * 〈獲得斜率〉 * * @params : [x, y, b] * @return : java.math.BigDecimal * @author : cwl * @date : 2019/10/24 11:57 */ public static BigDecimal getSlope(double x,double y,double b){ BigDecimal dependent = new BigDecimal(y); return (dependent.subtract(new BigDecimal(b))).divide(new BigDecimal(x), 2, RoundingMode.HALF_UP); } /** * 功能描述: * 〈正比例函數獲得斜率〉 * * @params : [x, y] * @return : java.math.BigDecimal * @author : cwl * @date : 2019/10/24 11:58 */ public static BigDecimal getSlope(double x,double y){ BigDecimal dependent = new BigDecimal(y); return dependent.divide(new BigDecimal(x), 2, RoundingMode.HALF_UP); } @Override public String toString() { return "LinearFunction{" + "slope=" + slope + ", point=" + point + '}'; } private BigDecimal getSlope() { return slope; } private void setSlope(BigDecimal slope) { this.slope = slope; } private BigDecimal getPoint() { return point; } private void setPoint(BigDecimal point) { this.point = point; } }