ed milne - queen's universitypost.queensu.ca/~pcclub/programming.pdf · 2015-01-07 ·...

40
Programming Ed Milne

Upload: others

Post on 29-Mar-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Programming

Ed Milne

Page 2: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Theme

● What is programming– How its done

– The tools that are used

Page 3: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Programming

● Software is the instructions which tell your computer what to do

● Programming is the process of creating software

Page 4: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

UML

● Universal Modelling Language– Graphical way of describing program and

system logic

– Can also be used for manual procedures

● There is freeware available for creating UML diagrams

Page 5: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

UML Example

Page 6: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Objects

● Objects are independent entities that perform specific functions within a program

● A program that uses an object does not have to know how an object works

Page 7: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Objects

● Objects can be reused in many programs– This reduces the programming effort

– If the object is successfully working in other programs it reduces the debugging effort

– Objects can also reduce the learning curve for software users

– e.g. the Open function in Windows programs

● Objects are used in other contexts such as word processing

– OLE = Object Linking and Embedding

Page 8: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Linking

● Programs are created as a series of separate modules rather than a single unit

● Linking resolves the addresses of the different modules so control can be passed between them

– The result of linking is an integrated program containing multiple modules

● A linker links the modules together

Page 9: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Dynamic Linking

● Some objects are linked into a program dynamically after it starts

– This reduces the time to start the program and the amount of RAM used

● Many programs have many options that are rarely used

● Dynamically linked modules are kept in Dynamic-Link Libraries (DLL) files

Page 10: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Programming Languages

● Used to tell the computer what to– This is source code of programs

● A major goal is to reduce the human effort needed to create programs

Page 11: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Machine Language

● Machine language is the code used by the CPU● This is just a series of bytes such as

– A0 12 FF 7E 3B

● Such code is time consuming to create and maintain

Page 12: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Assembly Language

● A human readable form of the machine language● There is usually a one-to-one relationship

between an assembly language source code and a machine instruction

● Assembly language is only used for the innermost loops where efficiency is critical

– It gives the finest control over the program instructions

Page 13: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Assembly Language

● An assembler converts the human readable code into machine code

● A disassembler converts machine code into human readable assembly language

Page 14: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Assembly Language Example

.text .globl main main: la $a0, query #First the query li $v0, 4 syscall li $v0, 5 #Read the input syscall move $t0, $v0 #store the value in a temporary variable #store the base values in $t1, $t2 # $t1 iterates from m-1 to 1 # $t2 maintains a counter of the number of coprimes less than m

sub $t1, $t0, 1 li $t2, 0 tot: blez $t1, done #termination condition move $a0, $t0 #Argument passing move $a1, $t1 #Argument passing jal gcd #to GCD function

Page 15: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Compiled Languages

● Compiled languages are higher level language● A compiler converts the source code into

machine code● A decompiler converts machine code into a

higher level language

Page 16: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Procedural Languages

● Before object orientated languages (OOP) procedural languages created a program as one complete unit

● Later versions were enhanced to create objects● The most common procedural languages were

COBOL and Fortran

Page 17: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

COBOL

● COmmon Business Oriented Language is a procedural language used for business applications

● The syntax is based on the English language

Page 18: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

COBOL Example

IDENTIFICATION DIVISION. PROGRAM-ID. TEST.ENVIRONMENT DIVISION.DATA DIVISION 01 some-record. 05 num PIC 9(10). 05 the-date. 10 the-year PIC 9(4). 10 the-month PIC 99. 10 the-day PIC 99.

PROCEDURE DIVISIONIF invalid-record IF no-more-records NEXT SENTENCE ELSE READ record-file AT END SET no-more-records TO TRUE.

Page 19: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

FORTRAN

● FORmula TRANslating system is a procedural language used for numeric and scientific calculations

● The syntax is based on algebra

Page 20: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

