Sunteți pe pagina 1din 5

SELF ASSESSMENT Functions, procedures, parameters

Question 1
A program was required to capture the meal orders of the Delphi students
attending the FunInTheSun workshop held recently in Utopia! Consider the
following Delphi code and the user interfaces presented.

To calculate the percentage of


discount to allow, the number
of meals and the number of
beverages are considered. If
there are more than ten items,
10% discount is allowed, if
there are five or more items
then 5%. If the number of items is less than five, then no discount is given.
1.1

Write the code for the procedure PlaceOrder. The number of meals and
the number of beverages must be passed as input parameters (integers),
and the cost of the order as well as the percentage of discount that was
given is retuned as output parameters from the procedure. (5)

procedure TfrmMyProcMCQ.PlaceOrder(nMeal, nBev : Integer;


var rCost,rDiscount : Double);
const
MealCost = 12.50;
BevCost = 5.00;
begin
if ((nMeal + nBev) > 10) then
rDiscount := 0.1
else if ((nMeal + nBev) > 5) then
rDiscount := 0.05
else
rDiscount := 0.0;
rCost := ((nMeal * MealCost) + (nBev * BevCost)) * (1rDiscount);
end;

1.2

Write
the
code
for
the
event
handler
procedure
TfrmMyProcMCQ.btnOrderClick(Sender: TObject); that includes the procedure
call : PlaceOrder(sedMeal.Value, sedBev.Value, rOrderCost, rOrderDiscount);
The results of the procedure call must be displayed in the ListBox. The results
of the previous order must first be cleared! (5)

procedure TfrmMyProcMCQ.btnOrderClick(Sender: TObject);


var
sOrder
: String;
rOrderCost, rOrderDiscount : Double;
nMealIndex, nBevIndex
: Integer;
begin
PlaceOrder(sedMeal.Value, sedBev.Value, rOrderCost, rOrderDiscount);
nMealIndex := rgpMeal.ItemIndex;
nBevIndex := rgpBev.ItemIndex;
lstOrder.Clear;
sOrder := IntToStr(sedMeal.value) + ' - ' + rgpMeal.Items[nMealIndex];
lstOrder.Items.Add(sOrder);
sOrder := IntToStr(sedBev.value) + ' - ' + rgpBev.Items[nBevIndex];
lstOrder.Items.Add(sOrder);
sOrder := 'Cost : ' + FloatToStrF(rOrderCost, ffCurrency, 15,2);
lstOrder.Items.Add(sOrder);
sOrder := 'Discount given : ' + FloatToStrF(rOrderDiscount, ffFixed, 15,2);
lstOrder.Items.Add(sOrder);
end;

1.3

Write the code for the event handler that resets the user interface. Note that
the last item in each of the RadioGroups must be selected by default. (2)

procedure TfrmMyProcMCQ.bmbResetClick(Sender: TObject);


begin
rgpMeal.ItemIndex := 5;
rgpBev.ItemIndex := 3;
sedMeal.Value := 0;
sedBev.Value := 0;
lstOrder.Clear;
end;

1.4

How would the program be modified to ensure that the student selects at least
one, but not more than 10 meals and at least one but not more than five
beverages? (3)

Set minimum to 1 and maximum to 5 or 10 of the SpinEdits during DESIGN TIME.


QUESTION 2 (Functions) (15 )
You are required to design and write a program that converts a given rand amount to
another currency. The three currency options are presented on the user interface as a
collection of RadioButtons in a GroupBox.A conversion percentage of 2% is deduced.The
program uses the event handlers shown below.
procedure TfrmCurrencyCalculator.radEuroClick(Sender: TObject);
begin
edtCurrency.Text
:=
ForeignCurrency(StrToFloat(edtRand.Text),0.1389);
lblOtherCurrency.Caption := 'Euro';
end ;
procedure TfrmCurrencyCalculator.radBritishClick(Sender: TObject);
begin
edtCurrency.Text
:=

ForeignCurrency(StrToFloat(edtRand.Text),0.0873);
lblOtherCurrency.Caption := 'British pound';
end ;
procedure TfrmCurrencyCalculator.radAusDollarClick(Sender:
TObject);
begin
edtCurrency.Text :=
ForeignCurrency(StrToFloat(edtRand.Text),0.2397);
lblOtherCurrency.Caption := 'Australian dollar';
end;
2.1

Why is the subroutine ForeignCurrency considered a function?

