languages & environments · web viewprogrammers realised that they had to develop low-level...

28
Languages & Environments

Upload: others

Post on 24-Feb-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments

Page 2: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

You should be able to describe the key characteristics of the following language types:

1. Low-level languages 2. High-level languages 3. Procedural languages 4. Declarative languages 5. Object-orientated languages

Low-level languagesAll processors understand data in binary form only. The value 180 is represented and processed in binary as: 10110100.

Reminder

Why is 10110100 the binary representation of 180?

128 64 32 16 8 4 2 1

1 0 1 1 0 1 0 0

128 + 32 + 16 + 4 = 180

Creating binary code for every instruction would be time consuming and require a lot of concentration. One slight error could send the wrong instruction to the processor.

Programmers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently.

The two common low-level languages are and machine code assembly code.

2 Higher Computing Science – Software Design & Development – Bitesize

Page 3: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Machine code (using hexadecimal)Machine code is the term used to describe binary instructions, e.g 0101101010101010010010100111111

Any low-level or high-level language instruction will ultimately end up as a binary instruction.

However, machine code can be represented by the use of hexadecimal code as a low-level language.

Hexadecimal code is the lowest form of programming language used by programmers. It cannot be understood by the processor but it makes binary more readable for humans. It is changed to binary for the processor to understand.

Earlier, we saw how 180 can be converted to binary as 10110100

Using hexadecimal the same instruction can be represented as B4

In hexadecimal the letter B represents the number 11. Hexadecimal code works to the power of 16 (Base 16). The hexadecimal representation of 180 is B4:

16 1

B 4

This means we are using 16 eleven times (as B represents 11) and we are using 1 four times.

16 x 11 = 176

1 x 4 = 4

176 + 4 = 180

We can see that B4 is a more succinct way of representing 180 than 10110100. Programming in hexadecimal saves time when compared to using binary instructions.

Here are the values 1-26 represented in decimal, binary and hexadecimal.

Decimal Binary Hexadecimal

0 00000000 00

1 00000001 01

2 00000010 02

3 00000011 03

4 00000100 04

5 00000101 05

3 Higher Computing Science – Software Design & Development – Bitesize

Page 4: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Decimal Binary Hexadecimal

6 00000110 06

7 00000111 07

8 00001000 08

9 00001001 09

10 00001010 0A

11 00001011 0B

12 00001100 0C

13 00001101 0D

14 00001110 0E

15 00001111 0F

16 00010000 10

17 0010001 11

18 00010010 12

19 00010011 13

20 00010100 14

21 00010101 15

22 00010110 16

23 00010111 17

24 00011000 18

25 00011001 19

26 00011010 1A

4 Higher Computing Science – Software Design & Development – Bitesize

Page 5: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Here we can see an example of the value for 500 in both binary and hexadecimal:

Binary

256 128 64 32 16 8 4 2 1

1 1 1 1 1 0 1 0 0

256 + 128 + 64 + 32 + 16 + 4 = 500

Hexadecimal

256 16 1

1 F 4

F represents 15 in hexadecimal so...

(256 x 1) + (16 x 15) + (1 x 4) = 500

An example of a complete binary instruction and the same hexadecimal instruction is shown below:

10110100 00000000 10110000 00010011 11001101 00010000

B4 00 B0 13 CD 10

Hexadecimal values are used to write instructions that are easier for humans to work with than machine code.

5 Higher Computing Science – Software Design & Development – Bitesize

Page 6: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Assembly codeAssembly code is another form of low-level language. Assembly code is created by the developers of processors. Assembly languages are architecture dependent - the form of assembly code depends upon the manufacturer of the processor.

Like hexadecimal code, assembly code does need some form of alteration before it can be understood by the processor. An assembler is the name given to the software that converts assembly code into binary instructions.

Assemblers generally allow for one to one conversion between an assembly language instruction and binary code. Assembly language is often used when high performance is crucial. The main drawbacks of assembly languages are lack of portability and the time that it takes to learn how to program.