FORTRAN Exampleprogram average ! Read in some numbers and take the average ! As written, if there are no data points, an average of zero is returned ! While this may not be desired behaviour, it keeps this example simple implicit none real, dimension(:), allocatable :: points integer :: number_of_points real :: average_points=0., positive_average=0., negative_average=0. write (*,*) "Input number of points to average:" read (*,*) number_of_points allocate (points(number_of_points)) write (*,*) "Enter the points to average:" read (*,*) points

Page 21: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

FORTRAN Example ! Take the average by summing points and dividing by number_of_points if (number_of_points > 0) average_points = sum(points) / number_of_points ! Now form average over positive and negative points only if (count(points > 0.) > 0) then positive_average = sum(points, points > 0.) / count(points > 0.) end if if (count(points < 0.) > 0) then negative_average = sum(points, points < 0.) / count(points < 0.) end if deallocate (points) ! Print result to terminal write (*,'(a,g12.4)') 'Average = ', average_points write (*,'(a,g12.4)') 'Average of positive points = ', positive_average write (*,'(a,g12.4)') 'Average of negative points = ', negative_average end program average

Page 22: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

C● Dennis Ritchie of Bell Labs developed 3

programming languages which he called A, B and C

● C was a success– Enhanced versions were named C+ and C++

– C++ is the current standard for hard core programming such as operating systems

Page 23: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

C++ Example

# include <stdio.h>

int main(void)

{

printf("hello, world\n");

}

Page 24: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

BASIC

● Beginner's All-purpose Symbolic Instruction Code wad initialized designed to teach programming to students

● Variants of BASIC underlay Microsoft Office, Open Office and LibreOffice

● There are many versions of BASIC

Page 25: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

BASIC ExamplePublic Class StarsProgram Public Shared Sub Main() Dim UserName, Answer, stars As String, NumStars As Integer Console.Write("What is your name: ") UserName = Console.ReadLine() Console.WriteLine("Hello {0}", UserName) Do Console.Write("How many stars do you want: ") NumStars = CInt(Console.ReadLine()) stars = New String("*", NumStars) Console.WriteLine(stars) Do Console.Write("Do you want more stars? ") Answer = Console.ReadLine() Loop Until Answer <> "" Answer = Answer.Substring(0, 1) Loop While Answer.ToUpper() = "Y" Console.WriteLine("Goodbye {0}", UserName) End SubEnd Class

Page 26: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Interpreters

● Interpreters directly execute source code without converting it to machine code

Page 27: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

JavaScript

● JavaScript is an interpreted language used in web pages

● An interpreted language is required because– Web pages can only contain ASCII characters

● Pictures, sounds and videos are included by inserting a URL into the web page

● Special characters are included as strings of ASCII characters e.g. &Egrave; for È

– Web designers want their pages to work on Widows, Linux, Mac, tablets and smart-phones without having to write separate versions for each type of device

Page 28: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

JavaScript

● A JavaScript interpreter is included in web browsers

● JavaScript is a Write Once Run Anywhere (WORA) language

Page 29: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

JavaScript Example

<!DOCTYPE html> <meta charset="utf-8"><title>Minimal Example</title> <h1 id="header">This is JavaScript</h1> <script> document.body.appendChild(document.createTextNode('Hello World!')); var h1 = document.getElementById('header'); // holds a reference to the <h1> tag h1 = document.getElementsByTagName('h1')[0]; // accessing the same <h1> element</script> <noscript>Your browser either does not support JavaScript, or has it turned off.</noscript>

Page 30: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Java

● Java is a combination of a compiled and interpreted language

– The source code is compiled into executable code for Java's virtual CPU

– The compiled code is executed by the Java Runtime Environment (JRE)

● JREs are available for many different systems

● Like JavaScript Java is a WORA language

Page 31: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Java

● Java was intended to protect computers from malware

– The programs run in sandbox which is isolated from the rest of the computer

– Hackers have taken this as a challenge and the JRE is under constant attack

