Java工具集-數學(指數函數)
- 2019 年 10 月 26 日
- 筆記
簡單工具類
寫作初衷:由於日常開發經常需要用到很多工具類,經常根據需求自己寫也比較麻煩 網上好了一些工具類例如commom.lang3或者hutool或者Jodd這樣的開源工具,但是 發現他們之中雖然設計不錯,但是如果我想要使用,就必須要引入依賴並且去維護依賴,有些 甚至會有存在版本編譯不通過問題,故此想要寫作一個每個類都可以作為獨立工具類使用 每個使用者只需要複製該類,到任何項目當中都可以使用,所以需要尊從以下兩個原則才能 做到.在此誠邀各位大佬參與.可以把各自用過的工具,整合成只依賴JDK,每個類都能夠單獨 使用的工具.每個人當遇到業務需求需要使用的時候,只需要到這裡單獨拷貝一個即可使用. 拋棄傳統的需要引入依賴的煩惱.讓大家一起來解決你所面臨的業務問題吧!
介紹
遵從兩大原則
- 1.絕不依賴JDK以外的源碼
- 2.犧牲程式碼復用性,每個類都必須是單獨的組件,絕不互相引用,做到完全解耦
package *; /** * @program: simple_tools * @description: 指數函數 * @author: ChenWenLong * @create: 2019-10-25 09:51 **/ public class ExponentialFunction { //y=a^x函數(a為常數且以a>0,a≠1) private double a = 2; public static ExponentialFunction instance; private static final Point DEFAULT_POINT = new Point(0,1); private ExponentialFunction(){} static { if(instance == null){ synchronized (ExponentialFunction.class){ if(instance == null){ instance = new ExponentialFunction(); } } } } /** * 功能描述: * 〈創建一個指數函數〉 * * @params : [a] * @return : void * @author : cwl * @date : 2019/10/25 10:11 */ public static void init(double a){ if(a < 0 || a == 1){ throw new RuntimeException("a is not less than zero and not be one"); } instance.setA(a); } /** * 功能描述: * 〈根據Y獲取X的值〉 * * @params : [y] * @return : double * @author : cwl * @date : 2019/10/25 10:15 */ public static double getX(double y,int result){ double a = instance.getA(); double nextTime; if(y % a == 0 && y % a != 1){ nextTime = y / a; result++; getX(nextTime,result); } if(y % a == 1){ return result; } throw new RuntimeException("this y is not on line"); } /** * 功能描述: * 〈根據x獲取y的值〉 * * @params : [x] * @return : double * @author : cwl * @date : 2019/10/25 10:31 */ public static double getY(double x){ double a = instance.getA(); return Math.pow(x,a); } /** * 功能描述: * 〈獲取指數函數默認經過的點〉 * * @params : [] * @return : com.simple.util.math.function.ExponentialFunction.Point * @author : cwl * @date : 2019/10/25 10:32 */ public static Point getDefaultPoint(){ return DEFAULT_POINT; } /** * 功能描述: * 〈判斷點是否在指數函數上〉 * * @params : [point] * @return : boolean * @author : cwl * @date : 2019/10/25 10:33 */ public static boolean isOnline(Point point){ double x = point.getX(); double y = point.getY(); return y == Math.pow(x,instance.getA()); } //坐標點 public static class Point { //坐標x private double x; //坐標y private double y; public Point(double x,double y){ this.x = x; this.y = y; } public double getX() { return x; } public void setX(double x) { this.x = x; } public double getY() { return y; } public void setY(double y) { this.y = y; } } public double getA() { return a; } public void setA(double a) { this.a = a; } }