H SDD Programming Languages Notes

advertisement
Programming Languages Notes
Software Design & Development:
Languages & environments, Low level operations and
computer architecture
Contents

High Level Languages & Low Level
Languages
 Questions

Declarative Languages
 Questions

Object Orientated Languages
 Questions

Translation – Emulation,Virtual Machines
& Mobile Devices
 Questions
High Level Languages
A High level programming language uses a grammar and
syntax that is closer to natural languages like English than to
the machine code understood by computers
HLLs abstract the architecture of the computer from the
programmer. The programmer does not need to think of their
program in terms of memory locations, busses and processor
registers
HLLs are easier to code in as a result, but they need to be
translated to something the processor can understand.
Additionally the automated translation process can sometimes
result in inefficient code
LiveCode uses very English-like
grammar and syntax
Purposes
High level programming languages are designed with
specific purposes in mind. Even general purpose
HLLs are designed with specific fields of use:
 Java – an object orientated general purpose language designed
for portability via use of virtual machines
 Ada – a strongly typed general purpose language designed to
be hard to crash, used for critical systems
 LiveCode – a natural language general purpose language,
easier for beginners and non programmers to learn
 PHP – primarily designed for manipulating data to generate
webpages dynamically
 Prolog – a declarative language associated with artificial
intelligence and expert system creation
Low Level Languages
A low level programming language uses
instructions that correspond to the processors
instruction set and architecture
LLLs do not abstract the details of memory locations,
registers and similar from the programmer. This makes
them harder to program in. However, a low level
language program can be optimised directly by the
programmer to run faster
Machine code and assembly language are examples of
low level languages
An example of assembly language, with
registers being addressed directly
In practice, the difference between high level
languages and low level languages is a continuous
spectrum rather than one or the other
LiveCode, other
natural language
languages
Higher Level
Java – virtual
machine, type
polymorphism
and garbage
collection
C family – ability
to allocate
memory directly
Machine code –
1s and 0s
Lower Level
Assembly languages
– processor
instructions,
register addressing
Imperative & Procedural
Programming Languages
In imperative programming languages, the program code is a
series of instructions that change the state of the program
Most programming languages you have encountered so far are
imperative, or have been used imperatively
Procedural programming languages are languages that break
up programs into subprograms
Procedural programming languages offer the advantages of
code reuse – the same subprogram can be called multiple times
– and ease of reading, design and implementation because they
are modular.
Questions – High Level Languages
and Low Level Languages
Answer questions in full sentences!
1. What is a high level programming language?
2. Give one advantage and one disadvantage of high level
programming languages
3. What is a low level programming language
4. Give one advantage and one disadvantage of low level
programming languages
5. Explain why the C family of languages is considered lower
level than LiveCode, but higher level than machine code
6. What is meant by a procedural programming language
7. Give two benefits of using a procedural programming
language
8. Find out about 3 other high level programming languages
not listed on the purposes slide. Write a sentence or two
about each describing its purposes and characteristics
Declarative Programming Languages
Declarative programming languages state rules and
definitions to be followed. They are different from
imperative languages in that they do not state how to
compute things
There are a number of different types of declarative
languages, including constraint programming,
functional programming and logical programming.
Logical programming is the type most associated with
declarative programming languages. Prolog is a
notable example
SWISH Prolog is a free online Prolog
environment that you can use
Logical Programming - Rules
Programs written in a logical programming language are a series of rules
and facts about a certain subject.
For example, the simplest form of rules in Prolog:
woman(alice).
This rule states that Alice is a woman. We might have other similar rules for
other women in the program too. Rules can also state relationships:
mother_of(alice, bob).
Which states that Alice is the mother of Bob. We could have similar rules for
other mother-child relationships. However, Prolog doesn’t understand the
inverse of the relationship in the humans do. We would have to make another
rule:
child_of(X, Y) :- mother_of(Y, X).
Logical Programming - Variables
Variables in logical programming behave differently from those in imperative
programming
A logical programming variable is not used to store information. It can have
potentially any value. The programmed rules constrain the potential values.
Variables are used to query the rules. For example, with the following rules:
woman(alice).
woman(belle).
woman(carol).
We can make the query:
?- woman(X).
X has three values: alice, belle & carol.
Questions – Declarative Languages
Answer questions in full sentences!
1. What is meant by the term declarative language?
2. List the types of programming that can be considered
declarative
3. In logical programming, what are rules?
4. Explain how logical programming variables are
different from imperative programming variables
5. Using SWISH Prolog (http://swish.swi-prolog.org/ )
find a tutorial online and learn how to use some
simple Prolog
Object Orientated Languages
Object orientated programming builds on the idea
of record data types and procedural programming.
Object orientated programs use data structures
called objects or classes. Classes contain
variables called attributes and procedures called
methods.
Classes define the way instances of their type
behave and object orientated programs are
about the interactions between instances of various
classes
Inheritance
In object orientated languages, a class can inherit from another
class. When a class inherits, it automatically includes all the
attributes and methods of its parent class.
Vehicle
Car
Truck
Additional attributes can be added to the child class. For
example, the Vehicle class might include noOfWheels as an
attribute, and both the Truck and Car class also include that, but
the Truck class might also need to include cargoCapacity as an
attribute
A method inherited from a parent class can be
overridden by programming a method with the same
name in the child class.
Vehicle
noOfWheels
speed
Truck
cargoCapacity
checkIfOverSpeedLimit
getNoOfWheels
checkIfOverSpeedLimit
In this example, if the getNoOfWheels() method is called
by Truck instance, it will use the method defined in the
Vehicle class. If the checkIfOverSpeedLimit() method is
called though, it will ignore the one defined in the
vehicle class and use the version defined in the Truck
class.
Type Polymorphism
Type polymorphism refers to the fact that a variable of a given class
can store an instance of that class, or any of its child classes
Vehicle
Car
Truck
In this example, an array of Vehicles could potentially store vehicles,
cars and trucks.
What makes type polymorphism particularly useful is that any method
calls will use overridden methods of the appropriate child class.
Something can be stored as a Vehicle, but still behaves like a Truck!
Questions – Object Orientated
Languages
Answer questions in full sentences!
1.
What is meant by the an object orientated language?
2.
Explain how object orientated methods differ from subprograms
in a procedural language
3.
In what ways is an object orientated class similar to a record?
4.
Explain what is meant by inheritance when talking about an object
orientated language. What can classes inherit from their parents?
5.
What is meant by overriding when talking about inheritance?
6.
Explain why type polymorphism is an advantage for object
orientated languages
7.
Find a tutorial online and learn how to use some Java – pay
particular attention to classes
or
Ask your teacher to demonstrate how Greenfoot uses
inheritance and type polymorphism
Translation
Recall that:
 Processors operate using machine code – 1s and 0s
 High Level Languages use English-like syntax
 Programs have to be translated to machine code in order to run
on a computer, either by interpreters or compilers
In fact, we do not need to translate a high level language
program all the way to the native machine code of a
computer.
It is possible to translate programs to an intermediate
state that would require further translation or
additional software to run, and there are a number of
advantages for doing so
Emulation
An emulator is software that is capable of running the
machine code of a processor on an entirely different processor
Emulators run entirely in software and often include the
emulation of other related hardware such as sound and graphic
cards. This potentially makes them relatively slow to run
programs.
This allows programs that could not otherwise be used to be
run on a computer. Emulation is particularly useful for running
older software on modern computers, especially where the
software may no longer be compatible.
DOSBox emulates IBM compatible PCs
running the DOS operating system
Virtual Machines
Virtual machines specify a hardware architecture and
bytecode instruction set. However, virtual machines aren’t
usually implemented in hardware. Instead they either run as an
emulator or translate to native machine code.
Because they translate to native machine code, and because the
bytecode is much closer to the machine code, virtual machines
can run faster than emulators.
Virtual machines are very useful for making software portable.
Once a virtual machine has been implemented for a specific
computer, any number of programs can be compiled to
bytecode.
1980s game developers Infocom wrote their games in a virtual
machine to make them easier to port to different platforms
Mobile Devices - Architecture
Smartphones broadly make use of similar architecture to other
computers. However, smartphone processors are usually RISC
devices
RISC processors make use of a simplified instruction set as
compared to desktop PCs. RISC machine codes usually take
less processor cycles to execute
This means that they are smaller, use less power and generate
less heat. RISC processors are ideal for mobile devices where
battery life and comfort in your pocket are an issue.
However, there are implications for application development
Mobile Devices - Development
Mobile devices typically use processors with a
drastically different machine code instruction set.
Developing applications directly on a mobile device is
impractical – touchscreen keyboards are not good for
large amounts of typing and the storage space on a
phone is relatively small
Mobile app development takes place on desktop
computers. Emulation is used for the majority of
testing the program, enabling the desktop to simulate
the phone’s hardware.
App Inventor’s emulator allows mobile phone
applications to be tested on desktop PCs
Questions – Translation
Answer questions in full sentences!
1. Explain why programs need to be translated from HLLs
2. What is meant by emulation?
3. Give one advantage and one disadvantage of using
emulation
4. Explain what a virtual machine is?
5. Explain how virtual machines can help make software more
portable
6. Give two differences between emulation and a virtual
machine.
7. Describe how the machine code instruction set on a
mobile device differs from the instruction set on a desktop
computer
8. Why are RISC processors usually used on mobile devices
9. How is emulation used to test mobile phone apps?
Download