practical grade 12 Flashcards
programming fundamentals; OOP; 2D arrays; DB & SQL : from the DBE textbook
Variable to store dates
DateTime
Prefix for DateTime variable
t
Methods for assigning date to DateTime variable
Convert from string
Convert from integer
Convert to DateTime from string
StrToDateTime(‘dd/mm/yyyy hh:mm:ss’);
Convert to DateTime from integer
EncodeDateTime(yyyy, mo, d, h, m, s);
Functions returning DateTime values
Yesterday - 00:00:00 of yesterday
Date - 00:00:00 of today
Tomorrow - 00:00:00 of tomorrow
Now - current date and time
How to do calculations with DateTime variable
Add/subtract number (or fractions of) DAYS to it
Functions performed on DateTime values
Type keywords (day, year, month, FormatSettings.Long) into ctrl+spacebar ; will usually return integer value
How to format DateTime
FormatDateTime(), for sFormat use letters : YMDH NSZ with - between
How to declare an array
Array[first..last] of type;
Steps to working with TextFiles
- Declare logical file in variable
- Assign physical file to logical file
- Set logical file to correct state
- Use file
- Close file
Declare file variable
fFile : TextFile
Assign file
AssignFile(fFile, ‘’)
States to set file to ***** include diff
Reset
Rewrite
Append
Close file
CloseFile(fFile)
How to read from textfile
Reset
ReadLn(fFile, sOutput) - moves to next line
How to write to textfile
Rewrite/append
WriteLn(fFile, sInput) - moves to next line
Write(fFile, sInput) - doesn’t move to next line
How to loop through textfile
Eof(fFile)
Input and output of methods
Parameters and return values
Difference between function and procedure
Both accept parameters but only functions return a value
Declare procedure
procedure ProcedureName(parameterName : type);
ctrl+shift+C
What do you use parameters as
Variable in method
How to use procedure
Type in name and input parameters
Declare function
function FunctionName(parameterName: type) : type;
Ctrl+shift+C
How to use function
Assign value to Result variable
within method
Type in name and input parameters and use result like assign it to variable or smth
Scope of methods and where to declare them
Declare after implementation BEFORE automatically created procedures
Use global variables : frmForm.component to reference them
Dynamic user interface
Components created using code in runtime
Types of dynamic components
Visual
Interactive
How to create non-interactive visual dynamic component
- declare empty var of component’s class (vVariable : TType) - in global var when in doubt
- create component (component.create(parent - can say self)) assigned to said variable
- use var (NOT COMPONENT) to assign values to prop of component - .PARENT FIRST (can be self)
how to insert image to be displayed
bitmap.createFromFile - string
what is an interactive component
has events during run time like onClick events
how to use interactive component
- create as would visual component
- declare procedure in public : add (Sender: TObject) - EXACTLY, dont change TObject to component
- assign procedure name to event property of component
how to write data to a database (through code)
tables component methods:
- append : adds record at end and selects it (NOT ADDS VALUE TO, DO AFTER - think of it as setting text file up to be used)
- insert : adds record after currently selected and selects it
NB!
- POST!!! saves changes after data changed in db
how to add data to created record
table[‘fieldName’] := value; (puts into selected field)
post OBVI
how to read data from table
- select first record (tbl.first)
- while loop to scroll through records till tbl.eof (EVEN THOUGH NOT A FILE HA)
- read record - tbl[‘fieldName’]
- go to next record (tbl.next)
WITH ALL THINGS OF USING TABLES REMEMBER!!!!!!!
put DBModule name before tblName OR IT WONT WORK
you could even be a cool kid and use WITH dbmName do like a SUPER COOL KID
how to edit existing data in db
- find + select record wanting to edit
- set tbl to edit mode (tbl.Edit)
- set new values as shown before
- POST!!
how to find record in DB
tbl.Locate(‘fieldName’, searchValue, []) - selects that record and do what you will with it after
generally with setting modes of tbl…
do right before doing it - like put tbl.edit right before editing ykyk - idk if this is actually a thing but i remember it once
what is a class
data type describing attributes and behaviour of object to be created from it (BLUEPRINT)
object
INSTANCE of the class - like a model formed from the blueprint
attributes
data fields of class (PROPERTIES)
behaviour
code providing interaction with attributes - INTERACTIONS with object made up of attributes, how can use attributes
encapsulation
grouping of attributes and behaviour into one entity (object of type class)
what is OOP used for?
- independent isolated code
- reusable code
- improved integrity
how to declare a class
- create unit file for class code
type
ClassName = class(optional baseClass)
private
declare attributes + private methods
public
declare public methods
access specifier
where accessible
- private: inside class
- protected: inside class / classes based on class
- public: any program where class used
- published: public but available in object inspector
remember for working with classes!
type class code in class unit!
types of class methods
getter (accessor)
setter (mutator)
constructor
ToString
other auxiliary (helper) methods
type double
real with more digits for fraction
class diagram
identity (name)
attributes (state)
name : type;
methods (behaviour)
declarations of methods
in total forms encapsulation
include private/public with -/+
after declared methods, what do?
ctrl+shift+C to make implementation of method
constructor method
INSTANTIATES (makes an instance of) object
constructor keyword
types of constructors:
- default: sets attributes to default values
> create; overload; —– only do this if smth not working bc it just confuses things imo
- manual: sets attributes to values
> create(dataName : type); overload; (overloads default constructor) - declare default one first and put after with overload at end
constructor method implementation
Self.attribute := inputVarFromParameter
get method implementation
function
result := attributeName
can transform to more useful output - like toString or smth
set method implementation
self.attribute := InputVarParameterValue
can include input validation or change form of input
auxiliary methods
just remember if function - use result keyword at end
ToString method implementation
function
construct output string of representation of state of object
assign to result
application of class
class declared as dataType in class unit
on main unit
1. add class unit to uses
2. create variable of dataType of the class (instantiate object) AS GLOBAL VARIABLE under implementation
3. call methods of class THROUGH OBJECT VARIABLE - always use constructor create first to make object
what is a 2D array
array with 2 indices, each holding an array (row and column)
what do you use when working with a 2D array?
nested loop
why use a 2D array
data centralised
data relationalised (totally a word)
what do indices represent
first (i) : row
second (j) : column
syntax for 2D array
arrArray[index1, index2]
how to declare a 2D array
arrArray : array[1..i, 1..j] of type;
limitations of 2D array
same type
set size of array - set way too big to initialise when in doubt
how to view 2D arrays when reading from or writing to them
treat as grid - not assigning 2 values and not assigning to 2 arrays - assigning ONE value to the jth spot of the ith array ykyk
how to get length of array in 2D array
length of 1st array (i): Length(arrArray)
length of 2nd array (j): Length(arrArray[i])
length of 3rd array: Length(arrArray[i,i]);
what to do when want to work through row of 2D array
keep row index constant and loop through j
what to do when want to work with column of data
make second index constant and loop through first
first part of SQL statement
SELECT fieldName, fieldName FROM tableName
select all
*
only select unique values in field
SELECT DISTINCT fieldName FROM tblName
how to sort data in SQL statement
SELECT fieldName FROM tblName ORDER BY fieldName orderType
types : ASC and DESC
if multiple: field1 ASC, field2 DESC
dont need to say ASC if ASC
how to select conditional data in SQL statement
SELECT * FROM tbl WHERE (logic statement)
SQL statement syntax
string in ‘’
what used in where clause when needing it to be more general
wildcards and the like operator
wildcards
represent any characters
_ : one character (not none)
%: zero/many characters
how use wildcards
put in string
ALWAYS USE LIKE NOT =
how to combine conditions in WHERE clause
boolean operators - place in brackets!!
boolean operators
AND
OR } between statements
NOT - place after where
what else can be used in WHERE clause?
special SQL operators
special SQL operators
BETWEEN
IN
IS NULL
IS NOT NULL
BETWEEN
WHERE fieldName BETWEEN min AND MAX
selects all between, INCLUDING min and max values
IN
selects values that are in list
WHERE fieldName IN (value1, value2, ..)
REMEMBER IN BRACKETS
IS NULL
selects records with no value
WHERE fieldName IS NULL
IS NOT NULL
selects records with values
WHERE fieldName IS NOT NULL
how to use a date in a condition
surround with ## NOT ‘’
not have to be in specific format - follow american ways though (eye roll)
how to use data of dates HA
date functions:
YEAR(date) - int
MONTH(‘’) - int
DAY(‘’) - int
DATE(‘’) - returns todays date
what do you do if you wanna change value of data before displaying it
calculated field
how is a calculated field declared
same way as normal field
SELECT calcField FROM tblName
but rename it for aesthetics
SELECT calcField FROM tblName AS calcFieldName
what is important to remember with AS
cannot reference AS name in later clauses - restate calcField!
what to use for calculations in calculated fields
mathematical operators
functions
> number
> date
> string
number functions to format calculated fields in SQL
INT
ROUND
STR
FORMAT
INT
INT(field) - discards decimals
ROUND
ROUND(field, decimals) - self-explanatory
STR
STR(field) - returns DateTime or number as string
FORMAT
FORMAT(field, formatString) - returns DT or number as string in format
how to do formatString for FORMAT command in SQL statement
similar to delphi
‘0.00’
‘0.##’
‘Currency’
‘0%’
keep in mind with date functions (previously covered) when working with calculated fields
can’t use function keywords as name of calcField - e.g. AS Year
string functions for SQL
LEN
LEFT
RIGHT
MID
LEN
LEN(string) - num chars
LEFT
LEFT(string, numChars) - returns indicated no of chars from start of string
RIGHT
opposite of LEFT
MID
MID(string, firstChar, chars) - returns no of chars from given point in string
how to combine strings
+
when formatting date
use abbreviations as placeholders
ddmmyy hhnnss
what to use when want to answer question about overall dataset
aggregate functions
aggregate functions
SUM
AVG
MIN
MAX
COUNT
how to use aggregate functions
self explanatory functions
NAME(fieldName)
returns single value
how to aggregate functions relate to other fields
can’t use alongside as only returns single value
what can you use to make aggregate functions look prettier
AS
how to get aggregate from multiple fields for comparison
GROUP BY
GROUP BY syntax
SELECT groupFieldName, aggFunc(field) AS aggfuncName FROM tbl GROUP BY groupFieldName
NB - include field in select and at end!!
how to make a conditional statement on GROUP BY results
HAVING after GROUP BY
condition
stompi for SQL statements
SDfFtOWGH
how to make changes to DB
INSERT INTO
UPDATE
DELETE
INSERT INTO
adds records
INSERT INTO tblName (fieldNames) VALUES (values) - separate with values and in correct order/data type
dont need to include field names if inserting all - ALWAYS ADD TO PK
insert multiple records into
INSERT INTO tbl (fieldNames) VALUES (values), (values), etc…
if no value for field in record
,,
DELETE
deletes records
DELETE FROM tbl WHERE
UPDATE
changes value
UPDATE tblName SET fieldName = value, fieldName = value …
WHERE - NB
ASCII space
32
ASCII 0
48
ASCII A
65
ASCII a
97
querying two tables
SELECT tbl1.fieldName, tbl2.fieldName FROM tbl1, tbl2 WHERE tbl1.FK = tbl2.PK
link through where clause with keys
how to simplify two table queries
tbl AS M or smth and then M.