/*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

Recommended Answers

All 4 Replies

Ok, so what is the question?

and why do this in Java? It's purely structural programming, far more suited to Pascal or C.

Here is how the program should work:

Offer the user the following 10 menu options (they must enter the option#)

1. Insert "n" values in the array starting at position 1
2. insert "n" value into the array as long as there is room
3. print all user-entered values (must show both index and value at index)
4. return average(not sum) and (must not loop over locations not being used)
5. return minimum
6. return number of times user-chosen value occurs in the array.
7. change a value of one array element chosen by index number
8. return the first position a given values occurs
9. delete the value in the user-defined position e.g (1=index1 etc), and compact the array
10. quit the program.

NOTE: all the above must be done using Switch Statement and JOptionPane.

Ok, you still haven't posted a question. We are not going to read all of the requirements and make sure that all of your code meets those requirements. That's your instructors job - grading your work.

If you have a question, state it clearly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.