CREATING MENUS IN JAVA Mimi Opkins CECS 174. Menus Menus work well for console applications. The menu can be contained within a do-while loop and the.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

PSEUDOCODE & FLOW CHART
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Topic 03 Control Statements Programming II/A CMC2522 / CIM2561 Bavy Li.
Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
Chapter 4 - Control Structures: Part 1 Outline 4.4Control Structures 4.5The if Selection Structure 4.6The if/else Selection Structure 4.7The while Repetition.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Loop variations do-while and for loops. Do-while loops Slight variation of while loops Instead of testing condition, then performing loop body, the loop.
1 Lab Session-7 CSIT-121 Fall Revising Structured Choice 4 The While Loop variations 4 Lab Exercises.
1 Lab Session-6 CSIT-121 Spring 2005 Structured Choice The do~While Loop Lab Exercises.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
1 Lab Session-7 CSIT-121 Fall Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises.
CS 106 Introduction to Computer Science I 09 / 28 / 2007 Instructor: Michael Eckmann.
Introduction to Programming G51PRG University of Nottingham Revision 2 Essam Eliwa.
LAB 10.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Lesson 7: Improving the User Interface
Java Programming: From the Ground Up
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
Chapter 5 Loops.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
Switch Statement Is a selection control structure for multi-way branching. SYNTAX switch ( IntegralExpression ) { case Constant1 : Statement(s); // optional.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Intro to Java Week 10 Menu-driven programs. Contents Multi-way selection Loop with post-condition Menu program Sub-procedures Alarm function.
COMP 110 switch statements and while loops Luv Kohli September 10, 2008 MWF 2-2:50 pm Sitterson
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Chapter 4: Control Structures II
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
CS110 Programming Language I Lab 4: Control Statements I Computer Science Department Spring 2014.
J AVA P ROGRAMMING 2 C H 03: C ONTROL STATEMENTS if, for loop (review) switch, while, do while break, continue Fall Java Programming.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exercise 2 Introduction to C# CIS Create a class called Employee that contains the following private instance variables: Social Securitystring.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Error Handling Tonga Institute of Higher Education.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Java Methods 11/10/2015. Learning Objectives  Be able to read a program that uses methods.  Be able to write a write a program that uses methods.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING For Loop.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
JAVA PROGRAMMING Control Flow. Jeroo: Finish Activities 1-7 Finish InputTest program w/changes.
Loops, Part II IT108 George Mason University. Indefinite Loop Don’t always have access to the number of iterations ahead of time If a condition (user-response,
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
CSC111 Quick Revision.
Maha AlSaif Maryam AlQattan
Chapter 5: Control Structures II
Java 24th sep /19/2018 CFILT.
Something about Java Introduction to Problem Solving and Programming 1.
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
INPUT STATEMENTS GC 201.
Control Statement Examples
The Basics of Recursion
Looping III (do … while statement)
Presentation transcript:

CREATING MENUS IN JAVA Mimi Opkins CECS 174

Menus Menus work well for console applications. The menu can be contained within a do-while loop and the do-while loop should be contained within main(). The menu itself is coded as a switch-case statement. 12/12/2015 2

Menu-Driven Application Suppose you want to design an application that will create and maintain a list of students. The student information consists of the student's identification number, first and last name. The menu selections presented to the user are: 1. Enter a student into the list. 2. Delete a student from the list. 3. Locate and display a student. 4. Clear the list of all students. 5. Display all students in id. number order. 6. Exit the program. 12/12/2015 3

Flowcharting Menu Application Prior to writing any code, create a flowchart of the top- level flow of control for your program. Once the top-level flowchart is completed, top-down design can be used to break down each task into subtasks. Lower level flowcharts can be created for each sub-level design. 12/12/2015 4

5 Declare and initialize variables for the menu option, student and list Display the purpose of the program Display the menu and prompt the user for a menu option menu option = 1 menu option = 2 Prompt for student info. Insert student into list Prompt for id. number Remove student from list

12/12/ menu option = 3 menu option = 4 menu option = 5 menu option = 6 Invalid menu option Prompt for id. number Retrieve student from list Clear list Display list Display program terminating Exit program Display invalid option message Display student

import java.util.Scanner; public class StudentListTest { public static void main(String args[]) { // Declaration and initialization of variables student s; list l = new list(); //Display the program’s purpose System.out.println("This program will allow the user to create and insert"); System.out.println( "a student into a list, retrieve a student from the list"); System.out.println("for display and delete a student from the list. The"); System.out.println("user can also clear the list and display all students"); System.out.println("in the list."); 12/12/2015 7

do { // Display the menu and get the menu option int menuOption=0; System.out.println("\n Student List Menu Options"); System.out.println("1. Enter a student into the list."); System.out.println("2. Delete a student from the list."); System.out.println("3. Locate and display a student."); System.out.println("4. Clear the list of all students."); System.out.println("5. Display all students in id. number order."); System.out.println("6. Exit the program."); System.out.println(“What do you want to do?”); menuOption = keyboard.nextInt(); keyboard.nextLine(); 12/12/2015 8

9 switch (menuOption) { case 1:// Insert a student into the list // Prompt the user for the student's information s = create_student(); // Insert the student into the list l.insert(s); break; case 2:// Remove the student from the list // Prompt the user for the student's // identification number s = get_id_number(); // Remove the student from the list l.remove(s); break; case 3:// Locate a student in the list // Prompt the user for the student's // identification number s = get_id_number(); // Retrieve the student from the list l.retrieve(s); // Display the student information System.out.printf("%s", s); break;

12/12/ case 4:// Clear the list l.clearList(); break; case 5:// Display all students in the list System.out.printf("%s", l); break; case 6:// Prepare to exit the program System.out.println("Program terminating"); break; default: // Invalid menu option System.out.println("Invalid menu option. Please re-enter."); break; } // end switch statement } while (menuOption != 6);// end do-while loop } // end main() } // end class StudentListTest

Coding Steps When writing your code, code one case statement at a time and comment or stub the remaining statements until you are ready to code them. If you are writing code to create and maintain a list of students, code the basic switch-case statement and stub all methods except the methods to display the purpose of the program, display the menu options and prompt the user for a menu choice. 12/12/

12/12/ switch (menuOption) { case 1:// Insert a student into the list System.out.println(“Menu Option 1 Chosen”); break; case 2:// Remove the student from the list System.out.println(“Menu Option 2 Chosen”); break; case 3:// Locate a student in the list System.out.println(“Menu Option 3 Chosen”); break; case 4:// Clear the list System.out.println(“Menu Option 4 Chosen”); break; case 5:// Display all students in the list System.out.println(“Menu Option 5 Chosen”); break; case 6:// Prepare to exit the program System.out.println("Program terminating“); break; default: // Invalid menu option System.out.println("Invalid menu option. Please re- enter.“); break; } // end switch statement } while (menuOption != 6);// end do-while loop } // end main()

12/12/ Sample output: This program will allow the user to create and insert a student into a list, retrieve a student from the list for display and delete a student from the list. The user can also clear the list and display all students in the list. Student List Menu Options 1. Enter a student into the list. 2. Delete a student from the list. 3. Locate and display a student. 4. Clear the list of all students. 5. Display all students in id. number order. 6. Exit the program. Enter the menu option: 3 Menu option 3 chosen

12/12/ Student List Menu Options 1. Enter a student into the list. 2. Delete a student from the list. 3. Locate and display a student. 4. Clear the list of all students. 5. Display all students in id. number order. 6. Exit the program. Enter the menu option: 8 Invalid menu option. Please re-enter. Student List Menu Options 1. Enter a student into the list. 2. Delete a student from the list. 3. Locate and display a student. 4. Clear the list of all students. 5. Display all students in id. number order. 6. Exit the program. Enter the menu option: -2 Invalid integer - please re-enter: 6 Program terminating

Stub Testing By commenting out or stubbing statements, you can thoroughly test each case statement and method call. When a case statement is thoroughly tested, you can proceed to coding the next case statement. 12/12/

Coding/Testing Steps For example, once the do-while loop is thoroughly tested using valid and invalid menu options, the next step might be to write the code for the first case statement. Code the method or methods to prompt the user for information about the student, then insert the student into the list. No other code would be written until the first case statement is thoroughly tested. Once case statement 1 is completely tested, the code should be saved (in case you need to back up to it at a later date) and the code for another case statement should be written and tested. 12/12/