Workshop 1 – Create an Example Program Using Snippets

advertisement
Workshops – Module3 Basic COBOL Statements
Workshop 1 – Create an Example Program Using Snippets
Purpose of this workshop:
After finishing this workshop you will be able to create a COBOL program that:



Exercises all of the major COBOL datatypes
Demonstrates all of the MOVE statement format options
Displays results in the Debugger
Pre-requisites:

Product installed

Education verification completed

Sections on RDz COBOL editor and RDz Workbench
completed
@ Copyright 2009, International Business Machine - 1
Workshops – Module3 Basic COBOL Statements
Create a new Project for the samples in this Unit
Steps





Launch Rational Developer for System z
From the menu:
o Click File
o New > Other
From the New Select a Wizard dialog:
o Type: local
o Select: Local Project
 Under Workstation COBOL or PL/I
 Click: Next >
Enter the name of the Project: chapter3
Check the property group: COBOL Sample Property Group

Click Finish
This will create a new project for you in your workspace. For the remaining exercises in this section
you will add COBOL programs to this project
Create a new cobol folder for the samples in this Unit
Steps

From the z/OS Projects explorer:
o Select your new chapter3 project
o Right-click and select:
 New > Folder
o Name the folder: cobol
@ Copyright 2009, International Business Machine - 2
Workshops – Module3 Basic COBOL Statements
Debug an Example COBOL Program
Steps


Expand the chapter3 project
Right-click over the \cobol\ folder and select:
o New > File
o Name the file: VARPGM.cbl
Make sure the Snippets View is open

With your cursor in column1 of the file, Double-Click the Data Representation
and Move Statements Snippet

Fill in as follows:
o Program-ID: VARPGM
o Author: Your Name
After the code is copied in from the snippet:

Set your cursor inside the file in a blank area.

Type another blank and press Ctrl/S – to save your new program
Briefly study the code in VARPGM – note the following:

Assortment of variables with different datatypes

MOVE statements that show different formats and options for value assignment
@ Copyright 2009, International Business Machine - 3
Workshops – Module3 Basic COBOL Statements
Build your example COBOL program
Syntax Check your Program


When finished, all of the above editing, Right-Click over your COBOL code in the
editor
From the context menu, select: Save and Syntax Check
o If you have any syntax errors they will appear as small red circles in the
COBOL margin, and in the Remote Error List.
o Fix all of the syntax errors before continuing
Build your Project
From z/OS Projects:

Select your program: VARPGM.cbl

Right-click, and from the context menu select: Nominate as Entry Point

Right-click over the project (chapter3) and select: Rebuild Project
Debug your Program
From Project Explorer:

Expand the BuildOutput folder

Select your program executable: VARPGM.exe

Right-click, and from the context menu select:
o Debug As…

Debug Configurations

From the Debug Configurations window type (or Browse to) the fully-qualified
name of your executable program: VARPGM.exe

Press Debug (if you are prompted to enter the Debug Perspective, select Yes)
@ Copyright 2009, International Business Machine - 4
Workshops – Module3 Basic COBOL Statements
Discover the COBOL Data Types and Assignment Statement Semantics

Expand all of the variables

Click the Step Over (F6) icon – this will execute one instruction at a time in your
program
@ Copyright 2009, International Business Machine - 5
Workshops – Module3 Basic COBOL Statements

As you debug your program note carefully the effect each value assignment (MOVE)
statement has on the Variables

Continue to Debug and watch the assignments – and be sure to recall and associate
the COBOL data representation and MOVE syntax you studied in this section –
mapping to the run-time behavior exposed through the debugger
@ Copyright 2009, International Business Machine - 6


Workshops – Module3 Basic COBOL Statements
When you get to the MOVE CORRESPONDING statements you will have to open
and expose different variable values, in order to understand the effect of these
statements
When you have finished debugging, click the Terminate icon
and return to the z/OS Projects perspective (top right-hand corner of your Workbench)
@ Copyright 2009, International Business Machine - 7
Workshops – Module3 Basic COBOL Statements
Close VARPGM.cbl in the editor
@ Copyright 2009, International Business Machine - 8
Workshops – Module3 Basic COBOL Statements
Apply and Master Data Representation and MOVE Statements

You will create a new program that processes insurance policy information. Here are the
specifications:
o Create a new program named: policy.cbl
o Use the Snippet named: Program Template
In the WORKING-STORAGE Section

