29
PHY281 Introduction Slide 1 Introduction Scientific Application Programming PHY281 http://www.lancs.ac.uk/phy281 Email: [email protected]

PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281 Email: [email protected]

Embed Size (px)

Citation preview

Page 1: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 1

Introduction

Scientific Application Programming

PHY281

http://www.lancs.ac.uk/phy281

Email: [email protected]

Page 2: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 2

Recommended Books

Java for Students - Third Edition Douglas Bell and Mike Parr Prentice Hall 2002ISBN 0-13-032377-2(Second Edition is not much different)

The main book is The Java Tutorial, which is available online at http://java.sun.com/docs/books/tutorialBut you may also want to look at

Page 3: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 3

Reference Book

Beginning Java 2 - JDK 1.4 Edition Ivor Horton Wrox Press 2002ISBN 1-861005-69-5

Many many Java books in every bookshop. For a very large reference book:

(Not necessary for this course)

Page 4: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 4

Organisation

Assessment for the course is by coursework only. There is no exam.

The marks will not be uniform week-by-week. In the early stages, you will be completing some fairly standard exercises in order to learn the techniques. Some degree of group working is acceptible in these parts, although you must type in the solutions yourself and not just copy.

As the weeks progress, you will be asked to do more original work. This must be entirely your own work, and will be awarded more marks.

Late work will be treated according to the usual rules. You are reminded that the university has strict rules about plagarism, to which you have agreed to abide. Some automated plagarism-checking software may be used on your submitted answers. Except in the most trivial ("Hello World") cases programs that differ only in the names of variables or in the spacing will be treated as suspect and investigated.

Page 5: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 5

Introduction to Computers

In this section we will briefly review the history of computers and the components of a computer and its operating system:

A brief History of Computers

Types of Computer

Inside the computer

The Motherboard

PC Components

CPU Speeds and Memory Size

The Operating System

Running a Program

Page 6: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 6

A Brief History of Computers

Early 'Computers'

3000 BC Abacus invented and used for quick, manual addition and subtraction calculations.

1645 A mechanical adding and subtracting machine called the Pascaline (after Blaise Pascal) invented.

1830 Charles Babbage (the Father of Computers) designed first electronic computer called the Difference Machine. It was never built but today's computers are based on it.

1890 Punched Cards invented to tabulate 1890 census. Punched cards used for computer input throughout 1960's.

1939 First vacuum tube calculating machine invented.

Page 7: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 7

A Brief History of Computers

'Modern' Computers:

1943 First electronic computer built at Bletchley Park for code breaking. 1946 First true general purpose, vacuum tube (1st generation) computer called ENIAC invented.1959 First solid state (2nd generation) computers using transistors.1965 First integrated circuit (3rd generation) computers developed by NASA for moon landing programme.1975 First 'home computers' - Altair 8800 - no keyboard or monitor and no software! Came in kit form.1977 Apple II - fully assembled home computer.1981 Introduction of the IBM PC - the forerunner of most of today's PCs.1984 Apple Macintosh - the forerunner of most of the rest.Today The PC architecture remains the same - just much faster and cheaper.

Page 8: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 8

ENIAC

ENIAC (Electronic Numerical Integrator and Computer) 1946

Page 9: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 9

Types of Computer

SupercomputersVery expensive - so fast they have to be cooled by liquid gases such as Helium. Limited to a few large government and business organisations e.g. for long term weather forecasting.

MainframesCornerstone of large business, data processing where heavy processing and several thousand users must be handled at once e.g. for bank accounts.

MinicomputersA multiuser computer which can handle up to 300 users. In the 1970s and 1980s used by medium sized businesses and organisations that could not afford mainframes. Now virtually extinct.

MicrocomputersThe smallest, least expensive and most popular computers - often called desktop computers or personal computers (PCs). Based on Intel (8088, 80286, 80386, 80486, Pentium, Pentium II and Pentium III) or compatible (AMD Athlon, Duron etc) chips.

