geog5990m programming for geographical analysis: core skills dr andy evans

46
GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Upload: wynona

Post on 14-Jan-2016

42 views

Category:

Documents


0 download

DESCRIPTION

GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans. This lecture. Introduction Java: what and why How to program The course Coding part one: the class. What’s the big idea?. You will become a programmer. You will make Windows programs. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

GEOG5990MProgramming for Geographical Analysis:

Core SkillsDr Andy Evans

Page 2: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

This lecture

IntroductionJava: what and whyHow to programThe courseCoding part one: the class.

Page 3: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

What’s the big idea?

You will become a programmer.You will make Windows programs.You will solve complex problems.

You will be able to look down your nose at people who say they can ‘use a computer’ when all they do is word process.

We shall, in short, become Über-Geeks, finely honed Code Warriors, the Elite of Scientists. Desired by the rich, admired by the poor.

Page 4: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

What doesn’t kill you makes you stronger

You will learn the patience of the Buddha.

You will learn that you are not a machine, you’re a real live human boy/girl.

Page 5: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Java

This course will centre on learning the Java programming language.

This section will look at what Java is, and why we’ve chosen it for this course.

Page 6: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

This lecture

Introduction

Java: what and why

What is Java?How does it work?

How we programThe course

Page 7: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

What is Java?A language you write in simple text files that lets you program a computer to do stuff.With it, you can do anything a computer can do.

int radius = 10;

int answer = 2*pi*radius;

System.out.println(answer);

Code written in text files.The code has a specific syntax along with keywords that have specific meanings for the computer.One or more files work together to make a program.

Page 8: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Terminology

Java ApplicationsStandalone programs that run on your desktop.

Java AppletsPrograms that are run in your web browser.More secure - can’t do certain things like writing to your

hard disk.

Page 9: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Terminology

JavascriptDeveloped by Netscape for creating interactive web pages; not covered in this course.http://en.wikipedia.org/wiki/Javascript

Java BeansBits of programs represented by jigsaw-like graphics you can stick together to make larger applications. Again, we won’t cover these much.

Page 10: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Why Java?

It’s useful and easy to useIt’s the best language for the InternetIt’s Operating System (OS) independentIt’s a good programming language (maybe better than C++)Because learning Java makes it simple to learn other

languages.Knowing it will help you work with other programmers.

How does it do all this?

Page 11: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

This lecture

Introduction

Java: what and whyWhat is Java?

How does it work?How we programThe course

Page 12: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

A brief history of programming

1930’s Alan Turing first thought about programmable machines that had flexible uses.

1940’s First properly flexible computers – programmed in binary ‘First Generation Languages’, for example ‘010110100110’.

Early 1950’s ‘Second Generation Languages’ – used simple codes to represent operations, like ‘02A02’ for ‘2 add 2’.

Page 13: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Converting code to binary

A compiler is used to convert human language code into binary code once. The binary code can then be run as many times as you like.

An interpreter runs the source code directly, usually using its own binary code to complete the tasks asked for.

Page 14: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

A brief history of programming cont...

Mid 1950’s Third generation languages: in a human readable form, e.g. ‘2 add 2’.

Fourth generation languages try to let people describe what they want to do, without complicating things with how they’re done.

Java is a Third generation ‘Object Orientated Language’.

Page 15: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Object Orientated Languages

At first programs were written so all the information about what to do was held in one file, and all the data in another.Fast became a nightmare.

Object Orientated Languages build bits of code called ‘Objects’ which can hold data and do specific things with it. Once they’re done, they pass the data on to another Object. Each Object has its own text file associated with it.

Example Objects

Page 16: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Example

Menu fileMenu = new Menu ("File");

MenuItem saveWeb =

new MenuItem ("Save as Web Page…");

fileMenu.add(saveWeb);

MenuListener a = new MenuListener(saveWeb);

Page 17: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Brief History of Java

Java was first released in 1995 as a platform independent and ‘nicer’ version of C++.