Create a Policy Record as an 01 level field

Create the following structure as 05 and 10 level variables:
o InsuredName
 LastName - character 20
 FirstName - character 20
o PolicyEffectiveDate - numeric 8
o PolicyAmount – input field, decimal numeric values, large enough to hold:
99999.99
o PolicyType – character 20
 Valid Types:
 Life
 Health
 Property/Casualty
 Auto


Create an output 01 record with edited output fields for the above. Use appropriate output
datatypes for the fields
Name the output fields the same as the input fields (except for the 01 record – which should
be named something like: Policy-out
@ Copyright 2009, International Business Machine - 9


Workshops – Module3 Basic COBOL Statements
In the Procedure Division
o In the 000-HOUSEKEEPING paragraph
 Use MOVE statements to assign values to the Policy record
o In the 200-MAINLINE paragraph
 MOVE the corresponding input fields to the output field in one
statement
Debug your program:
o Save your changes and Syntax Check
 Clean up any/all syntax errors
o From z/OS Project explorer
 Nominate policy.cbl as the entry point in the application
 Rebuild the chapter3 project
 Debug policy.exe
o From the Debug view
 Step through each statement
 Verify your logic
@ Copyright 2009, International Business Machine - 10
Workshops – Module3 Basic COBOL Statements
Workshop 2 – Create and Debug a Program with COBOL Math
Purpose of this workshop:
After finishing this workshop you will be able to create a COBOL program that:


Exercises all of the major COBOL Math statements, including:
o Add
o Subtract
o Multiply
o Divide
o Compute
Demonstrates the different arithmetic precision options:
o Rounded
Pre-requisites:

Product installed

Education verification completed

Sections on RDz COBOL editor and RDz Workbench
completed
@ Copyright 2009, International Business Machine - 11
Workshops – Module3 Basic COBOL Statements
Create and Debug an Example COBOL Program
Steps


Expand the chapter3 project
Right-click over the \cobol\ folder and select:
o New > File
o Name the file: calc.cbl
Make sure the Snippets View is open

With your cursor in column1 of the file, Double-Click the Data Representation
and Move Statements Snippet

Fill in as follows:
o programName: calc
o authorName: Your Name

In the WORKING-STORAGE SECTION – create the following records (01 levels
and all elementary dataitems), as shown below
o Calc-Fields
o temp-converter
o simple-interest
Note that the capitalization is your call…..
@ Copyright 2009, International Business Machine - 12
Workshops – Module3 Basic COBOL Statements
Create the Procedure Division

In the Procedure Division, add all of the statements as shown below – that
demonstrate COBOL arithmetic:
o
o
o
o
o
o
MOVE
ADD
SUBTRACT
MULTIPLY
DIVIDE
COMPUTE
@ Copyright 2009, International Business Machine - 13
Workshops – Module3 Basic COBOL Statements
Build your example COBOL program
Syntax Check your Program


When finished, all of the above editing, Right-Click over your COBOL code in the
editor
From the context menu, select: Save and Syntax Check
o If you have any syntax errors they will appear as small red circles in the
COBOL margin, and in the Remote Error List.
o Fix all of the syntax errors before continuing
Build your Project
From Project Explorer:

Select your program: calc.cbl

Right-click, and from the context menu select: Nominate as Entry Point

Right-click over the project (chapter1) and select: Rebuild Project
Debug your Program
From Project Explorer:

Expand the BuildOutput folder

Select your program executable: calc.exe

Right-click, and from the context menu select:
o Debug As…

Debug Configurations

From the Debug Configurations window type (or Browse to) the fully-qualified
name of your executable program: calc.exe

Press Debug (if you are prompted to enter the Debug Perspective, select Yes)

Debug through all of the code in the program – making sure that all of your code
works and is arithmetically – along with syntactically – correct

When you are finished remember to Terminate your debug session, and return to the
z/OS Projects perspective
@ Copyright 2009, International Business Machine - 14
Workshops – Module3 Basic COBOL Statements
Workshop 3 – Create and Debug a Program Conditional Logic
Purpose of this workshop:
After finishing this workshop you will be able to create a COBOL program that:

Exercises all of the major COBOL conditional statements, including:
o IF
 Complex
 Different types of IF
o EVALUATE
o Demonstrates the use of the Debugger to test complex structures
Pre-requisites:

Product installed

Education verification completed

Sections on RDz COBOL editor and RDz Workbench
completed
@ Copyright 2009, International Business Machine - 15
Workshops – Module3 Basic COBOL Statements
Create and Debug an Example COBOL Program
Steps


Expand the chapter3 project
Right-click over the \cobol\ folder and select:
o New > File
o Name the file: conditional.cbl
Make sure the Snippets View is open

With your cursor in column1 of the file, Double-Click the Data Representation
and Move Statements Snippet

Fill in as follows:
o programName: conditional
o authorName: Your Name

In the WORKING-STORAGE SECTION – create the following 77 and 88
elementary items as shown:
o ITEM-1
o ITEM-2
o ITEM-3
 VALID-ITEM
o ITEM-N
o Marital-code
o People-count
Note that the capitalization is your call…..
@ Copyright 2009, International Business Machine - 16
Workshops – Module3 Basic COBOL Statements
Create the Procedure Division

The Procedure Division is very lengthy – so code at least some of the statements that
demonstrate the types of conditional logic found in COBOL production applications
Note…. Your code is continued on the next slide
@ Copyright 2009, International Business Machine - 17
Workshops – Module3 Basic COBOL Statements
Final set of statements to enter in the PROCEDURE DIVISION
@ Copyright 2009, International Business Machine - 18
Workshops – Module3 Basic COBOL Statements
Build and Test your example COBOL program
Syntax Check your Program

When finished, all of the above editing, Right-Click over your COBOL code in the
editor

From the context menu, select: Save and Syntax Check
o If you have any syntax errors they will appear as small red circles in the
COBOL margin, and in the Remote Error List.
o Fix all of the syntax errors before continuing
Build your Project
From Project Explorer:

Select your program: conditional.cbl

Right-click, and from the context menu select: Nominate as Entry Point

Right-click over the project (chapter1) and select: Rebuild Project
Debug your Program
From Project Explorer:

Expand the BuildOutput folder

Select your program executable: conditional.exe

Right-click, and from the context menu select:
o Debug As…

Debug Configurations

From the Debug Configurations window type (or Browse to) the fully-qualified
name of your executable program: conditional.exe

Press Debug (if you are prompted to enter the Debug Perspective, select Yes)

Debug through all of the code in the program – making sure that all of your code
works and is conditionally – along with syntactically – correct

Most importantly – make sure you can follow all of the IF/ELSE patterns in the code
– and be able to explain why the code execution took one path versus another
@ Copyright 2009, International Business Machine - 19
Workshops – Module3 Basic COBOL Statements
Create and Debug a Sequential File Processing COBOL Program
Steps


Expand the chapter3 project
Right-click over the \cobol\ folder and select:
o New > File
o Name the file: seqfileproc1.cbl
Make sure the Snippets View is open

With your cursor in column1 of the file, Double-Click the Sequential File
Processing – Example 1 Snippet

Fill in as follows:
o programName: seqfileproc1
o authorName: Your Name

Start by studying this code – as you did on the course slides,
o First getting the big picture of the file-processing paragraphs tied
together by performs
o Understand the semantic concepts of what’s in the paragraphs (get an
“outline view”)
o Then study the line-by-line code for understanding
o
o
o
SELECT/ASSIGN – and their associated FD
The purpose of the WORKING-STORAE records, counters and
accumulators
Each of the paragraphs in the PROCEDURE DIVISION – what’s
coded in them, and why it’s coded the way it is
@ Copyright 2009, International Business Machine - 20
Workshops – Module3 Basic COBOL Statements
Create the input file
Steps:
Select the following lines, and copy them to your Windows buffer
11111Smith
Joanne
22222Martin
Ricky
33333Jones
James
44444McAllisterBillieJoe
55555Luthor
Lex
66666Barnum
P.T.
77777Clampett E.M.
88888Watson
Thomas
99999Hamilton Alex
00000Welch
John
88888Bad Pat. Type
99999Bad Ins. Type
2016521111I000112/12/20050001123010HMO012012345678
2124441212O000210/01/19520012345623GOV006001231421
2025551212I000311/23/20040000873547HMO020071232321
8762341234I023409/23/20080234323483GOV011045321321
7645234324I098412/31/20080434356746000000000123786
7854354354O078321/09/20060000432439HMO025070176321
5464432432I001308/12/20050002342138GOV030004536179
2127643483O002207/25/20050012112329000010003546321
9098883213I000206/30/20060002323483HMO020000047574
2036520980O000105/31/20070001123245GOV040097812143
X
GOV
O
XXX
From the Start Menu, save these lines in a Notepad file, named:
c:\datain.dat
Note: - not a text file, you will have to save as (*) All files
After you’ve saved, your file should look like this – from My Computer:
@ Copyright 2009, International Business Machine - 21
Workshops – Module3 Basic COBOL Statements
Build and test the program
Syntax Check your Program

When finished, all of the above editing, Right-Click over your COBOL code in the
editor

From the context menu, select: Save and Syntax Check
o If you have any syntax errors they will appear as small red circles in the
COBOL margin, and in the Remote Error List.
o Fix all of the syntax errors before continuing
Build your Project
From Project Explorer:

Select your program: seqfileproc1.cbl

Right-click, and from the context menu select: Nominate as Entry Point

Right-click over the project (chapter1) and select: Rebuild Project
Debug your Program
From Project Explorer:

Expand the BuildOutput folder

Select your program executable: seqfileproc1.exe

Right-click, and from the context menu select:
o Debug As…

Debug Configurations

From the Debug Configurations window type (or Browse to) the fully-qualified
name of your executable program: seqfileproc1.exe

Press Debug (if you are prompted to enter the Debug Perspective, select Yes)

Debug through all of the code in the program. Verify your desk-checking view of
the program logic.


Note that you may wish to Run to break-points, as there is
some amount of repetition in this (and all Batch
processing) programs 
When you are finished remember to Terminate your debug session, and return to the
z/OS Projects perspective
@ Copyright 2009, International Business Machine - 22
Workshops – Module3 Basic COBOL Statements
Enhance the Program
It is a common requirement to modify, enhance, maintain and support production COBOL programs
(more common in fact than creating new programs from scratch). So we might as well learn how this
works – and some best practices and keys to success.
Do the following – after you’ve made each code modification:

When finished, all of the above editing, Right-Click over your COBOL code in the
editor

From the context menu, select: Save and Syntax Check

Debug to verify your work
Enhancements to the seqfileproc1.cbl:
1. Make the READ routine a performed paragraph.

Note that the COBOL statements that read the input file is repeated
in three different places in the program.
o Create a paragraph that contains the statements
o Perform that paragraph from the three original spots
o Test your changes
2. Edit for bad numeric data

Production programs do not assume valid input values (especially
in online applications, but even in batch processing mode)
o Create a new error-flag in Working-Storage
o Create a paragraph that edits each of the numeric fields
in
WS-INPUT-REC for valid numeric values
o If you find an error set your new error-flag on

Typically this would be something along the
lines of
MOVE “Y” to ERRORFLAG-WS
o At the beginning of 100-MAINLINE – perform your new
paragraph
o If you find an error in the data, follow the kind of
processing logic in the IF VALID-TYPE statement – to add
to the error count, re-read the input file, and skip past
the rest of the 100-MAINLINE statements
@ Copyright 2009, International Business Machine - 23
Workshops – Module3 Basic COBOL Statements
Note that you will have to modify some of the test data values in: c:\datain.dat
– to create bad numeric values in order to test your logic
@ Copyright 2009, International Business Machine - 24
Workshops – Module3 Basic COBOL Statements
3. Test for empty input file

Copy c:\datain.dat – to some other file for backup

Delete all records in c:\datain.dat

Run your program, and ensure that it behaves correctly. If it does
not (hint/hint – when it does not) add logic to the program to
handle an empty file

When you’re satisfied with your work, copy the files back to their
original state (not-empty)
4. Average the final totals

Add an inline perform in the 200-CLEANUP paragraph that
calculates the average Total Patient Net amount for all
good (non-error) input records read

Modify WS-TOTALS-REC – and add the new value to your
final output record
5. Add a new file for error output records

Many production programs write error records to a
separate output file. In our case, we’ll simply write the
original input record to an output file.

Add a new:
o Select/Assign for an error file
o FD for the file (same file characteristics as
INFILE)
o In the PROCEDURE DIVISION:
 Modify the file OPEN and CLOSE
routines for the new file
 Add a paragraph that writes the
output file
 Add code so that when your edit
routines spot an error in the input file
record, after adding to the errorcounter it performs the paragraph to
write the input file with errors to the
output file
@ Copyright 2009, International Business Machine - 25
Download