Page 32: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Java Example

// This is an example of a single line comment using two slashes /* This is an example of a multiple line comment using the slash and asterisk. This type of comment can be used to hold a lot of information or deactivate code, but it is very important to remember to close the comment. */ package fibsandlies;import java.util.HashMap; * This is an example of a Javadoc comment; Javadoc can compile documentation * from this text. Javadoc comments must immediately precede the class, method, or field being documented. */public class FibCalculator extends Fibonacci implements Calculator { private static Map<Integer, Integer> memoized = new HashMap<Integer, Integer>();

Page 33: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Java Example * The main method written as follows is used by the JVM as a starting point for the program. */ public static void main(String[] args) { memoized.put(1, 1); memoized.put(2, 1); System.out.println(fibonacci(12)); //Get the 12th Fibonacci number and print to console } * An example of a method written in Java, wrapped in a class. * Given a non-negative number FIBINDEX, returns * the Nth Fibonacci number, where N equals FIBINDEX. * @param fibIndex The index of the Fibonacci number * @return The Fibonacci number */ public static int fibonacci(int fibIndex) { if (memoized.containsKey(fibIndex)) { return memoized.get(fibIndex); } else { int answer = fibonacci(fibIndex - 1) + fibonacci(fibIndex - 2); memoized.put(fibIndex, answer); return answer; } }}

Page 34: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Debugging

● Most of the time and effort in programming is spent removing defects (bugs) rather than writing the code

● In spite of this, programs still have bugs– The combinations and permutations of

hardware and other software are too large to be tested

– Some software developers, like Microsoft, have their software send data back to them when it fails

● This lets them use their customers as unpaid testers

Page 35: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

Macros

● Office and programs let you write code by recording your actions in a macro

Page 36: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

LibreOffice IDE

● The Integrated Development Environment (IDE) lets you edit and test BASIC code

● The controls are– Compile – compiles the source code

● The LibreOffice Basic compiler is embedded in the application

– Run – executes the compiled code after a break

– Stop – stops executing the code

– Step Over – steps over the next instruction

– Step Into – execute the next instruction

Page 37: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

LibreOffice IDE

– Step Out – returns to the previous routine in the current macro

– Breakpoint – stop executing when an instruction is reached or remove a breakpoint

– Enable Watch – displays the variables in the current macro

– Insert Source Text – inserts additional source code into the macro from a file

● In the large pane, you can edit the source code or enter new code

– You can find some useful functions online

Page 38: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

LibreOffice Basic Function

Function SmallIntToText(ByVal n As Integer) As String REM by Andrew D. Pitonyak Dim sOneWords() Dim sTenWords() Dim s As String

If n > 999 Then Print "Warning, n = " & n & " which is too large!" Exit Function End If

sOneWords() = Array("zero", _ "one", "two", "three", "four", "five", _ "six", "seven", "eight", "nine", "ten", _ "eleven", "twelve", "thirteen", "fourteen", "fifteen", _ "sixteen", "seventeen", "eighteen", "nineteen", "twenty") sTenWords() = Array( "zero", "Ten", "twenty", "thirty", "fourty", _ "fifty", "sixty", "seventy", "eighty", "ninety")

Page 39: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

LibreOffice Basic Function

s = "" If n > 99 Then s = sOneWords(Fix(n / 100)) & " hundred" n = n MOD 100 If n = 0 Then SmallIntToText = s Exit Function End If s = s & " " End If

If (n > 20) Then s = s & sTenWords(Fix(n / 10)) n = n MOD 10 If n = 0 Then SmallIntToText = s Exit Function End If s = s & " " End If

Page 40: Ed Milne - Queen's Universitypost.queensu.ca/~pcclub/Programming.pdf · 2015-01-07 · JavaScript JavaScript is an interpreted language used in web pages An interpreted language is

LibreOffice Basic Function

SmallIntToText = s & sOneWords(n)End Function