Java 1.1 was released early 1997Faster. Database access. Networking improved.

Java 1.1 is the most basic version running in Web browsers.

Java 1.2 or “Java 2”Slightly fancier. Most web browsers have a plugin for it.

Java 1.5 / “Java 5” (now 1.8 or “Java 8”)A few additional bits you can turn on.

Java was built by Sun, but they were bought by Oracle.

Page 18: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

This lecture

Introduction

Java: what and why

What is Java?How does it work?

How to programThe course

Page 19: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

What do I need to write Java?

You need a text editor and the ‘Java Development Kit’ (JDK).Text editors come with most OSs.

The JDK contains a compiler, and an interpreter, plus some files to add extras to the core language and some additional applications to help with coding.

It’s free; you’ll see where to download it in the practical.

Page 20: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

The Interpreter

When you compile a Java program, it gets converted into an intermediate language called ‘Java bytecode’ that any java interpreter on any OS can understand. This is done once.

The interpreter does the final job of running the code. As it happens, this is sometimes just converting bits of it into platform dependant code that the interpreter sends to the OS. The interpretation is done each time the program is run.

The interpreter is part of the Java Virtual Machine: an piece of software any machine that wants to run Java needs to have.

Page 21: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

The Java Virtual Machine (JVM)

The Java Virtual Machine runs on top of the normal Operating System (OS) and pretends to be a whole new computer.

The Java language then runs inside the JVM, oblivious to the actual OS.

Java is two things: a high-level programming language and a platform – the environment that actually does the work.

Page 22: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

What we do

So, to run Java as a developer, we need to:Write the commands in a text file.Pass the text file to the compiler to get compiled.Pass the compiler results (bytecode files) to the JVM for running.

The good thing is that the compiler will check our code matches the Java syntax, and the JVM will report problems that arise as the code runs.

Because the JVM is protecting the OS, you are very unlikely to crash or destroy an OS with Java, unlike languages like C++.

Page 23: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Debugging

Both the compiler and interpreter can catch problems in your code.

Both will give you a description of what has gone wrong and where.

Your job is then to fix the issue.

This isn’t because you can’t program; this is programming.

Programming is 50% writing code and 50% fixing it.

Page 24: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Before you code: Algorithms

Programming is the art of describing how to do very complicated things to a very stupid computer in very simple steps.

The initial outline for the processing done is called an ‘algorithm’, it’s a bit like a recipe.

Page 25: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

How to calculate the mean of three numbers

1. Get 3 numbers.2. Sum the numbers.3. Divide the sum by 3.4. Print the result.

Page 26: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

How to code