Page 10: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 10

Computer Hardware

Input DevicesOutput Devices System Unit

Peripheral Devices

Page 11: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 11

Inside the PC

Case Fan, Power indicators etc

Floppy and Hard Drives

CD ROM Drive

Peripheral Connections

Motherboard

Power Supply

Power Connections

Drive Connections

Page 12: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 14

CPU Speeds and Memory Size

CPU Speed

CPU Speed measured in Megahertz (MHz). Determines the number of instruction cycles the CPU can perform in a single second.

i.e. an 850MHz PC can process 850 million instructions per second.

Random Access Memory (RAM)

RAM and disk space measured in KB, MB or GB.

A bit is a single zero or one.A Byte is a single character (usually) equal to 8 bits.1KB is 1,024 Bytes. 1MB is 1,024 KB. 1GB is 1,024 MB (Note: 1,024 not 1,000)

A 19.5 KB file contains 19.5 X 1,024 = 19,968 Bytes.A 1.95 GB Disk holds 1.95 X 1,024 X 1,024 X 1,024 = 2,095,316,992 Bytes

Page 13: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 15

The Operating System

The Operating System is the interface between the hardware and the programs and data.

When a program issues a command to access a disk file or a peripheral device the operating system carries out the work for it.

Mostly Macintosh, Windows 3.1, 3.11, 95, 98, ME, XP, NT, 2000 or Unix (mostly Linux).

HARDWARE

PROGRAMS

OPERATING SYSTEM

Word Processor Internet

Explorer

Netscape

Your Program

Games

Page 14: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 16

Running a Program

