Teach learners how to read the paper
Question 3.1 (2.1 Supplementary 2018 and before) should all be done in the Class unit.
- The first question is usually a constructor or another method.
- If it is another method, this method is usually then needed in the constructor to set one of the fields (attributes).
- If the question is indicating that the method is returning a single value it will be a Function.
- If the method needs to return more than one value (which has not been asked to date) it will be a Procedure with reference parameters.
- Methods that do not return a value but make changes to the fields / do calculations will be Procedures.
- If the question says receive values - that means the method has parameters.
- If the method receive a value (singular) it has only one parameter and receives (plural) means more than one parameter.
- If it does not indicate that it is receiving values, do not add any parameters as this will make calling the method difficult.
- Do not add more or less parameters than the question asks.
- Name your parameters starting with a p.
- The parameters often have to make changes to the fields. If this is the case, as in the Constructor and Mutators, the parameters must be on the right and the fields on the left. Easy to remember because f is before p in the alphabet. For example: fName := pName;
- The question paper usually provides you with a list to indicate data types of your fields under 3.1 before 3.1.1 starts. For example: fAge - the age of the learner. This means that fAge is an integer.
- Learners can also view the data type of the fields by looking at the given code under Private of the class unit.
- When you receive parameters ensure they match the data types of the fields that they need to set. If you receive a value that has to set the fMark field, which was declared as real, the parameter, pMark, of the method must also be real.
- In the call statements remember to match the arguments with the number of parameters, data type and order in which the method needs to receive the values.
- If you have to create your own class enter Type under interface, but before implementation. Push <Ctrl><j>, then choose ClassC and delete what you won't need like protected, published and the default constructor and destructor.
- The constructor will either be asked with or without parameters.
- Constructors are always public.
- They will only be called by the main form.
- If there are no parameters the question paper will indicate what default values the fields should be set to. Strings are often set to empty strings, boolean to either true or false and number variables (real or integer) will be set to a given number. For example fName := ''; iAge := 13; rMark := 0;
- In a scenario like above there will be a mutator method setting the values of the fields similar to a parameterised constructor.
- Example of a signature: constructor Create;
- Example of call statement in the main form: Learner := TLearner.Create;
- If the Constructor has parameters you need to read carefully to see which values it will receive and set. This type of constructor might not set all the fields of the class. It might need to call the first method (if there was one) to set some fields and it could also set some of the fields to a default value using hard code like in the Supplementary of 2018. Some fields might not get a value at all.
- Learners need to match the data type of the parameters to the fields that they will be making changes to.
- It is important that the fields in the constructor appear on the left and the parameters on the right of the :=. For example: fName := pName;
- If you are having trouble adding the signature of the constructor you may use Creates which helps if there is a default constructor create in the program already.
- Recently I have had trouble adding my own Constructor and Wessel has also found a way fixing this problem. He recommended to change the file extension of the file ctor.xml to ctor.xml.bak in the folder C:\Program Files (x86)\Embarcadero\RAD Studio\7.0\ObjRepos\en\Code_Templates\Delphi and it worked.
- Example of a signature: constructor Create (pName,pDOB:string; pMark:Real);
- Example of call statement in the main form: Learner := TLearner.Create(sName,sDOB,rMark);
- The paper will often only indicate that the learner needs to write a method and not ask specifically for a function or a procedure. It is here where it is important to read the question carefully. If the question indicates that the method returns an answer, has a result, is an accessor method or a ToString, it is a Function. If it is not returning an answer it is a Procedure. For Example: If the method returns a true if the value received is valid and a false if not, it is a function. If the value received will be assigned to an attribute of the class it is a Procedure as it is not returning an answer.
- If the method is a function the returning answer indicates the data type of the function. For example: If the method returns a true if the value received is valid and a false if not the data type of the function will be boolean.
- Mutators are used to change the fields of the class.
- They are always public.
- They are always procedures.
- Their name will start with Set followed by the field it is changing. For example: SetMark
- They will receive one or more parameter - read the question to see receive or receives as explained above.
- The parameter that they receive needs to be of the same data type as the field it is setting.
- This method will probably only be called by the main form.
- The fields need to be on the left of the := and the parameter on the right. For example: fMark := pMark;
- Example of a signature: Procedure SetMark(pMark: real);
- Example of call statement in the main form: Learner.SetMark(rMark);
- Accessor methods are used to give the main form access to the fields:
- They are always public.
- They are always Functions.
- Their name will start with Get followed by what field is returned. For example: GetMark.
- Their data type will match the data type of the field it is returning.
- They will have no parameters.
- They will return only one field since a function can only return one result.
- The wording in the paper will be "Create a method that RETURNS a X". X will be the name of the field. the paper could also include the name of the method GetNum which will indicate that this is an accessor method.
- This method will mostly be called by the main form and sometimes by the ToString.
- In the question paper they could ask you in one question to write multiple accessor methods. For example: Write accessor methods to return the mark, name and age field. This will mean that you have to write three Get Functions.
- Example of a signature: Function GetMark : real;
- Example inside the function: result := fMark;
- Example of a call statement in the main form: if Learner.GetMark >= 80 then OR rMark:= Learner.GetMark;
- Auxiliary methods perform a task without changing the fields of the class:
- They could be public or private.
- These methods can be can be called by the main form or the class.
- They could be functions or procedures
- They could have parameters or not.
- Example of a signature: Function ValidMark:boolean;
- Example of a call statement in the main form: if Learner.ValidMark then
- Example of a call statement in the class: if ValidMark <> true then fMark := 0;
- A ToString will return the fields of the class as a single string.
- It will always be public.
- They will always be called by the main form.
- It will always be a function.
- The function will always have a string data type.
- It will have no parameters.
- There is often a need to call other functions in the class to display some values.
- The paper will show you the format of the string it needs to return. For example: Name: <Name>.
- The value inside the < > indicates the field that it must return i.e. result := 'Name: '+fName; and you do not need to add the < >.
- It is important to add all the labels since there are usually one mark allocated for all the labels which learners will lose if they leave one out.
- It also helps to go to question 3.2 and see where this ToString has been called to see a visual of the output of the string. This will help to see where there was maybe an empty line that was not clearly described the format of the ToString.
- You can use a #10 or #13 to create an enter and #10#10 or #13#13 to create an enter and an empty line. Make your learners aware of both since the given code could contain either or.
- I would suggest using the building of the string with the result, to help learners see their logical mistakes in the formatting.
- It also helps to not make the string to long to print. If the ToString was coded in one long line it often does not print from Delphi and only makes four squares where your code was entered. The best is to install, the free software, Notepad++ for the learners to print from during the exam. Printing from MS Word is also not recommended as it makes marking really difficult by printing a lot more pages due to the larger font and some learners do not copy out the whole file.
- Example of a signature: Function ToString:string;
- Example of a call statement in the main form: redOutput.Lines.Add(Learner.ToString);