// imports allow you to use code already written by others.  It is good to explore and learn libraries.  The names around the dots often give you a hint to the originator of the code.
import java.util.Scanner; //library for user input
import java.lang.Math; //library for random numbers


public class Menu {
    // Instance Variables
    public final String DEFAULT = "\u001B[0m";  // Default Terminal Color
    public final String[][] COLORS = { // 2D Array of ANSI Terminal Colors
        {"Default",DEFAULT},
        {"Red", "\u001B[31m"}, 
        {"Green", "\u001B[32m"}, 
        {"Yellow", "\u001B[33m"}, 
        {"Blue", "\u001B[34m"}, 
        {"Purple", "\u001B[35m"}, 
        {"Cyan", "\u001B[36m"}, 
        {"White", "\u001B[37m"}, 
    };
    // 2D column location for data
    public final int NAME = 0;
    public final int ANSI = 1;  // ANSI is the "standard" for terminal codes

    // Constructor on this Object takes control of menu events and actions
    public Menu() {
        Scanner sc = new Scanner(System.in);  // using Java Scanner Object
        
        this.print();  // print Menu
        boolean quit = false;
        while (!quit) {
            try {  // scan for Input
                int choice = sc.nextInt();  // using method from Java Scanner Object
                System.out.print("" + choice + ": ");
                quit = this.action(choice);  // take action
            } catch (Exception e) {
                sc.nextLine(); // error: clear buffer
                System.out.println(e + ": Not a number, try again.");
            }
        }
        sc.close();
    }

    Scanner input = new Scanner(System.in);
    Scanner inputStream = new Scanner(System.in);
    String userInputStr;
    int numofNum, userNum, fact, tempType;
    double  celcius, farenheit, kelvin, medianNum, sdNumber;
    char endReply; 
    public static final String REPORT = "\nFactorial of "; 
    public static final String FCONVERT = "\nConverted to Farenheit: ";
    public static final String KCONVERT = "\nConverted to Kelvin: ";
    

    //median
    public static double findMed(double arr[], int n)
    {
        // sort the array
        Arrays.sort(arr);
 
        // check for even case
        if (n % 2 != 0)
        {
            return (double)arr[n / 2];
        }
        return (double)(arr[(n - 1) / 2] + arr[n / 2]) / 2.0;
    }

    //factorial
    public static int findFact(int n)
    {
      int f = 1;
      for (int i=1; i<n+1; i++)
      {
         f = f * i;
      }
      return f;
    }

    // standard deviation
    public static double sdcalc (double[] numsSD) {
        double sum = 0.0;
        for (int i=0; i<numsSD.length; i++) {
            sum += numsSD[i];
        }
        double average = sum/numsSD.length;
        double sdsum = 0.0;
        double standev = 0.0;
        for (int j=0; j<numsSD.length; j++) {
            sdsum += Math.pow(numsSD[j]-average, 2);
            standev = Math.sqrt(sdsum/numsSD.length);
        }
        return standev;
    }

    // Print the menu options to Terminal
    private void print() {
        //System.out.println commands below is used to present a Menu to the user. 
        System.out.println("-------------------------\n");
        System.out.println("Choose from these choices");
        System.out.println("-------------------------\n");
        System.out.println("1 - Factorial");
        System.out.println("2 - Temperature Converter");
        System.out.println("3 - Median");
        System.out.println("4 - Number Sort");
        System.out.println("5 - Standard Deviation");
        System.out.println("0 - Quit");
        System.out.println("-------------------------\n");
    }

