/*This is a what I've written so far- see below.*/
/*The program is to ask a user to insert values into array,
print those values showing both index # and corresponding value, return average, min, change value of 1 array element, delete 1 element,
compact the array. ALL these MUST use the JOptionPane and SWITCH statement.*/
import javax.swing.JOptionPane;
import java.util.Scanner;
public class weightArray {
public static void main(String args[])
{
final int MAX_WEIGHT = 11; // allocates memory for 11 integers
final int QUIT = 10; //option used to quit the menu and exit
int userChoice; // holds option picked by user from menu
int [] weightArray = new int [MAX_WEIGHT]; // The array
int option,
theValue,
index,
returnValue, newValue;
double returnDoubleValue;
double[] weight; // array to hold weights
String outputString = "";
userChoice = showMenu();
while (userChoice != QUIT)
{
switch (userChoice)
{
case 1: // input setup
outputString = JOptionPane.showInputDialog("Please enter values into the array\n");
brea
case 2: theValue = Integer.parseInt( outputString );
outputString = JOptionPane.showInputDialog("Enter another value\n");
break;
case 3: System.out.println("\nThe values you entered are:");
JOptionPane.showMessageDialog(null, outputString);
break;
case 4: // average: finds the average of the array elements weight = new double [MAX_WEIGHT]; System.out.println("The average weight is: " + averageWeight(weight));
printWeight(weight); //output the weight
break;
case 5: int min = weightArray[0];
for(int i = 1, limit = weightArray.length; i < limit; ++i)
{
if(weightArray[i] < min)
min = weightArray[i];
}
System.out.println("The minimum is " + min);
break;
case 6: theValue = Integer.parseInt(JOptionPane.showInputDialog
("For which array value are we searching?"));
for ( int i=1; i < 10; i++ )
if ( userChoice <= 10) // Value found.
System.out.println(theValue + " occurred " + "times in the array");
break;
case 7: index = Integer.parseInt(JOptionPane.showInputDialog
("At which index is the value being changed?"));
for (int i = 1; i < MAX_WEIGHT; i++)
weightArray[i] = i++;
System.out.println("What's the new value?");
break;
case 8: theValue = Integer.parseInt(JOptionPane.showInputDialog
("What number are we looking for?"));
System.out.print (theValue + " first occurs at position: ");
break;
case 9: theValue = Integer.parseInt(JOptionPane.showInputDialog
("From which position are we deleting?"));
for (int i=1; i<10; i++)
userChoice += 1;
theValue--;
System.out.println (weightArray + " ");
break;
} // end of switch
userChoice = showMenu(); // get next option from the user
} // end of while
System.exit(0);
}
static int getNumber(String message)
{
return Integer.parseInt(JOptionPane.showInputDialog(message));
}
static void printWeight(double[] weight)
{
// This method prints the contents of the array of weights
int numberOfWeights; // number of values stored in the array
String outputString; // string to be output
numberOfWeights = (int) weight[0]; // initialize number of values outputString = "Array\n";
for (int index = 1; index <= numberOfWeights; index++)
{
outputString = outputString + weight[index] + "\n";
}
JOptionPane.showMessageDialog(null, outputString);
return;
}; // end method printWeight
static double averageWeight(double [] table)
{
// the first position starts at 0
double sum = 0;
for (int i = 1; i <= table[0]; ++i)
sum = sum + table[i];
return (sum/table[0]);
}
static int showMenu()
{
String message = "Here are your Choices: \n"
+ "Enter 1 to add Values into the Array \n"
+ "Enter 2 to add Values immediately after the last \n"
+ "Enter 3 to show the values entered by the user \n"
+ "Enter 4 to calculate the average of the values entered\n"
+ "Enter 5 to get the minimum value entered\n"
+ "Enter 6 to show the number of times you chose a value\n"
+ "Enter 7 to modify a value \n"
+ "Enter 8 to return the first position a value occurs\n"
+ "Enter 9 to delete a value in a position\n"
+ "Enter 10 to Quit \n\n";
return getNumber(message);
}
} // end of class