Here is the same instruction three times, once in binary, once using hexadecimal notation and once using an assembly language.

Binary instructionHexadecimal machine code

Assembly code

101101000000000010110000000100111100110100010000 B4 00 B0 13 CD 10mov ah, 0 / mov al, 13h / int 10

Key points

Low-level languages generally allow for a one to one conversion into machine code (binary) Hexadecimal notation is a low-level language Assembly code is a low-level language Assembly code is architecture dependent (different codes exist for different types of

processor) Assembly code is not fully portable because of architecture dependence Assembly code needs to be converted to binary using an assembler Low-level languages are hard to learn

Low-level languages are used for the following reasons:

Optimisation of code Where no translator software exists Greater control of hardware features (e.g. in small embedded computers such as home

appliances)

Optimisation of code means creating code that places as little demand on system resources as possible. Low-level languages will place a much lighter load on RAM than a high-level language.

High-level languages would require more space in processor and memory to hold the translator software that will be used to create binary.

6 Higher Computing Science – Software Design & Development – Bitesize

Page 7: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

7 Higher Computing Science – Software Design & Development – Bitesize

Page 8: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

High-level languagesMost contemporary programming is carried out using high-level languages. High-level languages have the following characteristics:

Require translation Portable Easier to read, write and maintain as commands are similar to English Allow access to module libraries Use data types and data structures, selection statements and repetition/iteration constructs Use logic operators and functions that are built into the language

High-level languages require translator software that will be held in RAM when a program is executed. It is the translator software that will take the code written in a high-level language and translate it into 'executable code'. Executable code is in binary form and can be understood by the processor. Unlike low-level languages, there is no one to one conversion between code and binary instructions.

8 Higher Computing Science – Software Design & Development – Bitesize

Page 9: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Compiler v InterpreterTranslator software usually comes in the form of either a compiler or an interpreter.

When translating using an interpreter, each line of source code will be translated to machine code one line at a time. This means that the interpreter software is present in main memory during each execution of the program and for the execution of every line. In this sense, an interpreter is not as efficient as a compiler.

The advantage of using an interpreter is that the execution will stop immediately if the interpreter translates a line of source code that results in an error. This makes an interpreter a useful tool to assist programmers as they attempt to identify and rectify errors. The interpreter cannot determine the actual error but can indicate the line that was not translated into executable machine code.

A compiler will translate the source code into what is known as object code. The compiler is held in RAM only for the time that it takes to translate the source code into object code.

Once object code has been created it is possible to run this code independently of the development environment and translator software. Using a compiler once to create object code makes run time more efficient but if the source code contains errors the programmer would need to re-compile to make use of the corrected object code.

As high-level languages depend upon translator software to convert instructions into binary, they do not depend on a specific hardware architecture. This means that a program written in a high-level language can be translated for use on systems using different processors, provided that translator software exists for the particular processor architecture. Software that can be used in this way is called 'portable' software.

High-level languages are easier to read, write and maintain than low-level languages. This is because high-level languages use terms that are similar to English. An example is the print command that is used in various high-level languages.

The following line of code from a high-level language is easy for a human to understand.

Print 'hello world'

9 Higher Computing Science – Software Design & Development – Bitesize

Page 10: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Without a lot of background knowledge it is possible for a human to understand that this line of code would show the words ‘hello world’ on screen. This also makes high-level languages easier to maintain in the future if a programmer needs to alter code that has been created by another programmer.

Programmers can also access module libraries when they are using a high-level language. Module libraries contain sections of pre-written and pre-tested code that can be used without the need to spend additional time on creation and testing.

As well as being able to access module libraries, programmers can make use of in-built functions such as length(), which can be used to return the length of string variable.

High-level languages will also make use of data types such as String, Integer, Boolean and Real as well as data structures such as 1-D Arrays and, in some cases, records. Selection statements such as nested If Statements and Case statements as well as the use of iteration are also features of high-level languages.

There are three forms of high-level language within the course:

Procedural language Declarative language Object orientated language

