OCR Worked Example – Design

//OCR Worked Example – Design
OCR Worked Example – Design 2016-10-25T17:28:33+00:00

Exempla Controlled Assessment

The OCR GCSE Computing course requires students to produce a solution to three tasks as part of the final A453 Controlled Assessment Brief.  Part 1 of the sample assessment is scratch based and outside the scope of this site, part 2 is a simple password verification system and could easily be tackled as a text or GUI type task.  This chapter looks at the Programming requirement for the third task – a High Score Database system.

This first page outlines what the exam board expect from you as a candidate and provides an analysis and outline solution for the problem – so read on…

Your task

Analysis

The task requires that a file is created and managed from within the program.  Its length needs to be 10 lines as it only needs to record the high scores of ten people.

What do we need to store in the file?
We need to store the names of the players and their high score (up to ten players).  Small Basic provides only simple file handling so we need to decide how we are going to achieve the required structure: name & high score.  We need to be able to read the data back and find a specific entry based on name or the high score.  This is because in a high score table, we need to be able to insert an entry based on score.  We also need to be able to update a score based on a players’ name.  Finally, for this task, we need to be able to delete an entry.

There is no explicit requirement to display the high score table, but in order to verify that actions have been successful, it will be useful to be able to do so, so we need to include this option in our solution.  There is also no need to sort the scores into numeric or alphabetic order so we will not try to do this initially. It might be a useful to do this as an additional development if there is time at the end.

Solutions

Leading on from the initial analysis, the best way of providing access to the high score table is probably going to be via a menu system.  This needs to have the have the following options:

  • Create File – this option should be carried out automatically when the program starts.
  • Add data – so that a new player (and their score), can be added to the table.
  • Search by name – to provide a method of finding a players current high score.
  • Search by score – to search for the name of the person with a specific score.
  • Update high score – to change the high score of a specific player.
  • Delete player – to delete an entry from the file.
  • Exit – to exit from the system.

Using these as the basic structure of our solution, we can now plan and develop solutions for each menu item.  However, before we do this we need to decide on the structure of the file.

The File Structure

Small Basic creates text based files – there is no explicit capability to store columns of values. To overcome this there are a number of options available:

My first option is to store each value on a new line so that for our ‘name’ and ‘score’ value sets, two lines would be used.
Advantage: I can easily see (using Notepad), each pair of values on two adjacent lines in the file.
Disadvantage: I would need to remember that each player would take two lines and I would need to read lines as pairs.

A second option would be to place all the names sequentially and then follow this with the scores for each player.
Advantage: I can read the first 10 lines – players’ names, and then use an offset value of 10 to read that players score.
Disadvantage: Initially there will not be 10 payers in the file so I would need to be careful to make sure we read the correct score for each player.

Another solution would be to use two files – one for the names and one for the high scores
Advantage:  Each file would have a maximum of ten lines and each line in each file would be the data for a specific player / score.
Disadvantage: I would be dealing with two files and if one became corrupted, the whole system would be incorrect or unusable.

Finally I  could construct each line to have a CSV structure using a comma or other special character to separate the values.
Advantage:  Each line contains all the data needed for an individual record so there could be no confusion.
Disadvantage: I will need to make my program manage all aspects of the structure myself as there is no CSV structure in Small Basic.

Decision

For this task I will use the CSV format because although it is slightly more difficult in Small Basic, it is more compact and could even allow the data to be read into a spreadsheet to analyse if required.  To enable this I will call the file HighScore.csv.

The way that Small Basic handles files allows specific lines of data to be written to the file.  This means that as long as the required file line is known (or found out) then a new line can be written directly on top of the old data.  This is a simple form of direct access which will make working with the file easier.   I can also use Insert() or append(), which would allow me to insert a new record at a specific line and automatically move following records down.

The File

Required file processes

When a new score is achieved, the first task will be to find out if it should be entered into the high score file.  It should only be entered if there are less than 10 records OR the score is bigger than the SMALLEST score already recorded.

Therefore I need to be able to check how many records are in the file and if there are less than 10 I can just add this record to the end.  If there are 10 records then I need to be able to search the file for the smallest score in the file and replace it.

If I find a record that needs to be replaced then I need to replace the complete line – the name and score.  Small Basic supports the feature File.WriteLine( File, LineNumber, Data ) which replaces a line at a specific point (indicated by LineNumber), in the file.

This will require the program to loop through each line of the file, looking at the first value – name to see if the name is in the file.  If it is found then the program needs to display it; otherwise it should display a suitable ‘error’ message.

Small Basic can only read complete lines from a file and so each read will return both the name and the score separated by a comma as a string.  I will be able to use string handling methods ( Text.IsSubTex() ) to compare the entered name with the line of data returned from the file. If a match is found I can display both parts suitable formatted.

This process will require an indefinite loop that will exit end at any point up to line 10. This loop will also need to have a counter to point to each line in the file until it is found or the end of the file reached.

This is very similar to searching by name except that it is the second value that needs to be checked each time through the loop.  The same Text.IsSubText() method can be used.
If a players’ name is found in the file then the high score value needs to exchanged for the new one and the line written back to the file in the same place.

The required line can be read from the file, modified as required and written back as this is a standard feature of Small Basic; it allows direct access to any line in a file without having to recreate the whole structure.  This process will make use of the Search features of option 1 (this implies that we must make this a subroutine), and then of simply replacing the score part of the line read from the file.

Deleting a player will be a little more involved as there is no feature within Small Basic to remove a line of data from a file.  In order to achieve this, lines following the line to be deleted will need to be moved up the file and a new blank line written at the end.  This, however, leaves the file the same length as before and a blank line at the end.

As an alternative, a new temporary file can be created without the required data and then renamed using features provided by the LitDev extension to Small Basic.