First, write the algorithm into your text file as comments (these are lines starting // that the computer doesn’t process).

Next, pick a line to work up as code.

Write the code a line, or couple of lines, at a time. Compile and test the code every couple of lines. Baby steps mean you won’t end up with 100 errors at once and you’ll know where the errors are.

Think ‘how can I test this is working properly?’ as each line goes in.

Page 27: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

How not to codeDon’t just start writing without thinking through how the program is roughly structured. Compile regularly.

Don’t ignore errors – they won’t go away, and they’ll just make everything wrong. If you get an error you can’t see, try cutting back chunks of code until you have something simple that works, then add it back in, a line at a time.

That said, if you get very stuck and no help is immediately forthcoming, think about cutting out the problematic code and replacing it temporarily with something simpler. For example, if you can’t work out how to open a file and read in data, just write the data directly into the program and work on something clearer until you can find someone to help.

Page 28: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Collaboration and the NetNo one codes from scratch; it would be counterproductive to

ignore the mass of experience available to draw on.The first thing to do (if you’re not being assessed for the

work!) is to see if someone else has already done it and made their work available through an Open Source License.

Open Source Licenses make code freely available (in the sense of putting it out there for others to use) and freely available (in the sense of not costing anything). Different licenses have different requirements and protection. You must make sure you match the licensing requirements.

But also, there are plenty of good forums where people will post useful examples, or answer questions.

It’s not called a coding community for nothing.

Page 29: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

This lecture

IntroductionJava: what and why

What is Java?How does it work?

How to program

The course

Page 30: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

The course

The first few lectures will look at the basics of the language.

We’ll then look at reading and writing files and making windows applications.

Weeks 1-6: Core language.Weeks 7-11: Object packages.

Page 31: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Assessment

Eight of the practicals build up a framework for geographical analysis.

These give you the basic code you need for...

Two assessed projects (2 x 50%).

Page 32: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Assessment

The final projects will solve some geographical problem:Disease spread;Strategic modelling;Government coverups;Titanic icebergs etc.etc.

Page 33: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Information

On the VLE: All the lectures, with extensive notes, and all the

practicals.Extra practice pieces.FAQsThe course outline.Recommended text books.Useful links mentioned in the lectures.

Page 34: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Other info

Practical:Today 1.00 - 3:00: Masters lab.Running our first program.

My office is along the corridor before the Masters lab.

Page 35: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Programming for Geographical Information Analysis:

Advanced Skills. This course is the foundation for PGIA: Advanced Skills.

Programming ArcGIS.Database programming.Scientific programming libraries (e.g. R)Parallel computingModelling (focussing on Agent-Based Models)

Opportunity to learn Python, Arduino programming, Javascript, Mobile programming, etc.

Page 36: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

This lecture

IntroductionJava: what and why

How to program

The courseCoding part one: the class.

Page 37: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Review

Code is held in text files.

You compile the code to bytecode.

You run the code in the Java Virtual Machine.

The JVM interprets the code and converts it to binary each time it runs.

Page 38: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Classes

The basic unit of code is the Class.Classes are chunks of code that do stuff.

Usual each class has its own file.The class name and filename should be the same, and start with an uppercase letter.E.g., the FilmQuiz class would be in the FilmQuiz.java file

Case Sensitive. No spaces. CamelCase.

Page 39: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Our first Class

Starting with a blank file, we add the following code…

public class HelloWorld {

}

And save it as HelloWorld.javaWon’t do anything – needs something between its brackets { }.The “public” bit isn’t always needed, but helps.

Keywords

Page 40: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Blocks

The area between the brackets is known as a block.These are used to divide up Java code into areas that do different things.These can be nested inside each other. They usually start with a “declaration” of what they are (e.g. the class declaration “public class HelloWorld”).

public class HelloWorld {{

Code to do stuff would go here.}

}

Page 41: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

The starting class

When you pass a file to the interpreter, the interpreter looks in the class for a block called ‘main’. It knows all the instructions for how to start are in this block.

Without a main block the interpreter wouldn’t know where to start - so you must have one to start the interpreter.

Only one class in a full program need have one.

Page 42: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

public class HelloWorld {public static void main (String args[]) {}

}

Don’t worry about the details (‘static’ etc.) we’ll cover these at a later point.

Any Class with a main block can be ‘run’.

Page 43: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

That’s the tricky bit done

We’ve made our starting class, and told the interpreter where to start.

Now we just have to fill the main block with stuff to do.

This is where we really start the coding.

Page 44: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Cheesy example

class HelloWorld {

public static void main (String args[]) {System.out.println("Hello World");

}

}

‘Prints’ (writes) “Hello World” to the screen (the command line).Note that it uses a class called ‘System’ to print.Note that all lines not followed by a block end in a semi-colon

("statements").

Page 45: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Review

We define classes which have code in them to do stuff.

Blocks are section of code that do stuff. Lines that don’t end in blocks end in semi-colons.

The first class you pass to the interpreter must have a ‘main’ block, with instructions of what to do to first run the program.

Page 46: GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Practical

We’ll learn more about Object Orientated code.

We start filling in the gaps.

Next Lecture

Running the compiler and interpreter.

“Hello You!”