Chap-7 Robust_Input. A Package for Robust Input Read input values by calls to procedure Get overloading “Get” procedure for Integer and Float values There.

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Control Structures Ranga Rodrigo. Control Structures in Brief C++ or JavaEiffel if-elseif-elseif-else-end caseinspect for, while, do-whilefrom-until-loop-end.
1 Programmer-Defined Functions Functions allow program modularization Variables declared in function are local variables Only known inside function in.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Stored Procedure Language Stored Procedure Overview Stored Procedure is a function in a shared library accessible to the database server can also write.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Exceptions (Large parts of these copied from Ed Schonberg’s slides)
1 Chapter 7 User-Defined Methods Java Programming from Thomson Course Tech, adopted by kcluk.
Concurrency - 1 Exceptions General mechanism for handling abnormal conditions Predefined exceptions: constraint violations, I/O errors, communication errors,
More Ada Constructs 7 Oct Today’s Constructs Derived types Record types Array types Visibility and scope Subprogram arguments.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 6: Loop Control Structures.
1 Lab Session-6 CSIT-121 Spring 2005 Structured Choice The do~While Loop Lab Exercises.
Programming Languages Tucker and Noonan
© Alan Burns and Andy Wellings, 2001 Programming in the Small Aim: to remind you how to read/write programs in Ada 95, Java and ANSI C.
1 Lab Session-7 CSIT-121 Fall Introducing Structured Choice 4 The do~While Loop 4 Lab Exercises.
Advanced Databases Advanced PL/SQL Programming: Procedure, Function and Package.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
1 CS 1430: Programming in C++. 2 Input: Input ends with -1 Sentinel-Controlled Loop Input: Input begins with.
ITEC 320 Lecture 5 Scope Exceptions. Scope / Exceptions Review Functions Procedures.
VB Core II Conditional statements Exception handling Loops Arrays Debugging.
ITEC 320 Lecture 10 Packages (2). Review Packages –What parts do they have? –Syntax?
CPSC- 120 Principle of Computer Science I Computer = Hardware + Software.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Chapter 7 Other Loop forms, Procedures; Exception Handling.
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
Writing JavaScript Functions. Goals By the end of this unit, you should understand … How to breakdown applications into individual, re-usable modules.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Sha Tin Methodist College F.4 Computer Studies Pascal Programming.
Chap 7- Control Structures : The WHILE Statement.
System Structures b Package b Abstract Data Type.
Chap 5 Control Structure Boolean Expression and the IF Statement.
Chapter 6 Counting Loops; Subtypes In preceding chapters, 1.Sequence and 2.Conditional structures In this chapter, 3.Repetition, or iteration.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
11. EXCEPTION HANDLING Rocky K. C. Chang October 18, 2015 (Adapted from John Zelle’s slides)
1 CS 1430: Programming in C++. Quiz Functions Function Prototype float sqrt(float x); Function name, type, parameter and parameter type Function.
Chapter 3 The General Structure of Ada Programs. General Form of an Ada Program With package1; With package2;... With packagen; Procedure pname IS - -
Essential Ada Terminology copyright © Michael B. Feldman, All Rights Reserved.
Manipulator example #include int main (void) { double x = ; streamsize prec = cout.precision(); cout
Review Expressions and operators Iteration – while-loop – for-loop.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Exception Handling How to handle the runtime errors.
Effective Java, Chapter 9: Exceptions Items Last modified Fall 2012 Paul Ammann.
Last Updated : 27 th April 2004 Center of Excellence Data Warehousing Group Teradata RDBMS Concepts.
structured statically typed imperative wide-spectrum object-oriented high-level computer programming language extended from Pascal and other languages.
WITH Ada.Text_IO; PROCEDURE Three_Days IS | Finds yesterday and tomorrow, given today.
Functions + Overloading + Scope
© 2016, Mike Murach & Associates, Inc.
Chapter 7 User-Defined Methods.
CS 1430: Programming in C++ No time to cover HiC.
Subtype Enumeration type
ECE Application Programming
H:\>| WITH Ada.Text_IO; PROCEDURE Hello_Initials IS
Chapter 5: Loops and Files.
Iterations Programming Condition Controlled Loops (WHILE Loop)
Chapter 8 Repetition Statements
Example: Finding the Mode
copyright © Michael B. Feldman, All Rights Reserved
Effective Java, 3rd Edition Chapter 10: Exceptions
C:\> sort_3_numbers.exe |
Chapter 4: Repetition Structures: Looping
ECE 103 Engineering Programming Chapter 18 Iteration
Effective Java, Chapter 9: Exceptions
Object-Oriented Programming (OOP) Lecture No. 44
Conditional Loops Counted Loops
Learning Intention I will learn about the standard algorithm for input validation.
Stored Procedure Language
Presentation transcript:

Chap-7 Robust_Input

A Package for Robust Input Read input values by calls to procedure Get overloading “Get” procedure for Integer and Float values There are 3 parameters for Get the first one to read item the second one for Min Value range the third one for Max Value range

Specification for Robust Input PACKAGE Robust_Input IS | Package for getting numeric input robustly

