29
Programming for Programming for Beginners Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Embed Size (px)

Citation preview

Page 1: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Programming for Programming for BeginnersBeginners

Martin Nelson

Elizabeth FitzGerald

Lecture 1: Introduction: Program Structure & Java Syntax

Page 2: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Aims & objectives of this course

Write your own programmes in a procedural language

Understand basic concepts such as procedural logic, variables, flow control, input and output principles

Identify important programming concepts

Page 3: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Course website

Course materials, exercises and useful web links can be found at:

www.maths.nottingham.ac.uk/~pmzmn/GSTPFB.html

Page 4: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Session 1 - aims & objectives

Appreciate how computer programs are constructed

Understand the differences between: Compiled and interpreted languages Procedural and object-oriented languages

Find out the basics of Java programming Write a few simple programs making use of

statements, comments and basic arithmetic

Page 5: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Computer programmes

Set of instructions for the CPU “Switch settings” Machine language

Assembly language Mnemonics representing binary code Assembler

3rd generation languages “High level” languages Compiled or interpreted (or both!)

Page 6: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

High Level Languages (3GL)

Examples: Fortran COBOL Pascal C C++ C# Java

Page 7: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Program construction

Computers are 'stupid' Mathematical and logical instructions are executed very

quickly and accurately They obediently but stupidly do what you tell them to –

not necessarily what you want them to! Tiny errors in a program can cause major problems when

it is executed

Careful planning is essential Flow charts are a useful tool to represent program

flow visually

Page 8: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Algorithms

Algorithm = how you go about solving a puzzle; your solution to a task

Example: how do you make a cup of tea?

In programming, you should plan your algorithm before you start coding

1. Boil kettle2. Put tea in cup

3. Pour boiling water into cup

Page 9: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Tea-making program – 1

Is kettle full?

Boil kettle

Pour boiling water into cup

Fill kettleNo

Yes

Put tea in cup

Page 10: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Elements of tea-making program

Fill kettle

Open tap

Wait

Is kettle full?

Yes

No

Close tap

Place kettle under tap

Has kettle boiled?

Pour water into cup

Boil kettle

Switch kettle on

Wait

No

Yes

Plug kettle into electrical socket

Page 11: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Tea-making program – 2

Is kettle full?No

Yes

Put tea in cup Pour boiling water into cup

Page 12: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Now for some jargon…

(… but don’t worry about this too much )

Compiled vs interpreted languages

Handy hint:If you’re not sure what these and other words mean, look at the ‘Glossary’ section of the course website

Page 13: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Compiled languages

Data storage

CPU/memory

Source code

Compiler

Machine code

Program execution

Page 14: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Interpreted languages

Data storage

CPU/memory

Source code

Interpreter

Machine code

Program execution

Page 15: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Compiled vs interpreted

Compiled Development more

cumbersome

Easy to distribute Machine code

generated at compile timee.g. most high-level languages

Interpreted Easy to develop

Distribution of interpreter required

Machine code generated at runtime

e.g. BASIC, LISP, Perl

Page 16: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

The Java model

Java is an object-oriented high level language "write once, run anywhere" It is both compiled and interpreted Java source code has .java extension Source code is compiled to produce a .class file

(bytecode) – not human-readable Bytecode is interpreted by the Java VM (virtual

machine)

Page 17: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

How Java works

Source code myprogram.java

Compiler

Bytecode myprogram.class

Program execution

Interpreter

Machine code

Data storage

CPU/memory

Page 18: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

A bit more jargon…

(… nearly finished though )

Procedural vs object-oriented programmes

What is all this object-oriented stuff about?

Does it matter?

Does it mean anything?

Page 19: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Procedural vs object-oriented

Procedural Early high-level

languages Contain functions

(or sub-routines) written and used inside the main program

Cannot use external functions easily

Object-oriented (OO) Later high-level

languages Contain methods

(or functions) and variables that can be written in main or external programs

Can call external functions or variables easily

Page 20: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Some examples

Procedural Fortran COBOL

(old versions) Pascal C Perl

Object-oriented (OO) COBOL (latest

version) Delphi C++ C# Java Visual Basic Perl

Page 21: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

What’s Java all about then?

NOT the same as JavaScript Java SDK consists of 2 components:

Java VM (Virtual Machine) Java API (Application Programming Interface)

+ accompanying documentation

If you alter your code you need to re-compile it before you can run the program

ONLY use Java version 2 and above (Java 1.2/Java 1.3/Java 1.4 etc)

Page 22: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

On with the code!

Quick intro to using Java

Then YOU start to code your first Java program!

Page 23: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Writing your first Java program

Use a text editor to write the source code Save it as a .java file Compile it using Java SDK on Granby

You need to have a Granby account before you can start writing your programs – if you haven't you will need to apply for one as soon as possible!

Page 24: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

'Hello world' program in Java

class myprog{

public static void main(String[] args){

System.out.println(“Hello world!”);

}

}

Page 25: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Be careful what you type!

If you type something wrong, your code will either give you an error or won’t work properly.

If you get an error, check the following: Capital letters are different to lower case – don’t

mix them up. { }, ( ) and [ ] all do different things – have you

used the right one? Some lines need to end with a semi-colon – miss

them off and your code won’t work.

Page 26: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Code Presentation Tips – 1

Indent code inside curly braces Every time you open a pair of curly braces, indent the

next line by 1 tab or three/four spaces. When you close braces, unindent. You can then see straight away if you have a brace

missing.

class myprog{ Some code Some more code { New braces, so indent again. }

More code}

Page 27: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Code Presentation Tips – 2

You can add comments to your code to remind you (or someone else) how the code works.

Comments are ignored by the compiler. On a single line, anything after // will be ignored. Over many lines, anything between /* and */ will

be ignored./* This is my first java code * I really enjoyed writing it – I hope you like it */

class myprog{ public static void main(String[] args) { // The following line will say hello System.out.println(“Hello!”); }}

Page 28: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Code Presentation Tips – 3

Comments should be brief and helpful.

No need to add comments which state the obvious.

Blank lines help to make the code readable – use them to separate each of the code’s tasks.

Page 29: Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 1: Introduction: Program Structure & Java Syntax

Coming up in Session 2...

Using variables to store information.

An introduction to the range of data types available.