|
|
|
The example problem, other than giving a practical illustration of a menu interface, also serves as a revision of the Java inheritance mechanism and the use of the round method found in the Math class.
1. INTRODUCTION |
A common "interface" requirement is to allow a user to select from a number of alternatives. A simple way of doing this is to present the user with a list of possibilities from which to choose from (this type of interface is less error prone). In sophisticated "window" interfaces the selection is made through mouse clicks. An alternative and simpler interface is to allow the user to indicate a selection by entering a number or letter at the keyboard. The disadvantage associated with this simpler approach is that the user may make erroneous inputs and therefore the interface must include some error recovery mechanism. |
Consider the Java application program given in Table 1. Note that it comprises a "top level" main method and two other methods, outputMenu and processSelector. The first outputs a list of options (i.e. a menu not unlike a restaurant menu) from which the user can make a single selection by entering the appropriate number associated with the desired option. The selection is then processed by the processSelector method which includes an error recovery mechanism which will cause the program to repeat until a valid option is entered. Some example output is presented in Table 2 and an Activity Diagram for the code in Figure 1. |
// MENU INTERFACE EXAMPLE APPLICATION // Frans Coenen // Thursday 15 April 1999 // Revised: Tuesday 26 July 2005 // The University of Liverpool, UK import java.util.*; class MenuInterfaceExApp { // ------------------- FIELDS ------------------------ // Create Scanner class instance private static Scanner input = new Scanner(System.in); // ------------------ METHODS ------------------------ /* Main method */ public static void main(String[] args) { // Output menu int selector = outputMenu(); // Process selection processSelector(selector); } /* Output menu */ private static int outputMenu() { System.out.println("PROGRAMMING PARADIGMS"); System.out.println("====================="); System.out.println("1. Imperative"); System.out.println("2. Object oriented"); System.out.println("3. Logic"); System.out.println("4. Functional"); System.out.println("Select option: "); // Return return(input.nextInt()); } /* Process selector. If unrecognised selection output error message and repeat. */ private static void processSelector(int selector) { switch (selector) { case 1: System.out.println("Example languages include C, Ada and " + "Pascal"); break; case 2: System.out.println("Example languages include Java, Eiffel " + " and C++"); break; case 3: System.out.println("Example languages include Prolog"); break; case 4: System.out.println("Example languages include Lisp and " + " Miranda"); break; default: System.out.println("ERROR: Unrecognised menu selection " + selector + "!"); selector = outputMenu(); processSelector(selector); } } } |
Table 1: Example Java application program illustrating menu interface
Figure 1: Activity diagram for code presented in Table 1
$ java MenuInterfaceExApp PROGRAMMING PARADIGMS ===================== 1. Imperative 2. Object oriented 3. Logic 4. Functional Select option: 5 ERROR: Unrecognised menu selection 5! PROGRAMMING PARADIGMS ===================== 1. Imperative 2. Object oriented 3. Logic 4. Functional Select option: 2 Example languages include Java, Eiffel and C++ |
Table 2: Example output from application program given in Table 1
Created and maintained by Frans Coenen. Last updated 10 February 2015