Title: GEOG5990M
1- GEOG5990M
- Programming for Geographical Analysis Core
Skills - Dr Andy Evans
2This lecture
- Introduction
- Java what and why
- How to program
- The course
- Coding part one the class.
3Whats 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.
4What doesnt kill you makes you stronger
- You will learn the patience of the Buddha.
- You will learn that you are not a machine, youre
a real live human boy/girl.
5Java
- This course will centre on learning the Java
programming language. - This section will look at what Java is, and why
weve chosen it for this course.
6This lecture
- Introduction
- Java what and why
- What is Java?
- How does it work?
- How we program
- The course
7What 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 2piradius
- 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.
8Terminology
- Java Applications
- Standalone programs that run on your desktop.
- Java Applets
- Programs that are run in your web browser.
- More secure - cant do certain things like
writing to your hard disk.
9Terminology
- Javascript
- Developed by Netscape for creating interactive
web pages not covered in this course. - http//en.wikipedia.org/wiki/Javascript
- Java Beans
- Bits of programs represented by jigsaw-like
graphics you can stick together to make larger
applications. Again, we wont cover these much.
10Why Java?
- Its useful and easy to use
- Its the best language for the Internet
- Its Operating System (OS) independent
- Its 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?
11This lecture
- Introduction
- Java what and why
- What is Java?
- How does it work?
- How we program
- The course
12A brief history of programming
- 1930s Alan Turing first thought about
programmable machines that had flexible uses. - 1940s First properly flexible computers
programmed in binary First Generation
Languages, for example 010110100110. - Early 1950s Second Generation Languages used
simple codes to represent operations, like
02A02 for 2 add 2.
13Converting 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.
14A brief history of programming cont...
- Mid 1950s 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 theyre done. - Java is a Third generation Object Orientated
Language.
15Object 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 theyre done, they
pass the data on to another Object. Each Object
has its own text file associated with it.
16Example
- Menu fileMenu new Menu ("File")
- MenuItem saveWeb
- new MenuItem ("Save as Web Page")
- fileMenu.add(saveWeb)
- MenuListener a new MenuListener(saveWeb)
17Brief History of Java
- Java was first released in 1995 as a platform
independent and nicer version of C. - Java 1.1 was released early 1997
- Faster. 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.
18This lecture
- Introduction
- Java what and why
- What is Java?
- How does it work?
- How to program
- The course
19What 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. - Its free youll see where to download it in the
practical.
20The 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.
21The 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.
22What 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.
23Debugging
- 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 isnt because you cant program this is
programming. - Programming is 50 writing code and 50 fixing
it.
24Before 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,
its a bit like a recipe.
25How to calculate the mean of three numbers
- Get 3 numbers.
- Sum the numbers.
- Divide the sum by 3.
- Print the result.
26How to code
- First, write the algorithm into your text file as
comments (these are lines starting // that the
computer doesnt 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 wont end up with 100
errors at once and youll know where the errors
are. - Think how can I test this is working properly?
as each line goes in.
27How not to code
- Dont just start writing without thinking through
how the program is roughly structured. Compile
regularly. - Dont ignore errors they wont go away, and
theyll just make everything wrong. If you get an
error you cant 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 cant
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.
28Collaboration and the Net
- No one codes from scratch it would be
counterproductive to ignore the mass of
experience available to draw on. - The first thing to do (if youre 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. - Its not called a coding community for nothing.
29This lecture
- Introduction
- Java what and why
- What is Java?
- How does it work?
- How to program
- The course
30The course
- The first few lectures will look at the basics of
the language. - Well then look at reading and writing files and
making windows applications. - Weeks 1-6 Core language.
- Weeks 7-11 Object packages.
31Assessment
- 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).
32Assessment
- The final projects will solve some geographical
problem - Disease spread
- Strategic modelling
- Government coverups
- Titanic icebergs etc.etc.
33Information
- On the VLE
- All the lectures, with extensive notes, and all
the practicals. - Extra practice pieces.
- FAQs
- The course outline.
- Recommended text books.
- Useful links mentioned in the lectures.
34Other info
- Practical
- Today 1.00 - 300 Masters lab.
- Running our first program.
- My office is along the corridor before the
Masters lab.
35Programming 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 computing
- Modelling (focussing on Agent-Based Models)
- Opportunity to learn Python, Arduino
programming, Javascript, Mobile programming, etc.
36This lecture
- Introduction
- Java what and why
- How to program
- The course
- Coding part one the class.
37Review
- 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.
38Classes
- 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.
39Our first Class
- Starting with a blank file, we add the following
code -
- public class HelloWorld
-
- And save it as HelloWorld.java
- Wont do anything needs something between its
brackets . - The public bit isnt always needed, but helps.
Keywords
40Blocks
- 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.
-
-
41The 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 wouldnt
know where to start - so you must have one to
start the interpreter. - Only one class in a full program need have one.
42- public class HelloWorld
- public static void main (String args)
-
-
- Dont worry about the details (static etc.)
well cover these at a later point. - Any Class with a main block can be run.
43Thats the tricky bit done
- Weve 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.
44Cheesy 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").
45Review
- We define classes which have code in them to do
stuff. - Blocks are section of code that do stuff. Lines
that dont 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.
46Practical
- Running the compiler and interpreter.
- Hello You!
Next Lecture
- Well learn more about Object Orientated code.
- We start filling in the gaps.