    // Private method to perform action and return true if action is to quit/exit
    private boolean action(int selection) {
        boolean quit = false;

        switch (selection) {  // Switch or Switch/Case is Control Flow statement and is used to evaluate the user selection
            case 0:  
                System.out.print("Goodbye, World!");
                quit = true;
                break;
                  
            case 1:
                System.out.print("Factorial ");
                System.out.print("\nInput an integer: ");
                userInputStr = input.nextLine();
                userNum = Integer.parseInt(userInputStr);
                fact = findFact(userNum);
                System.out.print(REPORT + userNum +" is "+fact);
                break;
            
            case 2:
                // initialize variables
                celcius = 0.;
                farenheit = 0.;
                kelvin = 0.;

                System.out.print("This program converts degress in Celcius to Farenheit or Kelvin \n");
                
                // get degress in celcius from user
                System.out.print("\nTemperature in Celcius: ");
                userInputStr  = inputStream.nextLine();
                celcius = Double.parseDouble(userInputStr);
                System.out.print(celcius);

                // input from user, what do they want to convert to
                while(true)
                {
                    System.out.print("\nConvert to (1)Farenheit or (2)Kelvin: ");
                    userInputStr = inputStream.nextLine();
                    tempType = Integer.parseInt(userInputStr);
                    if ((tempType == 1) || (tempType == 2))
                    {
                        break;
                    }
                    else
                    {
                        System.out.print(tempType);
                        System.out.println("\nPlease input either 1 or 2");
                    }
                }
                System.out.print(tempType);

                //conversion
                if (tempType == 1) //convert to farenheit
                {
                    farenheit = (celcius*1.8)+32;   //multiple by 1.8 and then add 32, result is a double
                    System.out.print(FCONVERT);
                    System.out.print(farenheit);
                }
                else   //convert to kelvin
                {
                    kelvin = celcius + 273.15;    //add 273.15, result is a double
                    System.out.print(KCONVERT);
                    System.out.print(kelvin);
                }
                break; 
            
            case 3:
                System.out.print("Median ");
                System.out.print("\nhow many values would you like to input?");
                userInputStr = inputStream.nextLine();
                numofNum = Integer.parseInt(userInputStr);
        
                double[] arr = new double [numofNum]; //creates array with doubles, numofNum number of values in the array
                for (int i=0; i<numofNum; i++)       //gets info from user for each spot in array
                {
                    System.out.print("\nenter value: ");
                    userInputStr = inputStream.nextLine();
                    arr[i] = Double.parseDouble(userInputStr);
                    System.out.print(arr[i]);
                }
                int n = arr.length;
                System.out.println("\nMedian: " + findMed(arr, n));
                break;

            case 4:
                System.out.print("Number Sort ");
                System.out.print("\nhow many values would you like to input?");
                userInputStr = inputStream.nextLine();
                numofNum = Integer.parseInt(userInputStr);
        
                double[] arr2 = new double [numofNum];  //creates array with doubles, numofNum number of values in the array
                for (int i=0; i<numofNum; i++)       //gets info from user for each spot in array
                {
                    System.out.print("\nenter value: ");
                    userInputStr = inputStream.nextLine();
                    arr2[i] = Double.parseDouble(userInputStr);
                    System.out.print(arr2[i]);
                }
        
                Arrays.sort(arr2);   //sort array
                System.out.println("\nSorted Numbers: ");
                for (int i=0; i<numofNum; i++)       //prints each spot in array
                {
                    System.out.print(arr2[i]+ ", ");
                }
                break;
            
            case 5:
                System.out.print("Standard Deviation");
                System.out.print("\nhow many values would you like to find the standard deviation of?");
                userInputStr = input.nextLine();
                numofNum = Integer.parseInt(userInputStr);
                double[] numsSD = new double [numofNum]; //creates array with doubles, nomofNum number of values in the array
                for (int i=0; i<numofNum; i++)
                {
                    System.out.print("\nenter value:");
                    userInputStr = input.nextLine();
                    numsSD[i] = Double.parseDouble(userInputStr);
                    System.out.print(numsSD[i]);
                }
                sdNumber = sdcalc(numsSD);
                System.out.print("\nthe standard deviation is "  +sdNumber);
                break;

            default:
                //Prints error message from console
                System.out.print("Unexpected choice, try again.");
        }
        System.out.println(DEFAULT);  // make sure to reset color and provide new line
        return quit;
    }

    // Static driver/tester method
    static public void main(String[] args)  {  
        new Menu(); // starting Menu object
 
    }

}
Menu.main(null);
-------------------------

Choose from these choices
-------------------------

1 - Factorial
2 - Temperature Converter
3 - Median
4 - Number Sort
5 - Standard Deviation
0 - Quit
-------------------------

1: Factorial 
Input an integer: 
Factorial of 7 is 5040
2: This program converts degress in Celcius to Farenheit or Kelvin 

