繁体   English   中英

我想将数组传递给另一个类,然后访问数组中的特定项目以计算平均值,最大值或最小值

[英]I want to pass an array to another class, then access specific items in the array to calculate average, maximum or minimum

此类用于将现象的名称存储到数组。 如何将数组传递给Collat​​or类以获取现象的平均值?

public class MonitoringStation {
    // Instance variables
    private String name;
    private Collator[] phenomena;
    private String phenomenonName;
    private double reading;
    private double average_value;
    private double sum;

    Collator collatorObject = new Collator(name);

    // Constructors
    public MonitoringStation(String name, String[] phenomenaNames) {
        // Create a MonitoringStation with the given name that will record statistics for the given phenomena.
        this.name = name;
        phenomena = new Collator[phenomenaNames.length];
        // System.out.println(phenomena.length);
    }

    // Methods
    public void recordReading(String phenomenonName, double reading) {
        // Update the records for the given phenomenon
        collatorObject.recordReading(reading);
        this.reading = reading;
        this.phenomenonName = phenomenonName;
    }

    public double average(String phenomenonName) {
        // Get the current value.
        return average_value;
    }
}

此类是负责测量现象的主要程序。

import java.util.Scanner;
//
public class StationUI {

    private StationUI() {}

    public static void main(final String[] args) {
        System.out.println("Monitoring Station Test Harness");
        final Scanner scanner = new Scanner(System.in);

        System.out.println("Enter the station name:");
        final String stationName = scanner.nextLine().trim();

        System.out.println("Enter a comma separated list of the phenomena to be recorded:");
        final String[] phenomena = scanner.nextLine().split("\\s*,\\s*");

        final MonitoringStation station = new MonitoringStation(stationName, phenomena);

        System.out.println("\nMake a selection and press return:");
        System.out.println("0.  Quit");
        System.out.println("1.  Record a phenomenon measurement.");
        System.out.println("2.  View the average reading for a phenomeon.");

        int selection = scanner.nextInt();

        while (selection!=0) {
            if (selection==1) {
                System.out.println("Enter the phenomenon name and value (e.g. 'temperature 7'):");
                if (scanner.hasNext()) {
                    final String phenomenon = scanner.next();
                    if (scanner.hasNextDouble()) {
                        final double reading = scanner.nextDouble();
                        station.recordReading(phenomenon, reading);
                    }
                    else {
                        System.out.println("Couldn't process phenomenon reading. Something wrong?");
                    }
                }
                else {
                    System.out.println("Couldn't process phenomeon name. Something wrong?");
                }                
            }
            else if (selection==2) {
                System.out.println("Enter the phenomenon name:");
                if (scanner.hasNext()) {
                    final String phenomenon = scanner.next();
                    System.out.printf("The average value for phenomeon %s is %.2f.\n", phenomenon , station.average(phenomenon));
                }
                else {
                    System.out.println("Couldn't process phenomeon name. Something wrong?");
                }
            }
            else {
                System.out.println("That selection was not recognised.");
            }

            System.out.println("\nMake a selection and press return:");
            System.out.println("0.  Quit");
            System.out.println("1.  Record a phenomenon measurement.");
            System.out.println("2.  View the average reading for a phenomeon.");

            selection = scanner.nextInt();
        }
        scanner.close();
    }
}

Collat​​or类处理这些值并获取平均值,记录的最小值和最大值以及现象的读数次数。

/**
 * A Collator object stores statistics on a series 
 * of readings, specifically, the number, maximum, minimum, and average.
 * A Collator has a label, usually the name of the phenomenon that the readings
 * represent e.g. temperature, pressure.
 */
public class Collator {

    private final String label;
    private int numReadings;
    private double average;
    private double maximum;
    private double minimum;

    public Collator(final String label) {
        this.numReadings = 0;
        this.label = label;
    }

    public String label() { return label; } 

    public void recordReading(final double reading) {
        if (numReadings<1) {
            // This must be the first reading
            this.average = reading;
            this.maximum = reading;
            this.minimum = reading;
            this.numReadings = 1;
        }
        else {
            // Calculate the current total
            double total;
            total = this.average*this.numReadings;

            // Calculate the new total and update the number of readings
            total = total+reading;
            this.numReadings++;

            // Calculate the new average
            this.average = total/this.numReadings;

            // Check for a new maximum
            if (reading>this.maximum) {
                this.maximum = reading;
            }

            // Check for a new minimum
            if (reading<this.minimum) {
                this.minimum = reading;
            }
        } 
    }

    public double maximum() {
        assert this.numReadings>0 : "Collator:maximum(): number of readings is zero.";
        return this.maximum;
    }

    public double minimum() {
        assert this.numReadings>0 : "Collator:minimum(): number of readings is zero.";
        return this.minimum;
    }

    public double average() {
        assert this.numReadings>0 : "Collator:average(): number of readings is zero.";
        return this.average;
    }

    public int numberOfReadings() { return this.numReadings; } 
}

样本I / O:

监控站测试线束
输入电台名称:
斑马
输入要记录的现象的逗号分隔列表:
高度宽度
进行选择,然后按回车键:
0.退出
1.记录现象测量。
2.查看现象的平均读数。
1个
输入现象名称和值(例如“温度7”):
高度13.6
进行选择,然后按回车键:
0.退出
1.记录现象测量。
2.查看现象的平均读数。
1个
输入现象名称和值(例如“温度7”):
高度12
进行选择,然后按回车键:
0.退出
1.记录现象测量。
2.查看现象的平均读数。
1个
输入现象名称和值(例如“温度7”):
宽100
进行选择,然后按回车键:
0.退出
1.记录现象测量。
2.查看现象的平均读数。
2
输入现象名称:
高度
现象高度的平均值是12.80。
进行选择,然后按回车键:
0.退出
1.记录现象测量。
2.查看现象的平均读数。
0

只需创建一个接收数组并计算内容的公共方法,如下所示:

public double calculateAverage(double[] arraysName){

    //calculate it using the arraysName

    return average;
}

因此,在创建具有此方法的对象时,可以在此处调用它。
我不确定这是否是您想要的答案,如果您已经执行了发布的代码,这对我来说似乎很基础,我可能有错误的想法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM