Records – Using Classes

A Record allows us to define a structure that can contain one or more variables (attributes) with each one potentially having a different data type.

As we are using Python, we will create a record by defining a class. The basic structure of a class can be seen below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class MySubjects(object):
 
    # this function creates the object and initialises any attributes
    def __init__(self, subjectName, noOfPeriods, teacherName):
        self.subjectName = subjectName
        self.numberOfPeriods = noOfPeriods
        self.teacherName = teacherName
 
    # when called this function (method) will return the value of subject name  
    def getSubjectName(self):
        return self.subjectName
 
    # when called this function (method) will set the attribute sunjectName to the value of the parameter passed
    def setSubjectName(self, subjectName):
       self.subjectName = subjectName

To create an object of type ‘MySubjects’ we do the following code:

1
subjects = MySubjects(subjectName, noOfPeriods, teacherName)

Task 1: Copy the program above and create three objects of ‘MySubjects’ and display all of the subject names by calling the appropriate method. Create get and set methods for the other attributes.

Task 2: Change the above program so that it can store a list of five subjects. You will get the user to enter five subjects and store them in a list (array).

Task 3: Make two more useful improvements to the program.

Task 1 – Becoming familiar and a bit more!

In order to complete these series of mini-tasks you will first need to complete sections 1, 2, 3, 4 and 5.

Read the following links too:
Quiz games and if statements.

1: Play withing Strings.
– The user of your program should enter their first name, surname and date of birth.
– The program should then display the length of each piece of data and convert it to uppercase and lowercase.

2: You must create a speed, distance and time calculator.

Help on creating a calculator can be found here.

– The user must first choose whether they want to calculate S, D or T.
– The user should then be prompted to enter in the relevant information for the chosen calculation.
– The program should then calculate the answer and display an appropriate message. You must use the technique from ‘Strings and Console Ouput – sub-section 15’.
– Your program should be timestamped (dd/mm/yyyy : hh-mm-ss).

3. Complete the above problem in Scratch!!!

3: A Mathematical Calculator (CHALLENGE)
 – You must create a calculator that can perform the basic operations, powers and roots (square) and percentages.

Linking your account

At code academy you are given the option to link your account to google, facebook and yahoo. During your National 5 course, it is preferred that you do not link to any social networking account. After the course has been completed and you have sat exam then by all means link your account.