I would like some help to index through an empty array and fill it with 10 entries one at a time. The objective of the task is to create a GUI with two buttons, one (Store) to save the value into array position and second (Quit) to run statistics calculations on stored inputs. The calculations code works because I tested it with hard coded data...in other words this code line produces the expected results for mean, sum, above and below average.
static int[] occupants = {3,0,4,1,2,3,2,3,0,2};
What I really want to do is every time I enter array position and value, it is stored and calculations performed. Then repeat the process for each of the values above (or desired entries except that there is a maximum 10 positions. Work in progress program is below.
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUIStats {
static final int BUILDING_SIZE = 10; // No. of appartment constant declared
static int[] occupants = new int[BUILDING_SIZE]; // Allocate appartment array variable
//static int[] occupants = {3,0,4,1,2,3,2,3,0,2}; // add this (manual array input)
static final int MAX_OCCUPANTS = 20; // Max occupants constant defined
static int sum = 0; // Declare total results holder
static double mean = 0; // Declare average results holder
static int above = 0; // Declare above average results holder
static int below = 0; // Declare below average result holder
static int index = 0;
static int total = 0;
static JFrame outFrame; // Declare a frame
static Container outPane; // Declare content pane of frame
static JButton store; // Declare a store button
static JButton quit; // Declare a quit button
static int ApartmentInput; // Stores apartment entry in I/O text field
static int OccupantsInput; // Stores occupants entry in I/O text field
static NumericHandler operation; // Declare the listener
// Declare Labels for display statements and results in GUI frame
static JLabel totalRegistry;
static JLabel sumRegistry;
static JLabel meanRegistry;
static JLabel aboveRegistry;
static JLabel belowRegistry;
static JLabel testresult;
static JLabel totresult;
static JLabel avgresult;
static JLabel abvresult;
static JLabel belresult;
static JLabel aptinput;
static JLabel occinput;
static JLabel storeinput;
static JLabel calcresult;
// Declare input I/O fields for GUI frame
static JTextField aptL;
static JTextField occR;
// ************************ Start the listening process here ****************************
static class NumericHandler implements ActionListener {
// Listener for the operations buttons
public void actionPerformed(ActionEvent event) {
// Converts string in inputField to integer value
String whichButton; // This variable hold's the button's name
index = ApartmentInput;
//ApartmentInput = Integer.parseInt(aptL.getText());
//OccupantsInput = Integer.parseInt(occR.getText());
ApartmentInput = Integer.valueOf(aptL.getText()).intValue();
OccupantsInput = Integer.valueOf(occR.getText()).intValue();
whichButton = event.getActionCommand(); // Gets the button's name
if (whichButton.equals("Store")) { // Proceeds on the basis of store button
for (index = 0; index < occupants.length; index++) { // Set each data variable to its own index position
occupants[index] = OccupantsInput; // Reads in the occupants inputs
}
total = total + OccupantsInput;
totalRegistry.setText("" + total);
aptL.setText("0"); // Keeps input fields cleared
occR.setText("0"); // Keeps input fields cleared
System.out.println("Stored total is: " + total); // *****Just checking reaction
System.out.println("Occupants in APT " + ApartmentInput + " is: " + occupants[OccupantsInput]);
}
else // Otherwise the quit button
{
//********************** Begin the statistical permutations here *************************
// The following calculated results produces two values for the above and below averages
// (1) Showing the assignment requirements and (2) revealing what the above and below
// actual array values are and printing out on screen - outside of the GUI. All other
// results are also printed out onscreen. The assignment requirements are
// only displayed within the generated GUI.
for (int index = 0; index < occupants.length; index++) {
// Calculate sum
sum += occupants[index];
}
sumRegistry.setText("" + sum);
System.out.println("The sum is: " + sum);
// Calculate average
mean = sum / occupants.length;
String output = "The array is: ";
for (int index = 0; index < occupants.length; index++) {
output += occupants[index] + " ";
}
output += "\nThe mean is: " + mean;
meanRegistry.setText("" + mean);
System.out.println(output);
// Calculate above mean
String out = "The numbers above average are: ";
int above = 0;
for (int index = 0; index < occupants.length; index++) {
if (occupants[index] > mean) {
out += occupants[index] + " ";
above++;
}
}
aboveRegistry.setText("" + above);
System.out.println(out);
System.out.println("Above average occupancy number: " + above);
// Calculate below mean
String out1 = "The numbers below average are: ";
int below = 0;
for (int index = 0; index < occupants.length; index++) {
if (occupants[index] < mean) {
out1 += occupants[index] + " ";
below++;
}
}
belowRegistry.setText("" + below);
System.out.println(out1);
System.out.println("Below average occupancy number: " + below);
}
}
}
//********************* End of the statistical permutations here *************************
// ***************************End of the listening process *******************************
Thanks for the help.