PROCEDURE Get (Item : OUT Integer; MinVal : IN Integer; MaxVal : IN Integer); -- Gets an integer value in the range MinVal..MaxVal -- Pre: MinVal and MaxVal are defined -- Post: MinVal <= Item <= MaxVal PROCEDURE Get (Item : OUT Float; MinVal : IN Float; MaxVal : IN Float); -- Gets a float value in the range MinVal..MaxVal -- Pre: MinVal and MaxVal are defined -- Post: MinVal <= Item <= MaxVal END Robust_Input;

Body of Robust Input PROCEDURE Get (Item : OUT Integer; MinVal : IN Integer; MaxVal : IN Integer) IS Declare SUBTYPE in the RANGE MinVal..MaxVal; Declare local variables BEGIN -- Get END Get;

PROCEDURE Get (Item : OUT Integer; MinVal : IN Integer; MaxVal : IN Integer) IS Declare SUBTYPE in the RANGE MinVal..MaxVal; Declare local variables BEGIN -- Get LOOP BEGIN --- Exception Handler Block END -- Exception Handler Block END LOOP; END Get;

PROCEDURE Get (Item : OUT Integer; MinVal : IN Integer; MaxVal : IN Integer) IS Declare SUBTYPE in the RANGE MinVal..MaxVal; Declare local variables BEGIN -- Get LOOP BEGIN -- exception handler block Sequence of statements for reading EXIT Exception Handling END; -- exception handler block END LOOP; END Get;

PROCEDURE Get (Item : OUT Integer; MinVal : IN Integer; MaxVal : IN Integer) IS Declare SUBTYPE in the RANGE MinVal..MaxVal; Declare local variables BEGIN -- Get LOOP BEGIN -- exception handler block Sequence of statements to read EXIT; -- valid data EXCEPTION -- invalid data WHEN condition => END; -- exception handler block END LOOP; END Get;

The Body of Robust_Input WITH Ada.Text_IO; WITH Ada.Integer_Text_IO; WITH Ada.Float_Text_IO; PACKAGE BODY Robust_Input IS | Body of package for robust numeric input handling PROCEDURE Get (Item : OUT Integer; MinVal : IN Integer; MaxVal : IN Integer) IS SUBTYPE TempType IS Integer RANGE MinVal..MaxVal; TempItem : TempType; -- temporary copy of Item

BEGIN -- Get LOOP BEGIN -- exception handler block Ada.Text_IO.Put(Item => "Enter an integer between "); Ada.Integer_Text_IO.Put(Item => MinVal, Width => 0); Ada.Text_IO.Put(Item => " and "); Ada.Integer_Text_IO.Put(Item => MaxVal, Width => 0); Ada.Text_IO.Put(Item => " > "); Ada.Integer_Text_IO.Get(Item => TempItem); Item := TempItem; EXIT; -- valid data

EXCEPTION -- invalid data WHEN Constraint_Error => Ada.Text_IO.Put (Item => "Value is out of range. Please try again."); Ada.Text_IO.New_Line; Ada.Text_IO.Skip_Line; WHEN Ada.Text_IO.Data_Error => Ada.Text_IO.Put (Item => "Value is not an integer. Please try again."); Ada.Text_IO.New_Line; Ada.Text_IO.Skip_Line; END; -- exception handler block END LOOP; END Get;

PROCEDURE Get (Item : OUT Float; MinVal : IN Float; MaxVal : IN Float) IS SUBTYPE TempType IS Float RANGE MinVal..MaxVal; TempItem : TempType; -- temporary copy of Item

BEGIN -- Get LOOP BEGIN -- exception handler block Ada.Text_IO.Put (Item => "Enter a floating-point value between "); Ada.Float_Text_IO.Put (Item => MinVal, Fore=> 1, Aft => 2, Exp => 0); Ada.Text_IO.Put(Item => " and "); Ada.Float_Text_IO.Put (Item => MaxVal, Fore=> 1, Aft => 2, Exp => 0); Ada.Text_IO.Put(Item => " > "); Ada.Float_Text_IO.Get(Item => TempItem); Item := TempItem; EXIT; -- valid data

EXCEPTION -- invalid data WHEN Constraint_Error => Ada.Text_IO.Put (Item => "Value is out of range. Please try again."); Ada.Text_IO.New_Line; Ada.Text_IO.Skip_Line; WHEN Ada.Text_IO.Data_Error => Ada.Text_IO.Put (Item => "Value is not floating point. Please try again."); Ada.Text_IO.New_Line; Ada.Text_IO.Skip_Line; END; -- exception handler block END LOOP; END Get; END Robust_Input;

A Program that use Robust_Input WITH Robust_Input; PROCEDURE Test_Robust_Input IS | Demonstrates Robust_Input package SUBTYPE SmallInt IS Integer RANGE ; SUBTYPE LargerInt IS Integer RANGE ; SUBTYPE SmallFloat IS Float RANGE ; SUBTYPE LargerFloat IS Float RANGE ;

Small : SmallInt; SmallF : SmallFloat; Larger : LargerInt; LargerF : LargerFloat; BEGIN -- Test_Robust_Input Robust_Input.Get(Small,SmallInt'First,SmallInt'Last); Robust_Input.Get(Larger,LargerInt'First,LargerInt'Last); Robust_Input.Get(SmallF,SmallFloat'First,SmallFloat'Last); Robust_Input.Get(LargerF,LargerFloat'First,LargerFloat'Last); END Test_Robust_Input;