Each language has its own set of characteristics and makes use of different features of high-level languages.

10 Higher Computing Science – Software Design & Development – Bitesize

Page 11: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Procedural languagesProcedural languages are designed to allow programmers to create code that will be processed logically and in a structured order. Code is contained within procedures (also called subroutines) that carry out one main task. Procedures will be created to allow a series of steps to be followed. Data can be passed from procedure to procedure using parameters.

The following chart simplifies the idea of creating independent sections of code that can pass data from procedure to procedure within the structure of a procedural language.

The concept of passing data from procedure to procedure is called parameter passing and is examined in more technical detail within section 3, Computational constructs.

Examples of procedural languages are BASIC, C, Pascal and Python.

This program is very small so does not have a procedure, however is does show that the programmer created a set of instructions which are executed in order to provide a solution to a problem.

11 Higher Computing Science – Software Design & Development – Bitesize

Page 12: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

facts

rule

Languages & Environments 2015-6

Declarative languagesDeclarative languages are often used by intelligent systems. Declarative languages are very different to the other types of language because of the way that the language is structured. Programmers will create a knowledge base. A knowledge base contains a series of related facts and rules that can be queried to provide solutions. Examples of Declarative programming languages are: PROLOG, Lisp.

ExampleSuppose we want to find out whether a person drives a fast car. We start by building a set of facts and rules for our knowledge base.

In this example we could ask the program to tell us whether Judy drives a fast car by typing the query:

?drives_fast_car(judy)

The result would be YES since the goal is satisfied.

If we asked:

?drives_fast_car(james)

then the result would be NO as drives_car(james, porche) would fail and the goal is not satisfied.

Contrast this with a procedural language where the programmer would need to set up a structure to hold the knowledge and predefine its type (string, number etc). Then they would need to describe the steps taken to search the structure in order to answer the query.

X and Y represent variables; when using Prolog, variables should always be written as capital letters but declarative languages do not require the programmer to declare a data type. It is necessary to be able to read, understand and create simple facts and rules in the format shown.

Queries depend upon in-built algorithms that will carry out pattern matching to return a result. Heuristic, breadth-first and depth-first searches are often used to perform pattern matching when querying a knowledge base.

12 Higher Computing Science – Software Design & Development – Bitesize

What is means:-

judy is a person

james is a person

james drives a car that is a ford escort

judy drives a car that is a porche

X drives a fast car IF X drives a car that is

a porche

Page 13: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Declarative languages like Prolog allow for the use of self-modifying code. This means that during execution the program can modify the facts and rules that it holds if necessary.

RecursionAnother key feature of declarative languages is the use of recursion. Rules are normally created to call other facts or rules. Recursion takes place when a rule calls itself. A commonly used example of recursion is that of a rule about ancestry.

ancestor_of(X,Y):- parent_of(X,Z), ancestor_of(Z,Y)

This reads as 'X is an ancestor of Y if X is a parent of Z and Z is an ancestor of Y'. Recursion is evident here as the rule ancestor_of(X,Y) calls itself within the rule definition – ancestor_of(Z,Y).

Activity: PROLOGThis PROLOG program consists of a set of facts about a set of individuals. Thefacts describe who is warm, who is fed and who is cold. There are two simple rules todetermine whether someone is happy or unhappy.

13 Higher Computing Science – Software Design & Development – Bitesize

Page 14: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Match the answers to the following queries:

1) ?warm(fred)

a) Yes

b) No

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2) ?happy(jack)

a) Yes

b) No

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3) ?happy(fred)

a) Yes

b) No

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4) ?unhappy(X)

a) X = jim

b) X = fred

c) X = justin

d) X = jim, X = Justin

14 Higher Computing Science – Software Design & Development – Bitesize

Page 15: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Object-orientated languagesProcedural languages rely on the process of passing data from one procedure to another. Procedures and the code within them exist independently of the data. They simply access data deemed relevant by the programmer as and when necessary. The data used is separate from the code.

