(// See my program below-after the question)
The program is supposed to do the following:
Repeatedly offer the user the following 10 menu options (they must enter the option number):
(1) insert n values in the array, starting with position 1.
(2) insert n values immediately after the last entered value (as long as there is room in the array)
(3) print all the user-entered values in the array on one screen or in one pop-up window
(4) return the average (not sum) of the user-entered array values
(must not loop over locations not currently being used).
(5) return the minimum (not maximum) of the user-entered values.
(6) return the number of times a user-chosen value occurs in the array.
(7) change the value of one array element (chosen by index number)
(8) return the first position a given value occurs (-1 if it's not in the array)
(9) delete the value in a user-defined position (1 = index 1, 2 = index 2, etc.)
Compact the rest of the array (i.e. Move all the values after the deleted
down by one place towards position 1.)
(10) quit the program
Each of the options should be implemented as a method that is called from a switch statement in the main method. All input should be obtained via the getNumber() method. All output should be done via a printResults method (that has one String parameter, the message).
// Here is the program I have tried to write, but of course it's not running!!
import javax.swing.JOptionPane;
import java.io.*;
public class ArrayAssignment1 {
public static void main(String args[])
{
final int MAX_VALUES = 11;
final int QUIT = 10; //option used to quit the menu and exit
int userChoice; // holds option picked by user from menu
int [] theArray = new int[MAX_VALUES]; //NOTE ARRAY SET UP BEFORE MENU LOOP
int theValue,
index,
returnValue, newValue;
float returnFloatValue;
// beginning of executable statements
userChoice = showMenu();
while (userChoice != QUIT)
{
switch (userChoice)
{
case 1: index = 0;
break;
case 2: for (int i= 0; i < 10; i++)
break;
case 3: System.out.println (theArray);
break;
case 4: if (theArray != null)
System.out.println("Average of user-entered values is: " + returnFloatValue / 11 );
break;
case 5: for (int i=0; i<10; i++)
if (index < 10)
System.out.print("The min is: " + newValue);
break;
case 6: theValue = getNumber("For which array value are we searching?");
for ( int i=0; i < 10; i++ )
if ( userChoice <= 10) // Value found.
System.out.println(theValue + " occurred " + returnValue +
"times in the array");
break;
case 7: index = getNumber(" At which index is the value being changed?");
System.out.print(" What's the new value?");
System.out.println(theValue + " index " + returnValue);
break;
case 8: System.out.print("What number are we looking for?");
BufferedReader getNumber =
new BufferedReader (new InputStreamReader (System.in));
System.out.print (theValue + " first occurs at position " + returnValue);
break;
case 9: theValue = getNumber("From which position are we deleting?");
for (int i=0; i<10; i++)
if (index == userChoice)
break;
for(int i=0; newValue < 10 - 1; index++)
userChoice += 1;
newValue--;
System.out.println (index + " ");
} // 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 printResults(String message)
{
System.out.println(message);
}
static int showMenu()
{
String message = "Here are your Choices: \n"
+ "Enter 1 to add Values into the Array \n"
+ "Enter 2 to add Values immidiately after the last \n"
+ "Enter 3 to show the values entered by the user \n"
+ "Enter 4 to calculate the avarage of the values entered\n"
+ "Enter 5 to the minimum value entered\n"
+ "Enter 6 to show the number of times you chose a choice\n"
+ "Enter 7 to modify a choice \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