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

GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

Embed Size (px)

Citation preview

GEOG5990MProgramming for Geographical Analysis:

Core SkillsDr Andy Evans

This lecture

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

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.

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.

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.

This lecture

Introduction

Java: what and why

What is Java?How does it work?

How we programThe course

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.

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.

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.

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?

This lecture

Introduction

Java: what and whyWhat is Java?

How does it work?How we programThe course

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’.

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.

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’.

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

Example

Menu fileMenu = new Menu ("File");

MenuItem saveWeb =

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

fileMenu.add(saveWeb);

MenuListener a = new MenuListener(saveWeb);

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.

This lecture

Introduction

Java: what and why

What is Java?How does it work?

How to programThe course

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.

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.

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.

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++.

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.

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.

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.

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.

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.

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.

This lecture

IntroductionJava: what and why

What is Java?How does it work?

How to program

The course

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.

Assessment

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

You need to pass these, but they also give you the basic code you need for...

Two assessed projects (2 x 50%).

Assessment

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

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.

Other info

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

My office is along the corridor next to the lecture theatre.

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.

This lecture

IntroductionJava: what and why

How to program

The courseCoding part one: the class.

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.

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.

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

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.}

}

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.

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’.

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.

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").

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.

Practical

We’ll learn more about Object Orientated code.

We start filling in the gaps.

Next Lecture

Running the compiler and interpreter.

“Hello You!”