如何从外部库调用方法?

时间:2017-03-28 02:58:43

标签: java libraries getter-setter

我只是有一个快速的问题,我的导师没有真正过去。他的例子没有帮助我。

 static double Q1(NormalDistribution distro, double x){
        // return the z-score of x in the given distribution
    }

它表示在给定的分布中返回x的z分数。为了不必每次都做所有的数学运算和“重新发明轮子”,我们被教导导入库,我导入它,我只是对如何调用类的方法感到困惑。请解释

外部图书馆

    package cse115.math;

import java.util.ArrayList;

public class NormalDistribution{

    private double standardDeviation;
    private double mean;

    public NormalDistribution(double standardDeviation, double mean){
        this.standardDeviation = standardDeviation;
        this.mean = mean;
    }

    /**
     * Creates a normal distribution given a data set.
     */
    public NormalDistribution(ArrayList<Double> data){
        double sum = 0.0;
        for(double value : data){
            sum += value;
        }
        double mean = sum / data.size();
        double variance = 0.0;
        for(double value : data){
            variance += Math.pow(value - mean, 2.0);
        }
        variance /= data.size();

        this.mean = mean;
        this.standardDeviation = Math.sqrt(variance);
        // yes, this method puts 3 free points on the table for the observant.
    }


    /**
     * Returns the z-score of the provided value. Z-score is the number of standard deviations the value
     * is away from the mean.
     */
    public double zScore(double value){
        return (value - this.mean) / this.standardDeviation;
    }


    public double getStandardDeviation(){
        return this.standardDeviation;
    }

    public double getMean(){
        return this.mean;
    }

    @Override
    public String toString(){
        return "{" +
                "mean=" + mean +
                ", standardDeviation=" + standardDeviation +
                '}';
    }

}

1 个答案:

答案 0 :(得分:0)

您需要在main方法中创建外部类的实例。

    NormalDistribution normalDistribution = new NormalDistribution(0,0);

然后,您将通过保存它的Classname调用静态函数Q1,传递NormalDistribution和double x。

    double value = Cse115.Q1(normalDistribution,1);

无论那些应该做什么取决于你,祝你好运。