Modify the stats class created in the previous assignment to allow it to store and analyze data sets of different sizes. it should now have an array that holds 100 double values instead of 12. An instance variable numElt will keep track of how many elements in the atrray are being used by a particular instance of the class. All of the Stats class functions should be revised to work with numElt elements instead of 12.
Can someone please guide me as to what exactly i need to do. im kinda confused.
public class Stats { // records array field created to store twelve values private double [] records = new double [12]; // method to set values in array public void setValue(int rainfall, double actValue ) { records[rainfall] = actValue; } public void processRainfallStats() { System.out.printf("The total rainfall for the year is %.2f\n\n", getTotal()); System.out.printf("The average rainfall for the year is %.2f\n\n", getAverage()); System.out.printf("The least rainfall for the year is %.2f\n\n", getSmallest()); System.out.printf("The most rainfall for the year is %.2f\n\n", getLargest()); } // method to calculate total public double getTotal() { double total = 0; for (double tot : records) {total += tot;} return total; } // method to calculate average rainfall in records array public double getAverage() { double average; int total = 0; for (double avg: records) {total += avg;} average = total / records.length; return average; } // method to find largest index value in records array public double getLargest() { double largest = records[0]; // assuming that records[0] is the largest for (double Lag : records) { if (Lag > largest) {largest = Lag;} } return largest; } // method to find smallest index value in records array public double getSmallest() { double smallest = records[0]; // assuming that records[0] is the smallest for (double small: records) { if (small < smallest) {smallest = small;} } return smallest; } }
package leedanielassignment4; import java.util.Scanner; public class StatsTest { public static void main(String[] args) { Scanner input = new Scanner(System.in); // new object of Stats class created Stats myStats = new Stats(); int rainfall; double actValue; // new array of months to be store months String [] months = new String[] {"January","February","March","April","May","June","July","August", "September","October","November","December"}; for (int i = 0; i < months.length; i++) { // print method used to set value to month System.out.printf("Please eneter the amount of rainfall for %s:\n ", months[i]); rainfall = i; actValue = input.nextDouble(); if(actValue < 0) {actValue = 0;} // object calling method in class to initiate values myStats.setValue(rainfall, actValue); } // object calls method of stats class to print info myStats.processRainfallStats(); }