Because only one value, the exchange amount, is returned from the subroutine.
2.2

Write the code for the subroutine ForeignCurrency. (8)

Function ForeignCurrency(Rand,ExchangeRate:double): String;


var Currency, ConPercentage : double;
begin
ConPercentage := 0.02;
Currency := ExchangeRate * Rand;
Currency := Currency - ConPercentage/100*Currency;
Result := FloatToStrF (Currency,ffFixed,15,2);
end;
Marks:
Declarations: 1
Header: 3
Calculations: 3
Conversion: 1
2.3

What will the program do when a non-numeric value is entered into the Rand Edit
component? (1)

The program will raise an exception.


2.4

Re-write the event handler TfrmCurrencyCalculator.radEuroClick to handle nonnumeric values in the Rand Edit component by setting edtCurrencys Text
property to 0.00'. (4)

procedure TfrmCurrencyCalculator.radEuroClick(Sender: TObject);


begin
try
edtCurrency.Text:=ForeignCurrency(StrToFloat(edtRand.Text),0.1389);
except
EdtCurrency.Text := '0.00';
end;
lblOtherCurrency.Caption := 'Euro';
end;

QUESTION 3 (Functions and RadioGroup) (15 credits)


This currency conversion program is an upgrade
of the one used in Question 2 of this
assignment. The following changes are required.
The currencies that could be considered for
conversion are presented in a RadioGroup. In
this example there are seven currencies
available for conversion.
Exchange rates: The exchange rates are
stored in an array declared in the program.
These must be entered prior to the conversion
of any currency. Note that the exchange rate array must be initialized to zero in the
FormShow. The array ExchangeRates is declared in accordance with the number of
currencies considered.
The declaration of the array for the exchange rates: ExchangeRates : array [0..6] of
Double;
The Enter button prompts for the exchange rate of the currency selected in the
RadioGroup. The Show button displays all the currencies as well as the exchange rates
(as per the array values).

4.1

Write the code for the FormShow event that


initializes all the rates of exchange to zero.

procedure TfrmCurrencyConvert.FormShow(Sender: TObject);


var
nIndex : integer;
begin
for nIndex := 0 to 6 do
begin
ExchangeRates[nIndex] := 0;
end;
end;

4.2

Write the code for the btnEnterRates that saves the exchange rate of the
selected currency. Note that this event must also clear the listing of the rates,
as well as the lblConverted.

procedure TfrmCurrencyConvert.btnEnterRatesClick(Sender: TObject);


var
sNewRate : String;
nIndex : Integer;
begin
lstExchanges.Clear;
lblConverted.Caption := 'Convert?';
nIndex := rgpCurrency.ItemIndex;
sNewRate := InputBox('Exchange rate for ' +
rgpCurrency.Items[nIndex], 'Enter rate ', '0');
ExchangeRates[nIndex] := StrToFloat(sNewRate);
end;

4.3

Write the code for the btnShowRates event, that lists the currencies and the
exchange rates in the ListBox. The listbox should be cleared first though!

procedure TfrmCurrencyConvert.btnShowRatesClick(Sender: TObject);


var
nCount : Integer;
sString : String;
begin
lstExchanges.Clear;
for nCount := 0 to 6 do
begin
sString := rgpCurrency.Items[nCount] + ' - ' +
FloatToStrF(ExchangeRates[nCount], ffFixed, 15,2);
lstExchanges.Items.Add(sString);
end;
end;

4.4

Write the code for the btnConvert that converts the amount in rand in the edit
box, according to the currency selected in the RadioGroup and the exchange
rate in the array. The result of the conversion must be displayed in the label
lblConverted.

procedure TfrmCurrencyConvert.btnConvertClick(Sender: TObject);


var
sCurrency : String;
nIndex
: Integer;
begin
nIndex := rgpCurrency.ItemIndex;
try
sCurrency :=ForeignCurrency(StrToFloat(edtRand.Text),
ExchangeRates[nIndex]);
except
ShowMessage('Invalid input!');
end;
lblConverted.Caption := sCurrency + ' ' + rgpCurrency.Items[nIndex];
end;

4.5

Write the code that will execute when the user selects a different currency.
This must show a message in lblConverted label.

procedure TfrmCurrencyConvert.rgpCurrencyClick(Sender: TObject);


begin
lblConverted.Caption := 'Convert?';
end;

UNISA 2010

S-ar putea să vă placă și