Delphi Dictionary
Delphi Dictionary
PART 4 – VALIDATION
Text Only Text / Spaces / Length Digits Only
If bValid = TRUE then… If (Length(sWord) = 13) AND (bValid) then … If bValid = TRUE then…
PART 5 – TEXT FILES
Checking if the file exists Reading from the Text File Creating a new text file
AssignFile(tFile, ‘new.txt’);
If FileExists(‘data.txt) then… AssignFile(tFile, ‘data.txt’); Rewrite(tFile);
Reset(tFile); WriteLn(tFile, sLine);
Checking if the file does not exist While not eof(tFile) do CloseFile(tFile);
Adding to an existing text file
Begin
If not FileExists(‘data.txt’) then… ReadLn(tFile, sLine); AssignFile(tFile, ‘myfile.txt’);
Append(tFile);
WriteLn(tFile, sLine);
End; CloseFile(tFile);
CloseFile(tFile);
DELPHI DICTIONARY
PART 6 – ONE DIMENSIONAL ARRAYS
Declaring an array Populating Array with random values Displaying values vertically
Displaying values horizontally Populating Array with user Input Calculating Sum / Average
rSum := 0;
For i := 1 to 5 do For i := 1 to 5 do For i := 1 to 5 do
Begin Begin Begin
edtOut.SelText := IntToStr(arrNum[i]) + #9; arrNum[i] := StrToInt(InputBox(‘Cram’, ‘Enter number’, ‘’)); rSum := rSum + arrNum[i];
End; End; End;
rAve := rSum / Length(arrNum);
Finding Highest Bubble Sort Search
* If there’s parallel arrays * If there’s parallel arrays * If there’s parallel arrays
sSub := copy(sLine, startPos, NoOfChars); Delete(sLine, startPos, NoOfChars); INSERT(sSub, sLine, Position);
EXAMPLE: EXAMPLE:
EXAMPLE:
sLine := ‘magnetic’; sLine := ‘Gears of War 3’;
sLine := ‘Hello’;
sSub := copy(sLine, 4, 3); // “net” Insert(‘drobe’, sLine, 13);
Delete(sLine, 5, 1); // “Hell”
// Gears of Wardrobe 3
DELPHI DICTIONARY
PART 7 – TWO DIMENSIONAL ARRAYS
Declaring a 2D array: Assigning Random Values Assigning values on declaration:
(Example range: 1 – 100)
arrName : Array[1..Rows, 1..Cols] of type arrNum : Array[1..5, 1..4] of Integer = ((10, 12, 5,
For r := 1 to 5 do 25), (59, 29, 92, 11), (11, 22, 44, 66), (10, 9, 8,
EXAMPLE: 7), (88, 59, 83, 82));
Begin
arrNum : Array[1..5, 1..4] of Integer; For c := 1 to 4 do
Begin
When referencing 2D arrays, always
reference the Row (r) first and then the arrNum[r][c] := RandomRange(1, 101);
Column (c). End;
End;
Displaying in StringGrid Displaying in Grid format in a Rich Edit Box: Calculating the sum/ave of Rows:
iHigh := arrNum[1][1]; Declare a new array with number of rows and *Declare arrColSums based on number of rows:
number of columns swopped: EXAMPLE:
arrColAve : Array[1..4] of Real;
for r := 1 to 5 do
arrNew : Array[1..4, 1..5] of Integer;
begin For c := 1 to 4 do
for c := 1 to 4 do for r := 1 to 4 do Begin
begin begin iSum := 0;
if arrNum[r][c] > iHigh then for c := 1 to 5 do For r := 1 to 5 do
begin begin Begin
iHigh := arrNum[r][c]; arrNew[r][c] := arrNum[c][r]; iSum := iSum + arrNum[r][c];
rPos := r; end; End;
cPos := c; end;
arrColAve[c] := iSum;
end; NOTE: Transposing means the rows become // Divide by number of rows if working out
end; columns and the columns become rows. average – in this case 4
end; End;
Right Diagonal Sum / Block Adjust (Entire Array) Block Adjust (1 Row)
SQUARE ARRAYS ONLY EXAMPLE ADDS 1 to all elements
r := 3; // Increase all values in Row 3 by 10
iSum := 0; for r := 1 to 5 do
begin for c := 1 to 4 do
c := 5;
for c := 1 to 4 do begin
For r := 1 to 5 do begin arrNum[r][c] := arrNum[r][c] + 10;
Begin arrNum[r][c] := arrNum[r][c] + 1; end;
iSum := iSum + arrNum[r][c]; end;
dec(c); End;
End;
PART 8 – OOP
CONSTRUCTOR ACCESSOR (GETTER) MUTATOR (SETTER)
*Declare under Public Always a function with a return type the same as Always a procedure with a parameter of the
the attribute it’s associated with. same type as the attribute it is associated
with.
EXAMPLE:
EXAMPLE:
Attributes: fName, fAge, fID Write an accessor for fAge. EXAMPLE:
Write a mutator for fAge.
Write a parameterised constructor to UNDER PUBLIC:
initialise fName and fID: UNDER PUBLIC:
Function getAge : Integer;
Constructor CREATE(sName : String; Procedure setAge(iAge : Integer);
sID : String); PRESS CTRL + SHIFT + C
PRESS CTRL + SHIFT + C
Under IMPLEMENTATION
Press CTRL + SHIFT + C
UNDER IMPLEMENTATION
Function TClass.getAge : Integer;
Under IMPLEMENTATION Begin Procedure TClass.setAge(iAge : Integer);
Result := fAge; Begin
Constructor TClass.CREATE(sName : End; FAge := iAge;
String; sID : String); End;
Begin
fName := sName;
fID ;= sID;
end;
toString Instantiation Calling a procedure:
Always a function Calls the constructor, creating the object in RAM objLearner.setAge(17);
Returns the attributes in a user-
The object should be declared like a variable: Calling a function:
friendly manner.
objLearner : TLearner;
iAge := objLearner.getAge;
Function TClass.toString : String; objLearner := TLearner.Create(sName, sID);
Begin redOut.Text := objLearner.toString;
Result := ‘Name: ‘ + #9 + fName + #13 +
‘ID:’ + #9 + fID;
End;
PART 9 – CALCULATIONS BASED ON %
Increase rAmount by 15% Decrease rAmount by 15% Get 15% of rAmount