Вы находитесь на странице: 1из 7

Ngobeni PN INF1511 4131-999-0 Assignment 02 Semester 02 ------------------------------------------------------------------------------------------------------Question 01 1 2

3 procedure TForm1.btnSummarizeClick(Sender: TObject); var x : Integer; begin lblVenue.Caption := 'The match was placed at ' + rgpStadium.Items.Strings[rgpStadium.ItemIndex]; x := StrToInt(sedScore1.Text) + StrToInt(sedScore2.Text); case x of 0: form1.Color := clBlue; 1,3: form1.Color := clYellow; 2: form1.Color := clGreen; 4,5,6,7,8,9,10: form1.Color := clRed; else begin form1.Color := clPurple; end; end; end;

Ngobeni PN INF1511 4131-999-0 Assignment 02 Semester 02 ------------------------------------------------------------------------------------------------------4 procedure TForm1.bmbResetClick(Sender: TObject); begin form1.Color := clScrollBar; edtTeamName1.Text := ''; edtTeamName2.Text := ''; lblResults.Caption := 'No Results' ; lblVenue.Caption := 'No Results'; sedScore1.Text := '0'; sedScore2.Text := '0'; rgpStadium.ItemIndex := 0; end; 5 If is a conditional statement which perform different computation or action depending on whether a programmer-specified Boolean condition evaluates true or false If X > 100 then begin Y := 20; If X > 200 then begin Y := 40 GetTotal(Y); end; end;

A case statement is a type of selection control statement. Its purpose is to allow the value of the variable or expression to control the flow of the program execution. case Amount of 0 : Fine := 0; 10: Fine := 20; 15: Fine := 50; else begin Fine := GoToCourt; JailTime := GetSentence; end; end;

Ngobeni PN INF1511 4131-999-0 Assignment 02 Semester 02 ------------------------------------------------------------------------------------------------------6 case statements are a more structured way than multiple if statements to express conditional logic and are easier to read 7 if statement can be used when dealing more than one types or variables when case only handles integral quantities. Question 02 1. For loop is used when the number of iteration is known before entering the loop. And the loop executes for the given values of the loop variable. 2. While loop is suitable when a programmer wants to allow code to execute repeatedly based on a given Boolean condition 3. Yes it can be converted, but according to the following example For Loop: For 0 to 5 do doSomething(); While Loop: Var X : Integer Begin X := 0; While X < 5 do doSomething(); from the above a for loop simplifies the while loop; 4. The requirements of a for loop is to have an initial value and an end value 5. Looping is required when more data is to be captured, or perform certain actions until a condition is met. 6. An infinite while loop is when code executes nonstop repeatedly without any terminating condition, or condition is not met.

Ngobeni PN INF1511 4131-999-0 Assignment 02 Semester 02 ------------------------------------------------------------------------------------------------------7. procedure TformBuyTickets.btnAddNamesClick(Sender: TObject); begin if lstStadiums.Items.Count < 5 then lstStadiums.Items.Add(inputBox('FIFA Stadiums', 'Please enter the name of the stadium','')); if lstStadiums.Items.Count > 0 then begin btnBuyFOR.Enabled := true; btnBuyWHILE.Enabled := true; end; end;

8. procedure TformBuyTickets.btnBuyFORClick(Sender: TObject); var i,rand,total : Integer; begin lstCost.Items.Clear(); for i :=0 to (lstStadiums.Items.Count - 1) do begin rand := Random(400); lstCost.Items.Add(lstStadiums.Items.Strings[i] + ' Stadium ticket costs R ' + IntToStr(rand)); total := total + rand; end; lstCost.Items.Add('TOTAL COST R ' + IntToStr(total)); end;

Ngobeni PN INF1511 4131-999-0 Assignment 02 Semester 02 ------------------------------------------------------------------------------------------------------Question 03 1. This application received input of soccer teams competing on a tournament. The teams which win on each group the program will automatically determine which team will compete for the 1st position and the losing teams compete for the 3rd position. After the tournament it displays the most number of scored goals and minimum scored goals, including the total number of scored goals. 2.

3.

Ngobeni PN INF1511 4131-999-0 Assignment 02 Semester 02 ------------------------------------------------------------------------------------------------------4. procedure TForm1.btnNextRoundClick(Sender: TObject); begin {Group A Scores} if Round1grpATeam1Score.Value > Round1grpATeam2Score.Value then begin Round2grpATeam1.Caption := Round1grpATeam1.Text; Round2grpBTeam1.Caption := Round1grpATeam2.Text; end; if Round1grpATeam1Score.Value < Round1grpATeam2Score.Value then begin Round2grpATeam1.Caption := Round1grpATeam2.Text; Round2grpBTeam1.Caption := Round1grpATeam1.Text; end; {Group B Scores} if Round1grpBTeam1Score.Value > Round1grpBTeam2Score.Value then begin Round2grpATeam2.Caption := Round1grpBTeam1.Text; Round2grpBTeam2.Caption := Round1grpBTeam2.Text; end; if Round1grpBTeam1Score.Value < Round1grpBTeam2Score.Value then begin Round2grpATeam2.Caption := Round1grpBTeam2.Text; Round2grpBTeam2.Caption := Round1grpBTeam1.Text; end; lstScores.Items.Add(IntToStr(Round1grpATeam1Score.Value)); lstScores.Items.Add(IntToStr(Round1grpATeam2Score.Value)); lstScores.Items.Add(IntToStr(Round1grpBTeam1Score.Value)); lstScores.Items.Add(IntToStr(Round1grpBTeam2Score.Value)); end;

5. procedure TForm1.btnStatsClick(Sender: TObject); var i, Max, Min, TotalScore : Integer; begin Max := StrToInt(lstScores.Items.Strings[0]);

Ngobeni PN INF1511 4131-999-0 Assignment 02 Semester 02 -------------------------------------------------------------------------------------------------------

{Min := StrToInt(lstScores.Items.Strings[0]);} TotalScore := 0; for i := 0 to (lstScores.Items.Count - 1) do begin if( StrToInt(lstScores.Items.Strings[i]) > Max) then Max := StrToInt(lstScores.Items.Strings[i]); if( StrToInt(lstScores.Items.Strings[i]) < Min) then Min := StrToInt(lstScores.Items.Strings[i]); TotalScore := TotalScore + StrToInt(lstScores.Items.Strings[i]); end; lstStats.Items.Add('The Most Scored Goals is ' + IntToStr(Max)); lstStats.Items.Add('The Minimum Scored Goals is ' + IntToStr(Min)); lstStats.Items.Add('Total Goals Scored ' + IntToStr(TotalScore)); end;

Вам также может понравиться