SAP - ABAP: I need to rework the following program to abide by the following rul
ID: 3817563 • Letter: S
Question
SAP - ABAP:
I need to rework the following program to abide by the following rules.
Rewrite the program so that the appropriate actual parameters are passed to subroutines within which the mathematical calculations take place. No write statements are allowed inside your subroutines. After being rewritten, it should look and function exactly the same as the original from the user's perspective.
*-------------- parameters -------------------------------------------*
PARAMETERS par_num1(2) TYPE p.
PARAMETERS par_num2(2) TYPE p.
PARAMETERS par_code(1) TYPE c.
*-------------- variables --------------------------------------------*
DATA calc_result(6) TYPE p decimals 2.
*-------------- constants --------------------------------------------*
CONSTANTS con_num1(12) TYPE c VALUE 'Number One: '.
CONSTANTS con_num2(12) TYPE c VALUE 'Number Two: '.
CONSTANTS con_result(8) TYPE c VALUE 'Result: '.
CONSTANTS con_add(1) TYPE c VALUE 'A'.
CONSTANTS con_sub(1) TYPE c VALUE 'S'.
CONSTANTS con_mult(1) TYPE c VALUE 'M'.
CONSTANTS con_div(1) TYPE c VALUE 'D'.
CONSTANTS con_error(17) TYPE c VALUE 'Invalid Operation'.
* ------------- main program -----------------------------------------*
* performing the calculation using a case
CASE par_code.
* perform the add
WHEN con_add.
COMPUTE calc_result = par_num1 + par_num2.
FORMAT COLOR 1 INVERSE.
WRITE: /, con_num1, par_num1.
WRITE: /, con_num2, par_num2.
WRITE: /, con_result, calc_result.
FORMAT COLOR OFF INVERSE OFF.
* perform the subtract
WHEN con_sub.
COMPUTE calc_result = par_num1 - par_num2.
FORMAT COLOR 2 INVERSE.
WRITE: /, con_num1, par_num1.
WRITE: /, con_num2, par_num2.
WRITE: /, con_result, calc_result.
FORMAT COLOR OFF INVERSE OFF.
* perform the multiply
WHEN con_mult.
COMPUTE calc_result = par_num1 * par_num2.
FORMAT COLOR 3 INVERSE.
WRITE: /, con_num1, par_num1.
WRITE: /, con_num2, par_num2.
WRITE: /, con_result, calc_result.
FORMAT COLOR OFF INVERSE OFF.
* perform the divide
WHEN con_div.
COMPUTE calc_result = par_num1 / par_num2.
FORMAT COLOR 4 INVERSE.
WRITE: /, con_num1, par_num1.
WRITE: /, con_num2, par_num2.
WRITE: /, con_result, calc_result.
FORMAT COLOR OFF INVERSE OFF.
* invalid operation code
WHEN OTHERS.
WRITE / con_error.
FORMAT COLOR 5 INVERSE.
WRITE: /, con_num1, par_num1.
WRITE: /, con_num2, par_num2.
WRITE: /, con_result, calc_result.
FORMAT COLOR OFF INVERSE OFF.
ENDCASE.
* end of program
Explanation / Answer
Hello,
As per the given program code snippet in sap abap and requirements specified .The detailed explanation of the rework is mentioned below:-
===================================================================================
1)Rewrite the program so that the appropriate actual parameters are passed to subroutines within which the mathematical calculations take place:-
Ans:-Parameters are selection-screen elements in SAP ABAP, which are used to provide inputs fields on selection-screens.
Hence if the code requirement is to pass actual parameters please find the modified code below.i.e not to pass from selection screen then.Code is modified accordingly.
2) No write statements are allowed inside your subroutines
Ans:- This program has no subroutines only case statements.Hence write statements can be included in them.
================================================================================
Rewritten code can be found below.Changes made are shown in bold.
CODE:-
*-------------- parameters -------------------------------------------*
PARAMETERS par_num1(2) TYPE p.
PARAMETERS par_num2(2) TYPE p.
PARAMETERS par_code(1) TYPE c.
*-------------- variables --------------------------------------------*
DATA calc_result(6) TYPE p decimals 2.
*-------------- constants --------------------------------------------*
CONSTANTS con_num1(12) TYPE c VALUE 'Number One: '.
CONSTANTS con_num2(12) TYPE c VALUE 'Number Two: '.
CONSTANTS con_result(8) TYPE c VALUE 'Result: '.
CONSTANTS con_add(1) TYPE c VALUE 'A'.
CONSTANTS con_sub(1) TYPE c VALUE 'S'.
CONSTANTS con_mult(1) TYPE c VALUE 'M'.
CONSTANTS con_div(1) TYPE c VALUE 'D'.
CONSTANTS con_error(17) TYPE c VALUE 'Invalid Operation'.
* ------------- main program -----------------------------------------*
* performing the calculation using a case
*assumed the parameters values as:-
*par_num1=10
*par_num2=20
*par_code='D'
CASE D.
* perform the add
WHEN con_add.
COMPUTE calc_result = 10 + 20.
FORMAT COLOR 1 INVERSE.
WRITE: /, con_num1, 10.
WRITE: /, con_num2, 20.
WRITE: /, con_result, calc_result.
FORMAT COLOR OFF INVERSE OFF.
* perform the subtract
WHEN con_sub.
COMPUTE calc_result = 10 - 20.
FORMAT COLOR 2 INVERSE.
WRITE: /, con_num1, 10.
WRITE: /, con_num2, 20.
WRITE: /, con_result, calc_result.
FORMAT COLOR OFF INVERSE OFF.
* perform the multiply
WHEN con_mult.
COMPUTE calc_result = 10 * 20.
FORMAT COLOR 3 INVERSE.
WRITE: /, con_num1, 10.
WRITE: /, con_num2, 20.
WRITE: /, con_result, calc_result.
FORMAT COLOR OFF INVERSE OFF.
* perform the divide
WHEN con_div.
COMPUTE calc_result = 10 / 20.
FORMAT COLOR 4 INVERSE.
WRITE: /, con_num1, 10.
WRITE: /, con_num2, 20.
WRITE: /, con_result, calc_result.
FORMAT COLOR OFF INVERSE OFF.
* invalid operation code
WHEN OTHERS.
WRITE / con_error.
FORMAT COLOR 5 INVERSE.
WRITE: /, con_num1, 10.
WRITE: /, con_num2, 20.
WRITE: /, con_result, calc_result.
FORMAT COLOR OFF INVERSE OFF.
ENDCASE.
* end of program
================================================================================
Please let me know if more information is required.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.