Temperature in Celcius: 35.0
Convert to (1)Farenheit or (2)Kelvin: 2
Converted to Kelvin: 308.15
3: Median 
how many values would you like to input?
enter value: 1.0
enter value: 6.0
enter value: 5.0
enter value: 7.0
Median: 5.5

4: Number Sort 
how many values would you like to input?
enter value: 2.0
enter value: 2.0
enter value: 1.0
Sorted Numbers: 
1.0, 2.0, 2.0, 
5: Standard Deviation
how many values would you like to find the standard deviation of?
enter value:3.0
enter value:3.0
enter value:4.0
enter value:8.0
enter value:99.0
the standard deviation is 37.844946822528364
0: Goodbye, World!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Timer;
import java.util.TimerTask;
// Graphical-User-Interface for Desktop in Java using Java Swing. 
public class MenuJFrame extends JFrame implements ActionListener {
    private JFrame frame;
    private JMenuBar menubar;
    private JMenu menu;
    private JLabel message = new JLabel("Click on Menu to select an action.");
    public final String[] MENUS = { // 1D Array of Menu Choices
        "Hello", "Colors", "Loading bar",  
    };
    // Statics to assist with timer and messaging, single copy (no instance)
    private	static int delay = 20;
    private	static int step = 1;
    private static String hashes = "";

    // Constructor enables the Frame instance, the object "this.frame"
    public MenuJFrame(String title) {
	    // Initializing Key Objects
        frame = new JFrame(title);
	    menubar = new JMenuBar();
	    menu = new JMenu("Menu");

        // Initializing Menu objects and adding actions
        for (String mx : MENUS) {
            JMenuItem m = new JMenuItem(mx);
            m.addActionListener(this);
            menu.add(m); 
        }

        // Adding / Connecting Objects
        menubar.add(menu);
        frame.setJMenuBar(menubar);
        frame.add(message);

        // Sets JFrame close operation to Class variable JFrame.EXIT_ON_CLOSE
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        // set the size of window based on objects
        frame.setSize(300,200);

        // makes the frame object visible according to properties previously set
        frame.setVisible(true);  // flow of control shifts to frame object
    }

    // event from user selecting a menu option
    public void actionPerformed(ActionEvent e) {
        // local variable to ActinEvent
        String selection = e.getActionCommand();  // menu selection
        String msg; // local variable to create response from action
        final String[] COLORS = {"Red", "Green", "Blue"};  // add more colors here
 	    final String start_msg = "<html>";  // html building
       	final String end_msg = "</html>";
       	final String hash = "#";

        // run code based on the menuItem that was selected
        if ( selection.equals(MENUS[0]) ) {  // Hello Action
            msg = "Hello, World";
            message.setText(msg);
        } else if ( selection.equals(MENUS[1]) ) { // Color Action
            msg = start_msg + "<p>" + selection + "</p>";
            for (String color : COLORS) {
                msg += "<font color=" + color + ">" + color + " </font>";
            }
            msg += end_msg;
            message.setText(msg);
        } else {  // Loading Bar Action
	    String loading = "<p>Loading</p>";
            // Code to run on a Timer
            Timer timer = new Timer();
            TimerTask task = new TimerTask() {
                public void run() {  // Method for TimerTask
                    // Static and Local variables used to manage message building
                    int random = (int) (Math.random() * COLORS.length);  // random logic
                    MenuJFrame.hashes +=  "<font color=" + COLORS[random] + ">" + hash + "</font>";
                    String msg = start_msg + loading + hashes + end_msg;
                    message.setText(msg);
                    
	  	            // Shutdown timer and reset data
                    if(MenuJFrame.step++ > MenuJFrame.delay) {
                        MenuJFrame.step = 1; MenuJFrame.hashes="";
                        timer.cancel();
                    }
                };
            };
            // Schedule task and interval
            timer.schedule(task, 200, 200);
            message.setText(start_msg + loading + hash + end_msg);  // prime/initial display
        }
    }

    // Driver turn over control the GUI
    public static void main(String[] args) {
        // Activates an instance of MenuJFrame class, which makes a JFrame object
        new MenuJFrame("Menu");
    }
}
MenuJFrame.main(null);
The Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click <a href='https://aka.ms/vscodeJupyterKernelCrash'>here</a> for more info. View Jupyter <a href='command:jupyter.viewOutput'>log</a> for further details.