Lecture 1 Slides

Download Report

Transcript Lecture 1 Slides

Programming for Beginners
Lecture 1: Introduction: Program Structure & Java Syntax
Martin Nelson
Elizabeth FitzGerald
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
Course website

Course materials, exercises and useful web links can be
found at:
www.maths.nottingham.ac.uk/~pmzmn/GSTPFB.html
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
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!)
High Level Languages (3GL)

Examples:

Fortran

COBOL

Pascal

C

C++

C#

Java
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
Algorithms

Algorithm = how you go about solving a puzzle; your
solution to a task

Example: how do you make a cup of tea?
1.
2.
3.

Boil kettle
Put tea in cup
Pour boiling water
into cup
In programming, you should plan your algorithm before
you start coding
Tea-making program – 1
Is kettle full?
No
Fill kettle
Yes
Boil kettle
Put tea in cup
Pour boiling water into cup
Elements of tea-making program
Fill kettle
Boil kettle
Place kettle
under tap
Plug kettle into electrical
socket
Open tap
Switch kettle on
Wait
Wait
No
No
Is kettle full?
Yes
Close tap
Has kettle boiled?
Yes
Pour water into cup
Tea-making program – 2
Is kettle full?
No
Yes
Put tea in cup
Pour boiling water into cup
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
Compiled languages
Data storage
CPU/memory
Source code
Machine code
Compiler
Program execution
Interpreted languages
Data storage
Source code
CPU/memory
Interpreter
Machine code
Program execution
Compiled vs interpreted
Compiled
 Development more
cumbersome
Interpreted

Easy to develop
 Easy to distribute

Distribution of
interpreter required

Machine code
generated at
runtime
 Machine code
generated at
compile time
e.g. most high-level languages
e.g. BASIC, LISP, Perl
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)
How Java works
Data storage
Source code
myprogram.java
Bytecode
myprogram.class
CPU/memory
Compiler
Interpreter
Machine code
Program execution
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?
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
Some examples
Procedural

Fortran

COBOL
(old versions)

Pascal

C

Perl
Object-oriented (OO)
 COBOL (latest version)
 Delphi
 C++
 C#
 Java
 Visual Basic
 Perl
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)
On with the code!

Quick intro to using Java

Then YOU start to code your first Java program!
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!
'Hello world' program in Java
class myprog
{
public static void main(String[] args)
{
System.out.println(“Hello world!”);
}
}
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.
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
}
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!”);
}
}
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.
Coming up in Session 2...

Using variables to store information.

An introduction to the range of data types available.