What happens when you run a program (yours or someone else's)?You issue a

command to start the program (click on its icon).

The Program runs (executes) following the instructions in the code

RAM

Disk, Keyboard, Mouse etc

Monitor, Printer etc

Operating System

Interprets command searches disk or CD ROM for program's executable file.

Loads program from disk to RAM (not necessarily all at once).

Reads or writes to peripheral devices (e.g. the Monitor or disk).

Regains Control, frees RAM, closes files etc.

The program ends, you stop it or it crashes.

Page 15: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 17

What is a Program?

A Program is a list of detailed instructions that the computer carries out.

To make a cup of coffee the following would not be sufficient:

The instructions would have to be much more detailed:

You have to say exactly what to do in the right order with no ambiguity.

1. Boil water2. Put coffee in cup3. Pour in hot water4. Add milk

1.1. Go to kettle1.2. Check kettle has water in it1.3. If not 1.3.1 Take kettle to cold tap 1.3.2 Remove lid of kettle 1.3.3 Put kettle under cold tap 1.3.4 Turn on tap 1.3.5 When kettle full turn off tap 1.3.6 Return kettle to worktop1.4. Plug kettle into mains1.5.Switch on electricity…….

Page 16: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 18

Early Programming

Binary numbers are all that computers understand. They speak ‘Machine Language’. The instructions as to what to do (the Program) and the data have to be given to the computer hardware in binary.

The early computers (late 1940s to early 1950s) had to enter these digits directly into memory using switches or later via paper tape or punched cards.

•Coding very error prone•Coding very tedious and slow•Difficult to modify program•Impossible to decipher what a program did•Not portable to other computers

0011 10011111 01101111 10001111 1010

The first word might be an instruction to add the number at the location given by the next word (246) to the number at the location given by the 3rd word (248) and store the result at the location given by the 4th word (250).

Page 17: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 19

Assembly Languages

The second generation of computer languages (early 1950s to present) use a set of mnemonic symbols known as an Assembly Language to represent the instructions and data. The previous example in assembly language could be:

ADD A, B, C

An Assembler translates the mnemonic symbols into machine language (one to one correspondence).

ADD A,6MOV A,OUTLOAD BSUB B,A

Assembler

0011 01101100 01001011 10110001 1101

The programmer types the mnemonics

Load and run the program

The assembler translates to machine language

More programmer friendly but still tedious to use, difficult to modify etc.

Page 18: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 20

High Level Languages

The third generation of computer languages (mid 1950s to present) are known as high level languages and are much closer to English.

Each instruction in a high level language corresponds to many instructions in machine language. Translation is done by a compiler or interpreter. A linker links together different parts of the program.

Programs written in high level languages are generally portable though they might have to be re-compiled on different computers. Almost all software to day is written in high level languages.

A = 1B = 2C = A + BPRINT C

Compiler

0101 10001101 11001100 1110….. Linker

1011 10011001 0010…..

Executable File

Source File Object Files

You execute (run) thisYou type this in

Page 19: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 21

High Level Languages

Early examples of high level languages are FORTRAN (Formula Translator) and COBOL (Common Business Oriented Language) both of which are still in use today, although decreasing with time .

FORTRAN is used for mathematical and scientific applications and offers excellent libraries of built in trigonometric routines to aid the developer. FORTRAN has evolved over the years (FORTRAN 4, FORTRAN 66, FORTRAN 77 and now FORTRAN 90. Although many existing scientific applications are written in FORTRAN, it has lost ground over the years first to PL/I (Programming Language 1) and Pascal and then to C, C++ and Java.

COBOL was developed as a self-documenting language for processing large amounts of business data such as inventory and personnel records. Instead of being self-documenting, COBOL became very wordy. COBOL is still one of the most common languages in use today although newer programs in other languages such as C++ are slowly replacing it.

Page 20: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 24

Modern Programming Languages

COBOL and FORTRAN and other early programming languages are procedural languages. That is they provide individual instructions that the computer follows in a sequential matter until the job is done. It is possible to read through the code predicting exactly what will happen. The results are generally just text output to a printer and a dumb terminal or terminal window under DOS.

The popularity of Graphical User Interfaces (GUIs) such as the Macintosh or Windows has seen a rapid trend towards Graphical Programming Languages. The procedural languages don’t lend themselves well to the needs of the graphical environment where the computer doesn’t follow the instructions sequentially all the time but can be interrupted by mouse clicks etc.

Page 21: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 25

C

The C language was developed at Bell Laboratories by Brian Kernighan and Dennis Ritchie in 1972 to write a new operating system (which became Unix).

C is a high low-level language in that it supports all the programming constructs of a high-level language but compiles into very tight and efficient code that runs almost as quickly as assembler language.

C’s popularity grew rapidly and nowadays almost all PC programs are written in C (or C++). The demand for C and C++ programmers continues to grow.

C is a language that has few words and numerous operators that are special characters that perform some operation on data (such as the + and - operators). Because of this C programs can be rather cryptic and is not necessarily the best choice for a first computer language.

Page 22: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 26

Example C Program

#include <stdio.h>

/* Print Fahrenheit to Celsius conversion table */

main(){ float fahr, celsius; int lower, upper, step; lower = 0; /* Lower limit of table */ upper = 300; /* Upper limit */ step = 20; /* Step size */ fahr = lower; while (fahr <= upper) { celsius = (5.0/9.0) * (fahr-32.0); printf ("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; }}

Comments between /* amd */

0 -17.8 20 -6.7 40 4.4 60 15.6 80 26.7100 37.8120 48.9140 60.0160 71.1180 82.2200 93.3220 104.4240 115.6260 126.7280 137.8300 148.9

Which produces this output:

All variables have to be declared

Lines are free format with a semicolon to end each statement

Includes standard code from elsewhere

Page 23: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 27

C++

C++ is a newer version of C. It was designed by a Swedish programmer named Bjarne Stroustrup in the early 1980s.

C++ is based on C and many C++ programs look exactly like C programs. C++ is more restrictive than C.

Much of the popularity of C++ is because of its Object-Oriented (OO) capabilities.

Page 24: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 28

Example C++ Program

#include <iostream.h>

// Calculate the Fibonacci numbers below 1000

int old_number; // Previous Fibonacci numberint current_number; // Current Fibonacci numberint next_number; // Next number in series

int main () { // Start things off old_number = 1; current_number = old_number; cout << "1"; // Print first number while (current_number < 1000) { cout << " " << current_number; next_number = current_number + old_number; old_number = current_number; current_number = next_number; } cout << endl; // End the output line return (0);}

C++ comments follow //(Can use C /* … */ as well)

Similar statements to C

Different method of outputting results

1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987Which produces this output:

Page 25: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 32

Java

Java was developed by Sun Microsystems in the mid 1990s and has language features similar to C++.

You can write two kinds of Java programs:

• Java Applications are complete standalone programs like those produced with other languages.

• Java Applets are small programs that download with web pages and execute on the web users computer inside their browser.

Whereas most language compilers turn programs into machine-dependent executable programs, Java compilers compile your Java code into a special machine independent form called bytecode. (This is effectively the machine language of a virtual machine and not a specific type of computer).

The web browser or a Java interpreter translates the bytecode into machine code for the user's specific computer.

Page 26: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 33

Object Oriented Programming

Java and C++ are Object Oriented (OO) Programming Languages.The key aspect is Objects. Objects allow you to build complex programs out of smaller simpler building blocks. Objects in a program can mirror objects in the real world. They have :• Things that describe the object called attributes - colour, size etc• Things the object can do to its data or to something else called methods.

A word processor has objects - pages, lines, headings etc.A line object might have: • attributes - length, the data - words (themselves perhaps objects) etc.• methods - create, delete, insert word, display etc. In Object Oriented Programming you don't need to describe every object (e.g. every line object ) separately - you describe a general type of object called a class.

Then every individual object (e.g. each line object) is an instance of this class created using the instruction called new. For example, you could have a class Table describing the general features of a table in a restaurant, and then have several instances of Table objects representing each actual table.

Page 27: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 34

Object Oriented Programming

Another key aspect is Inheritance.

You don’t have to describe different but similar objects completely. You can define a superclass (sometimes called base class) that has the common attributes and methods and inherit these adding only the ones that are different.

Vehicle

Bus Lorry

Speed, Colour Start, Stop

Maximum LoadPick up Load

Maximum PassengersPick up Passengers

These are inherited

Page 28: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 35

Which Language to Choose?

We want to write Scientific Applications with Graphical Interfaces.

The Easiest Way - use Visual Basic

Advantages • Very easy to use• Can create powerful applications• Simple BASIC syntax

Disadvantages• Windows only• Not proper OO language• Not compiled - need to distribute large system libraries • Relatively slow execution.

The New Way - use Java

Advantages • Proper OO Language• Easier to learn than C++• Can create powerful Applications and Applets• Portable• Free• Very similar syntax to C++

The Standard Way - use C++

Advantages • Can create very powerful applications• Decent OO language

Disadvantages• Hard to learn• Graphical applications not portable

We will use Java

Page 29: PHY281IntroductionSlide 1 Introduction Scientific Application Programming PHY281  Email: Roger.Jones@lancs.ac.uk

PHY281 Introduction Slide 36

1 Java: programming language2 C++ : programming language3 SQL (Structured Query Language): database/programming language4 Unix :operating system5 Oracle: database/programming language6 Windows NT: operating system7 Visual Basic: programming language8 HTML (Hyper Text Mark-up Language): web programming language9 C: programming language10 TCP/IP (Transmission Control Protocol/Internet Protocol): networking

standard11 XML (Extensible Mark-up Language): web programming language12 LAN (Local Area Networks)13 Object-oriented programming14 WAN (Wide Area Networks)15 Windows (general): operating system16 ADA: programming language17 Sun Solaris: operating system18 CORBA (Common Object Request Broker Architecture): software standard19 WAP (Wireless Application Protocol): wireless mobile networking standard

Skills in Demand

Source: Survey of Appointments Data & Trends (SSP/Computer Weekly, SSP 2001)