Tester & TestMaker

by James Farnsworth: Y8 Age: 12

Introduction to Tester

James required little tuition before he was able to write this pair of programs. The method of file naming and the options to quit are nice touches that are exceptional from such a young Pascal programmer. Note that after finishing a test if you press Enter instead of an acceptable exit word you will be invited again to try a test.

Each test comprises ten questions, each with one correct answer. The case-sensitivity is deliberate, to enable the testing of German vocabulary. You must run the TestMaker program first to obtain your files of questions and answers.

James wrote most of his code on his Raspberry Pi using Lazarus. Following instructions in our resource page entitled Installing and using Lazarus he added to the Run Parameters the code /usr/bin/x-terminal-emulator -T 'Tester' -e $(LazarusDir)/tools/runwait.sh $(TargetCmdLine). The following screenshot shows the completion of an easy test.

Tester running on the Pi

Tester running on the Pi

The Code of Tester

program Tester;
{
    Copyright (c) 2013 James Farnsworth

    Licensed under the Apache License, Version 2.0 (the "License"); you may not
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/ }

{$mode objfpc}{$H+}

uses
  crt, strutils, SysUtils, Math;
var  
  fa : text; // file of answers
  fq : text; // file of questions
  testchosen : string;
  c, score : integer;
  q, a, usera, e : string;

begin
  repeat
    e := 'false';
    writeln('What test do you want to load? (do not include the file extension, but do');
    writeln('include the file address if you saved the test with a specific file address)');
    readln(testchosen);
    assignfile(fq, testchosen + ' questions.txt');
    reset(fq);
    assignfile(fa, testchosen + ' answers.txt');
    reset(fa);
    for c:= 1 to 10 do
      begin
        clrScr;
        readln(fq, q);
        readln(fa, a);
        writeln('Question ', c, ': ', q);
        readln(usera);
        if (usera = a) then
          begin
            score := score + 1;
            writeln('Correct!');
          end
        else
          begin
            writeln('Incorrect');
          end;
        sleep(2000);
      end;
    clrScr;
    writeln('You scored ', score, '!');
    readln(e);
    if (e = 'exit') or (e = 'quit') or (e = 'terminate') or (e = 'end') then
      begin
        e := 'true';
      end;
  until (e = 'true');
  closeFile(fq);
  closeFile(fa);
end.


Could you make the program output the correct answer when the user gives a wrong answer?

Introduction to TestMaker

TestMaker produces a test file when you supply it with ten questions and answers. James suggests a test on German numbers with these answers:

eins = 1
zwei = 2
drei = 3
vier = 4
fünf = 5
sechs = 6
sieben = 7
acht = 8
neun = 9
zehn = 10

We think that some of his interesting suggestions for a general knowledge quiz need modification to give the user a fair chance. Note that the answers are case sensitive. Could you adapt the program so that it loses its case sensitivity?

1) How long did the Hundred Years War last?  116 years
2) Which country makes Panama hats?   Ecuador
3) From which animals do we get cat gut?  Sheep and Horses
4) In which month do Russians celebrate the October Revolution?  November
5) What is a camel's hair brush made of?  Squirrel Fur
6) The Canary Islands in the Pacific are named after what animal?  Dogs
7) What was King George VI's first name?  Albert
8) What colour is a purple finch?  Crimson
9) Where are Chinese gooseberries from?   New Zealand
10) What is the colour of the black box in a commercial aeroplane?   Orange

The Code of TestMaker

program TestMaker;
{
    Copyright (c) 2013 James Farnsworth

    Licensed under the Apache License, Version 2.0 (the "License"); you may not
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/ }

{$mode objfpc}{$H+}

uses
  sysutils, strutils;
var
  finaltext, finaltexta : textFile;
  answer, question : array [1 .. 10] of string;
  filename, filenamea : string;
  c : integer;
begin
  writeln('What do you want the test to be called? (Without file extension, but with file');
  writeln('address if you want to save it in a specific location. If no file address is');
  writeln('given the program will save the questions where the program is on your hard');
  writeln('drive, usb stick etc.)');    
  readln(filename);
  filenamea := filename + ' answers.txt';
  filename := filename + ' questions.txt';
  //no 1
  writeln('What is the first question?');
  readln(question[1]);
  writeln('What is its answer?');
  readln(answer[1]);
  //no 2
  writeln('What is the second question?');
  readln(question[2]);
  writeln('What is its answer?');
  readln(answer[2]);
  //no 3
  writeln('What is the third question?');
  readln(question[3]);
  writeln('What is its answer?');
  readln(answer[3]);
  //no 4
  writeln('What is the fourth question?');
  readln(question[4]);
  writeln('What is its answer?');
  readln(answer[4]);
  //no 5
  writeln('What is the fifth question?');
  readln(question[5]);
  writeln('What is its answer?');
  readln(answer[5]);
  //no 6
  writeln('What is the sixth question?');
  readln(question[6]);
  writeln('What is its answer?');
  readln(answer[6]);
  //no 7
  writeln('What is the seventh question?');
  readln(question[7]);
  writeln('What is its answer?');
  readln(answer[7]);
  //no 8
  writeln('What is the eighth question?');
  readln(question[8]);
  writeln('What is its answer?');
  readln(answer[8]);
  //no 9
  writeln('What is the ninth question');
  readln(question[9]);
  writeln('What is its answer?');
  readln(answer[9]);
  //no 10
  writeln('What is the tenth question?');
  readln(question[10]);
  writeln('What is its answer?');
  readln(answer[10]);

  assignfile(finaltexta, filenamea);
  assignfile(finaltext, filename);
  rewrite(finaltext);
  rewrite(finaltexta);
  for c := 1 to 10 do
    begin
      writeln(finaltext, question[c]);
      writeln(finaltexta, answer[c])
    end;
  closeFile(finaltext);
  closeFile(finaltexta);
end.

Programming - a skill for life!

Three programs (including Notes) and Raspberry Pi guidance by James Farnsworth