1 CHAPTER 13 INTRODUCING DELPHI PROGRAMMING Theory through Practise CHAPTER 13 Contents Chapter 13 - Functions and E
Views 189 Downloads 1 File size 122KB
1
CHAPTER 13
INTRODUCING DELPHI PROGRAMMING Theory through Practise CHAPTER 13
Contents Chapter 13 - Functions and Exceptions ..................................................................................................... 2 Problem 13.1 - Safe spraying conditions ................................................................................................... 2 Problem 13.2 - Mortgage bond application ............................................................................................... 4 Problem 13.3 - Converting a procedure to a function................................................................................ 6 Problem 13.4 - Procedure/function criteria (1) .......................................................................................... 8 Problem 13.5 - Procedure/function criteria (2) .......................................................................................... 8 Problem 13.6 - Improved validation for Mr Kite..................................................................................... 10 Problem 13.7 - Convert a procedure to a function ................................................................................... 12
2
CHAPTER 13
Chapter 13 Functions and Exceptions A possible solution for each problem appears below. These solutions mostly apply to the third edition too. Differences are mentioned in the text at the appropriate points.
Problem 13.1 Safe spraying conditions In terms of properties, lblDecision’s Font.Size property needs to be increased, say in the range of 20pt to 24 pt. These methods (general functions) need to test particular conditions. For this, one can use an If statement as demonstrated in the question by the TempOK function. One can also use an assignment statement involving a Boolean expression. The solutions for the 3rd edition use the If statements while the solutions for the 4th edition use the assignment technique. 3rd edition: This illustrates using the If statements: function TempOK: Boolean; begin if frmSpraying.sedTemp.Value > 20 then Result := True else Result := False; end; {end function TempOK} function WindOK: Boolean; begin if frmSpraying.sedWind.Value 25) and (frmSpraying.sedHumid.Value < 65) then Result := True else Result := False; end; {end function HumidOK} procedure TfrmSpraying.btnEvaluateClick(Sender: TObject); begin // as given in the question end; { procedure TfrmSpraying.btnEvaluateClick } procedure TfrmSpraying.bmbResetClick(Sender: TObject);
3
CHAPTER 13
begin sedTemp.Value := 0; sedWind.Value := 0; sedHumid.Value := 0; lblDecision.Caption := ''; lblDecision.Color := clBtnFace; sedTemp.SetFocus; end; { procedure TfrmSpraying.bmbResetClick }
4th edition: This illustrates using the assignment statements: function TfrmSpraying.TempOK: Boolean; begin Result := (sedTemp.Value > 20); end; {end function TfrmSpraying.TempOK} function TfrmSpraying.WindOK: Boolean; begin Result := (sedWind.Value 25) and (sedHumid.Value < 65); end; {end function TfrmSpraying.HumidOK} procedure TfrmSpraying.btnEvaluateClick(Sender: TObject); begin // as given in the question end; { procedure TfrmSpraying.btnEvaluateClick } procedure TfrmSpraying.bmbResetClick(Sender: TObject); begin sedTemp.Value := 0; sedWind.Value := 0; sedHumid.Value := 0; lblDecision.Caption := ''; lblDecision.Color := clBtnFace; sedTemp.SetFocus; end; { procedure TfrmSpraying.bmbResetClick }
With the 4th edition, because we are writing methods attached to the form, we must also declare each method as part of the form’s type declaration: type
4
CHAPTER 13
TfrmSpraying = class(TForm) // list of components and event handlers as generated by Delphi private { Private declarations } function TempOK: Boolean; function WindOK: Boolean; function HumidOK: Boolean; public { Public declarations } end;
Enter the full version of the program and then check that it works correctly by using the test cases below. For the test cases we need to check that each of the three factors is taken into account. How would we do this? We need in turn to set two factors to valid values and then vary the third between valid and invalid values to see the effect. We should also attempt to test each factor at its borderline values. What are the borderline values? The temperature must be ‘greater than 20ΕC’, so at 20ΕC there should not be spraying while at 21ΕC spraying is OK. The humidity must be ‘between 25 and 65 percent’, so 25% and 65% are outside the limits. The wind speed must be ‘at most 15 kph’, so 15kph is OK while 16kph is not. On this basis, the minimum set of test cases is as follows, though one might do some additional ones: Test case 1 2 3 4 5 6 7 8
Temperature 30 30 30 30 30 30 20 21
Wind speed 10 10 10 10 15 16 10 10
Humidity 25 26 64 65 45 45 45 45
Outcome No OK OK No OK No No OK
Problem 13.2 Mortgage bond application The function to evaluate whether or not the applicant meets the acceptance criteria can use eithr an If statements or an assignment statement to a Boolean expression. As in the previous solution, we give both these versions here. The 3rd edition version uses the assignment statement and the 4th edition version uses the If statement. 3rd edition: This illustrates using the assignment statement in the ApplyOK function: function ApplyOK: Boolean; begin Result := (frmBond.sedAge.Value >= 18) and (frmBond.sedIncome.Value >= 24000) and (frmBond.sedIncome.Value = 18) and (sedIncome.Value >= 24000) and (sedIncome.Value