However, object-oriented languages treat data and code as part of the same object. Firstly a class is created. A class acts as a template for an object and classes are examined in more detail later in this section. Objects are created within a class and will contain code and the data that the code requires. Data and code is not held separately. The name given to the concept of an object containing both the code and its related data is called 'encapsulation'.

In our world there are many objects such as cars, boats, buses, doors etc. To think of objects in this way is a good starting point. An object such as a train will have a set of attributes.

15 Higher Computing Science – Software Design & Development – Bitesize

Page 16: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

AttributeWhen describing objects it is common to make reference to attributes. Attributes describe an object. Attributes are sometimes called fields or instance variables.

StateThe term 'state' describes the value of an attribute/instance variable at any given time. The following example shows different attributes for an object, in this case a train. At any point in time the attributes will have a state. The state of an attribute is simply its current value.

Attribute/Instance variable State

Manufacturer Mordor

Power source Coal

Maximum speed 80mph

Capacity 300

Carriages 6

Operation/MethodIf a change is necessary we can alter an attribute’s state. The process of making a change to a state is called an operation. The term 'Method' is sometimes used instead of operation. In this example an operation could be to add another carriage to the train. This would change the state of the 'Carriages' attribute to 7.

In computing an example of an object could be a line drawn in a vector graphics package.

If an operation was carried out to change the line colour to black then the state of the line colour attribute would change from blue to black.

16 Higher Computing Science – Software Design & Development – Bitesize

Page 17: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Class, Sub Class, Super Class, and InstanceThe term class is used to describe a group of objects that will have some common characteristics. In the example of 2-D vector graphics there would be a class called 'Shape'. Each type of shape within the class is known as a sub class.

In this example 'Shape' is a super class, meaning that there are a large number of different sub classes that could be thought of as shapes. As well as super classes and sub classes, there are also instances shown. An instance is a single member of a sub class.

In the example shown, the triangle subclass has two instances, equilateral and isosceles.

Operations/Methods are defined as part of a class and are then available to every object created within a class. There may be an operation that is created to allow the colour of a shape to be altered. In this case, the operation could be defined as part of the shape class and would then be available to each sub class and instance.

InheritanceInheritance is an excellent feature of object-oriented programming languages. Inheritance means that a subclass will inherit the characteristics of the superclass, similarly an instance will inherit the characteristics of a sub class.

In essence this means that we could define attributes and operations for the super class shape. Each time a sub class is created it will automatically inherit the attributes and operations of shape. The programmer only needs to add attributes and operations that make that subclass unique.

If line thickness were regarded as an attribute of a shape then each time a new sub class was created it would automatically contain an attribute for line thickness. This makes it much easier to make changes to objects as a change to the characteristics of a superclass will be reflected in the same change being made in each subclass and instance.

Inheritance allows for the re-use of the same code when creating a subclass or instance, such code can be contained within a class library, further reducing implementation time.

17 Higher Computing Science – Software Design & Development – Bitesize

Page 18: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Activity : Inheritance diagramQ14: Match the words to the correct nodes to complete the inheritance diagram:

Code example: BYOB (Build Your Own Blocks)BYOB is an example of an object-oriented language used to teach programming. In this example, the boy sprite has inherited the attributes of the general sprite class, but has the additional specific attributes of several costumes to give a walking animation effect. It communicates with the stage when it is touching the edge by sending a message. When the stage receives the message it changes its background attribute to display the next image.

18 Higher Computing Science – Software Design & Development – Bitesize

Page 19: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Code example: JavaIn this example of the Java programming language, there are three methods defined within the average class: fillarray() which returns an integer array, average() which returns an integer value and the main method which calls fillarray() and average() and displays the average of the values in the array.

19 Higher Computing Science – Software Design & Development – Bitesize

Page 20: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

20 Higher Computing Science – Software Design & Development – Bitesize

Page 21: Languages & Environments · Web viewProgrammers realised that they had to develop low-level languages that were easier for humans to use but that could be converted into binary efficiently

Languages & Environments 2015-6

Mind Map

21 Higher Computing Science – Software Design & Development – Bitesize