Java工具集-数学(指数函数)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_42528266/article/details/102738295

简单工具类

写作初衷:由于日常开发经常需要用到很多工具类,经常根据需求自己写也比较麻烦
网上好了一些工具类例如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;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42528266/article/details/102738295