DELPHI: Example Class question
 

Create a program using object oriented programming that displays and makes calculations with test marks. The data is saved in a file called Marks.txt. Follow the steps below to complete this program.

1) Create an object class (another unit) named clsTMarks.
2) Define a class named TMarks in this unit. This class must contain the following private fields:
    -  Surname : String;
    -  Test1 : Integer;
    -  Test2 : Integer;
    -  Test3 : Integer;
3) Also create the following public methods:
    -  constructor Create; overload;
    -  constructor Create(pSurname : String; pTest1, pTest2, pTest3 : Integer); overload;
    -  function getAverage : String;
    -  function showMarks : String;
4) Create a parameterised constructor named create that will pass values for the fields in the class. These parameters should be used to initialise the fields of the class.
5) Write a method getAverage that takes the three test marks and calculates the average of the three (keep answer integer) and display them all in the RichEdit on the main form.
6) Also write a method showMarks that only shows the surname and three test marks of every student.
7) Create an array named arrMarks that keeps objects of TMarks. Write code in the OnActivate Eventhandler of the form to read information from the text file Marks.txt. Every field is separated with a hash (#).
8) Set up the tabs so that five different columns of information can be displayed neatly (see below)
9) Button1 (Show marks) calls the method showMarks method and displays the surnames and only the three marks of every student.
10) Button2 (Calculate Average) calls the method getAverage and it calculates the average and then displays the surnames, three marks and averages of every student.

 


OWN CREATED UNIT (clsTMarks.pas)

unit clsTMarks;

interface

uses Sysutils;          //Added because IntToStr is used in this unit

type
  TMarks = class           //Defining class TMarks
    private
      Surname : String;
      Test1 : Integer;
      Test2 : Integer;
      Test3 : Integer;
    public
      constructor Create; overload;
      constructor Create(pSurname : String; pTest1, pTest2, pTest3 : Integer); overload;
      function getAverage : String;
      function showMarks : String;
  end;

var
  iAverage : Integer;        //Variable used in this unit

implementation

constructor TMarks.Create;
begin
  Surname := '';
  Test1 := 0;
  Test2 := 0;
  Test3 := 0;
end;

constructor TMarks.Create(pSurname : String; pTest1, pTest2, pTest3 : Integer);
begin
  Surname := pSurname;     //Puts values into this class
  Test1 := pTest1;
  Test2 := pTest2;
  Test3 := pTest3;
end;

function TMarks.getAverage: String;
begin
  iAverage := (Test1 + Test2 + Test3) DIV 3;   //Calculates average
  getAverage := Surname + #9 + IntToStr(Test1) + #9 + IntToStr(Test2) + #9 +
  IntToStr(Test3) + #9 + IntToStr(iAverage);
end;

function TMarks.showMarks: String;
begin
  showMarks := Surname + #9 + IntToStr(Test1) + #9 + IntToStr(Test2) + #9 +
  IntToStr(Test3);                      //Display marks note the tabs (#9)
end;

end.


MAIN UNIT (unit1.pas)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Buttons;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    Button1: TButton;
    Button2: TButton;
    BitBtn1: TBitBtn;
    procedure FormActivate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
end;

var
  Form1: TForm1;

implementation

uses clsTMarks;        //Indicating that own created unit is used

var
  arrMarks : array[1..42] of TMarks;   //Array that keeps objects of class TMarks
  iCount : Integer;                    //Counter keeping track of number of entries

{$R *.dfm}

procedure TForm1.FormActivate(Sender: TObject);
var
  fFile : TextFile;
  sTemp, sSurname : String;
  iC, iTest1, iTest2, iTest3, iH : Integer;
begin
  Richedit1.Paragraph.TabCount := 5;   //Adds tabs for neat layout in RichEdit
  Richedit1.Paragraph.Tab[1] := 100;
  Richedit1.Paragraph.Tab[2] := 200;
  Richedit1.Paragraph.Tab[3] := 300;
  Richedit1.Paragraph.Tab[4] := 400;
  Richedit1.Paragraph.Tab[5] := 500;
  iCount := 0;                        //Initial value for counter
  AssignFile(fFile, 'Marks.txt');     //Assign text file
  Reset(fFile);                       //Open text file
  While NOT eof(fFile) do             //Reads text file from beginning to end
  begin
    inc(iCount);
    Readln(fFile,sTemp);
    iH := pos('#',sTemp);            //This and next 12 lines separate fields
    sSurname := copy(sTemp, 1, iH-1);
    Delete(sTemp,1,iH);
    iH := pos('#',sTemp);
    iTest1 := StrToInt(copy(sTemp, 1, iH-1));
    Delete(sTemp,1,iH);
    iH := pos('#',sTemp);
    iTest2 := StrToInt(copy(sTemp, 1, iH-1));
    Delete(sTemp,1,iH);
    iH := pos('#',sTemp);
    iTest3 := StrToInt(copy(sTemp, 1, iH-1));
    Delete(sTemp,1,iH);
    arrMarks[iCount] := TMarks.Create(sSurname,iTest1,iTest2,iTest3); //Into array
  end;
  CloseFile(fFile);                 //Remember to close file!
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  iK : Integer;
begin
  Richedit1.Clear;                 //Clear RichEdit and add heading (next line)
  RichEdit1.Lines.Add('SURNAME' + #9 + 'TEST 1' + #9 + 'TEST 2' + #9 + 'TEST 3');
  For iK := 1 to iCount do
  begin
    RichEdit1.Lines.Add(arrMarks[iK].showMarks);   //Call method showMarks
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  iK : Integer;
begin
  Richedit1.Clear;                 //Clear RichEdit and add heading (next line)
  RichEdit1.Lines.Add('SURNAME' + #9 + 'TEST 1' + #9 + 'TEST 2' + #9 + 'TEST 3' +
  #9 + 'AVERAGE');
  For iK := 1 to iCount do
  begin
    RichEdit1.Lines.Add(arrMarks[iK].getAverage); //Call method getAverage
  end;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  Application.Terminate;                         //Close application
end;

end.

Download source files
Class.zip

 

Return to Delphi resources index




Return to Home Page

JAK Olivier (2008)

Website: http://www.jako.nom.za/
E-mail: olivier@teachitza.com