1.4 Principles of programming Flashcards
What are meta languages and why are they preferable to using natural English
meta language - a language which describes the syntax of another language (e.g. BNF, syntax diagram)
They are preferable to natural languages as they are unambiguous (sentence has one meaning) which is good for the computer as it needs to know precisely what it is being asked to do when running the program
define syntax
rules of a particular programming language
do bnf questions
do syntax diagram questions
why do programming languages need to be unambiguous and compare to natural language
programming languages need to be unambiguous because:
-computers have to know exactly what it is being asked to do
this is unlike natural languages which are ambiguous
what is a programming paradigm and describe the various paradigms
programming paradigm - a fundamental structure and approach of a programming language
Types:
Procedural languages - (e.g. C++) written as an ordered set of instructions in a sequence that the computer follows strictly i.e. actions/calculations/selection/iteration/procedure calls in order
Non-procedural languages - (e.g. PROLOG) allows programmers to specify the results they want without having to solve the problem. The program is written as a list of facts and rules. Used for making AI like expert systems.
Event-Driven programming - (e.g. sensors monitoring heat) more often part of a visual programming language, rather than one main subroutine that runs and calls other subroutines, subroutines are written to react to events that occur e.g. button press
Visual Programming - (e.g. visual basic) allows rapid, easy development of professional-looking applications which reduces the need for typical programming
Mark-up languages - (e.g. HTML) not really a programming paradigm, but are a key part of web programming. They add commands to a text document to add further meaning to the text or to structure it (e.g. titles)
Object-oriented programming - ability to combine data and the procedures that use the data into one object, makes the program easier to maintain and understand as the object complexity can be hidden from the program.
NOT MUTUALLY EXCLUSIVE - CAN BE MULTIPLE (e.g.visual basic event driven, visual and object oriented)
Define inheritance
easy to re-use objects in other programs and if necessary create new objects that inherit from other objects without having to recode.
Inheritance enables new objects to take on the properties of existing objects
It is when a new class is created that is based on another class
It can take on the methods and attributes of the parent class
what are the features of a low level language
e.g. assembly language which is usually specific to the type of processor on which the program needs to run
programmers have to write the software in a ‘computer-centric’ way, interacting directly with registers, memory locations, etc.
one low level instruction translates into one machine code instruction, therefore will run faster. They are also programs smaller in size.
Slower and more difficult to program
Uses more cryptic and harder to understand references to memory locations and sections of code (i.e. contains no self-documenting identifiers)
time to convert to machine code (using an assembler) is negligible so can run immediately
what are the features of a high level language
e.g. Java, Python, VB which is available on many different computer architectures.
a compiler translates the source code into machine code
Programmer works in a more ‘human-centric’ way, not requiring direct access to the internal registers of the processor. Closer to spoken english semantics
One line of code translates into many lines of machine code, compiler will generally not translate into as few machine code instructions as a hand-crafted low level program
Easier to program and programs that translate into hundreds of machine code instructions can be rapidly created.
has self-documenting identifiers
requires compilation before code can be run for the first time
easier to create modules that can be re-used and accessed by other parts of the program
allow the use of powerful commands that can perform quite complex tasks
what is standardisation, why is it useful to standardise computer languages and explain the difficulties of doing to
Most languages started out standardised e.g. basic -> then different companies added extra functions and commands to produce various different variants -> e.g. Quick Basic, BBC basic, Visual Basic, etc.
Difficult to agree standards as it takes time, languages need to develop and evolve new features as computers become more powerful and new tech appears
Programming in a popular language is a good idea-> compilers available for many different systems -> companies fan more easily create different versions of their software for different operating systems
Makes it easier for companies to employ programmers that are already skilled in a particular language (programmers are in demand)
define class
is used in object-oriented programming
A template defining methods and attributes used to make objects
define object
The definition of an object is known as a class
is used in object-oriented programming
Are created for things that the program needs to manage e.g. student as an object
Contains:
-data fields (attributes) about the object, such as StockID, Description, etc.
-subroutines/procedures that can be performed on the object e.g. Addstock, DisplayQuantitySold, etc.
define attributes
is used in object-oriented programming
Data fields about the object e.g. stockID, Description, etc.
define methods
Subroutines/procedures that can be performed on the object
E.g. addstock, displayquantitysold
what are the advantages of the object-oriented paradigm
Classes can be reused in future programs and objects can be created that inherit attributes and methods from another object but also gains its own methods such as Add_Exam_Scores
E.g.
Class student inherits from person Method Input_Exam_Results (exam, grade)
End method
..more methods
End class
Private attributes cannot be inherited still however protected can be
Ability to combine data and procedures that use the data together in one object is more intuitive
Encapsulation makes the program easier to maintain and understand (complexity is hidden from program/people that use it)
Abstraction is similar to encapsulation but it means that we allow access only where necessary to the encapsulated data via public or protected methods/attributes
Inheritance: easy to reuse objects in other programs and if necessary create new objects that inherit from other objects without having to recode
E.g. gears in a car are encapsulated (hidden), Abstraction is provided via a gearstick to allow the driver to change gears
In visual languages, new custom controls can be created, inheriting all the methods and attributes of a normal listbox
what does an object consist of?
-Data fields
-Subroutines/Procedure
What are the disadvantages of object oriented programming?
Define Public, Private, protected attributes and methods and explain the difference between them
Public attributes can be accessed anywhere as its public and inherited
Protected attributes can be accessed through inheritance but I’d private in every other case
Private attributes can be accessed through methods in subclass
define instantiation
Creating a new object
define abstraction
define encapsulation
All attributes and methods related to one thing kept together in a single object
(Effectively another form of modularisation and black-box programming)
Look at UML class diagrams
explain the nature of procedural and non-procedural programming paradigms and their application
explain the advantages of using a procedural language to write a program based on an algorithm
explain the features of the mark-up language programming paradigm
explain the features of event-driven language
explain the features of visual programming languages
How do you create an object variable of value b based on the class person
What can it then be used to do
Define p as new person
Can now use the public methods of the object
E.g. p.InputDetails(“Smith”,”Janet”,”12/5/1990”)
And public attributes
E.g. p.surname
But cannot access any private or protected attributes
What is different about protected and private attributes compared to public
Protected and private attributes and methods are accessible only within the class.
However, protected attributes and methods can be used by derived classes that inherit from the class
Explain instantiation
A class (which acts like a template)
Instantiate, so declares a variable object based on the class template
So e.g. a variable p1 has all the public properties within the class (variables, methods)
Can do stuff like p1.surname = “smith” and the property surname for that object is changed to smith
Another one is p1.AddToFile(“students.txt”) is a public method which will he called and students.txt will be sent to it
What is a superclass
It is a term given to the parent class
What is a subclass
The derived class
What is UML
Unified modelling language
Often used in class diagrams to describe classes and inheritence:
Example:
________________________
| Vehicle |
|——————————‐|
|-Manufacturer :String |
|#Price :Real |
|+colour : string |
|——————————‐|
| +GetManufacturer() |
|+GetPrice |
|——————————‐|
Top box:name of class
Middle box:list of attributes
Bottom box: list of methods
- denotes a private attribute or method
+ denotes a public attribute or method
# denotes a protected attribute or method
What is a reason that methods in subclasses are required
There could be a private attribute that couldn’t be inherited from the superclass so a method is developed to allow the private attribute to be given a value
What is a reason that methods in subclasses are required
There could be a private attribute in attribute so cannot be set directly
What is a reason that methods in subclasses are required
There could be a private attribute in the subclass so cannot be set directly
Requires a publicly available method
What does inheritance enable
Classes that are similar to existing classes can be created by indicating differences (rather than starting again) and thereby allowing code to be organised and reused effectively
What does inheritance define
It defines relationships between classes and organises classes into groups
What is polymorphism
Allows a derived class to provide its own version of a method from the superclass
I.e. subclasses can override the inherited superclass method with their own
What are the differences between natural language and computer languages
Natural languages are ambiguous I.e. there can be more than 2 meanings to a sentence
Computer languages are designed to be unambiguous I.e. 1 meaning and the computer needs to know precisely what is being asked to do when running
The rules of how a particular language is structured is known as the syntax of the language
Because a computer language is unambiguous the syntax rules of the language can be defined using a meta language
What is the purpose of BNF
To describe unambiguously the syntax of the computer language