Presentation is loading. Please wait.

Presentation is loading. Please wait.

GEOG5990M Programming for Geographical Analysis: Core Skills

Similar presentations


Presentation on theme: "GEOG5990M Programming for Geographical Analysis: Core Skills"— Presentation transcript:

1 GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans Welcome to the course

2 This lecture Introduction Java: what and why How to program The course Coding part one: the class.

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

4 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. Learning to computer program is not easy. This course will probably take more effort than any other course you’ve done. If you’re on the course to learn a new skill, however, although it is hard, it will also be one of the most satisfying things you’ll have done. Almost anyone can program if they are dedicated to learning it, but it takes everyone a different amount of time before it ‘clicks’. Up until that time, it is really really horrible. After that time it is highly rewarding. Trust us, you can get there. Once you’re fully institutionalised, you’ll even find it fun.

5 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. Before all this, however, we’ll spend this session looking at the language we’re going to learn, Java, and why we’ve chosen it for this course.

6 This lecture What is Java? Java: what and why Introduction
How does it work? How we program The course

7 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. As you can see from the above example, Java looks like a mix of English and mathematical notation. When programmers talk about syntax, they mean the structured sequence of terms that is necessary in any given location to get a program to work. Computer languages have a very specific grammar that you absolutely can’t vary away from if you want the computer to understand you. Part of learning the language is learning this grammar.

8 Terminology Java Applications Java Applets
Standalone programs that run on your desktop. Java Applets Programs that are run in your web browser. More secure - can’t do certain things like writing to your hard disk. Most of what we’ll be doing will be building ‘applications’, that is, programs like Word or Excel, which run on their own on your computer. Applets are like little applications, but they’re trapped within a Web browser and can’t do certain things (like write to your hard drive) without explicit permission.

9 Terminology Javascript Java Beans
Developed by Netscape for creating interactive web pages; not covered in this course. Java Beans Bits of programs represented by jigsaw-like graphics you can stick together to make larger applications. Again, we won’t cover these much. There are a number of things you might hear about that this course won’t cover. Javascript looks and feels like a sub-set of Java, but is largely different. If you can program in Java you shouldn’t have any problems programming in Javascript, which is one of the reasons we teach it. Although we won’t teach you much about Java Beans, which are not especially relevant when you are learning Java, we will teach you good coding practice which is appropriate for building such Beans later.

10 Why Java? It’s useful and easy to use It’s the best language for the Internet It’s Operating System (OS) independent It’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? It’s trendy, therefore it’s sought by employers. This is especially true of anyone who runs applications over a network – so banks and firms with intranet distributed data are especially looking for people. Java’s easy to knock stuff up in and everything you need is freely available on the web, so it’s good in a corner. It also has great documentation, which makes learning easy on the brain. It was written specifically for the internet. Secure networking was built in from the start and it can run in most web browsers. Both Microsoft and Oracle (the makers of Java) are pushing the net as the future of computers, with applications and data being stored and running from anywhere. Java is the only real language at present that can make this idea reality. Most programs have to be rewritten depending on whether it’s going to run on a Windows machine, or a Mac, or a UNIX box. Java was written specifically so it would run on everything equally well. (If you haven’t come across UNIX yet, it’s a rather complex but very stable OS. Because it’s very stable it’s used to run most of the Internet). Java has the advantage of being an advanced language, but easy to use. It’s an ‘advanced’ language because you need to understand all the code and what it’s doing (unlike, for example, Visual Basic, where some of the complexity is hidden away), and because it’s not simplified just for teaching purposes or because of its historical uses (like, for example, PASCAL or FORTRAN). However, it’s much simpler than the other main advanced language ‘C++’, because it was written by C programmers who used C++ every day and hated it. The two languages are very similar, but Java is for people who don’t want to spend 90% of their time weeping into their hands whimpering. GIS manufacturers are increasingly aware that people don’t want to write programs just for a single GIS package – they want to integrate GIS into existing applications and environments. Because of this they have moved away from their own programming languages like ArcInfo’s AML (Arc Macro Language) and lett users program in languages like Java within their GIS. At our last Alumni conference almost all Master’s students who went to work in industry said we should be teaching programming, and 9 out of 10 said this was the language to teach.

11 This lecture How does it work? Java: what and why Introduction
What is Java? How does it work? How we program The course

12 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 ‘ ’. Early 1950’s ‘Second Generation Languages’ – used simple codes to represent operations, like ‘02A02’ for ‘2 add 2’. Binary coding represents all operations the computer should do, and the numbers and letters it needs for communicating with people, in the forms of ones and zeros., for example may mean ‘add 2 and 2’. Ultimately this is how all computers see stuff. Why would we want to know this? Because sometime we want to alter numbers or images using the binary code stored in the computer directly. For example, if we want to reverse all the colours in an image, the easiest way we can do this in in binary. Second generation languages are sometimes called ‘Machine Code’ or ‘Assembler’. Second generation and higher languages need changing back into binary so the machine can read it. This is done by a compiler or interpreter.

13 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 complier is used to convert human language code into binary code once. The binary code can then be run as many times as you like.

14 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’. Third generation languages started with FORTRAN, which is still used today, mainly for scientific calculations. It’s very good at fast mathematics, but very bad for almost anything else. Most people use it because they’ve nicked old code off someone else and can’t be bothered rewriting it. Other popular third generation languages are C++, FORTRAN, and Python. To a certain extent Visual Basic and Java Beans are moving in a Fourth generation direction. ArcGIS 9.0 introduced a fourth generation programming method for Arc.

15 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 If it’s not obvious why you wouldn’t want to write all the instructions in one file, Windows XP has 40 Million lines of code in it ( If you want an example of an Object, then look at any Window’s program menu – each item on the menu may just look like text, but it’s actually an Object which does some specific job for you. Plainly menu elements do a job. But they also store information, e.g. the text to display on themselves for a given language. This is something we’ll come back to next week. Don’t worry about the details of Object Orientated Programming now, we’ll go into it in more detail next lecture.

16 Example Menu fileMenu = new Menu ("File"); MenuItem saveWeb = new MenuItem ("Save as Web Page…"); fileMenu.add(saveWeb); MenuListener a = new MenuListener(saveWeb); So, here’s the code that would make the example on the previous slide in Java. Note how little code it takes, and how (with the exception of the MenuListener) you can kind of understand what it does without really knowing too much about how it does it. These are two key reasons people like Object Orientated programming languages.

17 Java 1.1 was released early 1997
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 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. If you download one of the latest browsers, chances are it will be running the Java Plugin, and is therefore able to cope with Java 2, however, you should remember that this isn’t true for everyone. Java 1.7 now here, and there were some big changes at 1.5, but we’ll cover these towards the end of the course.

18 This lecture How to program What is Java? How does it work? The course
Introduction Java: what and why What is Java? How does it work? How to program The course

19 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. We’ll use JDK1.7 - it’s backwards compatible if you just use the Java 1.1 bits of it. JDK1.7 is part of the Java 7 Platform Standard Edition, which is what you want to look for if you need to download it from the Java website. The JDK includes the JVM, the files you need to write the Java, some files for doing extra stuff, like making fancy windows, the source code for most of the JDK, and some applications. These include the complier ‘javac’ and a viewer for applets, as well as a program ‘javadoc’ for making neat documentation for your programs. We’ll cover this in later lectures and practicals.

20 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 file containing the machine readable code of a fully complied language is sometimes called the ‘binary’ or ‘exe’. Usually OSs know how to run such files – for example, this is what happens when you double click a file in a Windows folder to open an application. Java is unusual in that it is complied AND interpreted. Most programs written in interpreted languages (like Visual Basic for Applications) are converted from human to machine readable code every time they’re run. The java complier and interpreter are programs you can run. The complier is called ‘javac’ and the interpreter is (confusingly) called ‘java’. You’ll see how to do this in the practicals.

21 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. The JVM was mainly designed to run platform independent code – but as a useful side effect it also increases the security of Java programs and makes them easy to write. Because programmers don’t have access to the real OS it’s hard to write very destructive programs in Java, either maliciously or accidentally. The JVM handles all the tricky chatting to the OS which makes so many virus writers happy and puts so many C++ programmers in rehab.

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

23 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. There are two things to remember: Programming is the art of telling something very very stupid how to do things. Computers have all the intelligence of an earthworm – you are smarter than them – don’t forget this. You just have to learn to talk down to them in terms they understand. When you start programming your programs won’t work. You’ll probably have hundreds of errors. This isn’t because you’re a bad programmer and can’t program. This is because this is what programming is: it’s 50% writing code, and 50% fixing problems. All that happens as you become a better coder is that the easy problems get fixed as you write the code, and harder ones appear to take their place. You know you’re a coder the moment that you’re institutionalised to the point you enjoy solving these problems. In the meantime, remember that problems arise because you’re a human being, not a computer, and be glad of it.

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

25 How to calculate the mean of three numbers
Get 3 numbers. Sum the numbers. Divide the sum by 3. Print the result. As you can see, algorithms are all about getting the order of things right. This is much of the skill involved in computer programming. For example, if you want to mail someone a present, you have to buy the present and have a box ready before you send it. This may sound dumb to the point of pointlessness, but when people come to write computer programs you’d be surprised how many people try and send empty boxes, or send the box before they’ve bought the present. When you’re first starting out, algorithms are very important for helping with this problem. Later on, you’ll find that other people publish algorithms that describe how to solve particular, and very complex, problems. You can see a list of the top ten here… Don’t worry – we won’t be using any of them! Here’s your first Java code – it implements this algorithm. See if you can understand it, and note how the algorithm is written into the code. It’s usual to write down the algorithm as comments (which the computer doesn’t run) and build the code around it. /** * Program to calculate the mean of three numbers Andy Evans * The next two lines are important for the code to run - we'll look at these * in the practical **/ class Calculate { public void main (String args[]) { // Get 3 numbers. // "ints" are integer numbers. int numberOne = 7; int numberTwo = 23; int numberThree = 42; // Sum the numbers. int sum = numberOne + numberTwo + numberThree; // Divide the sum by 3. // "doubles" are numbers with decimal places. double result = sum/3; // Print the result. System.out.println(result); }

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

27 How not to code Don’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.

28 Collaboration 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 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.

29 This lecture The course How to program Introduction Java: what and why
What is Java? How does it work? How to program The course

30 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. You can find out more about the course on the course website.

31 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%). We’ll start discussing the projects in 9. The type of project is reasonably flexible, if you have some ideas of your own.

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

33 Information 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. We’ll visit the site in Practical One.

34 Other info Practical: Today :00: Masters lab. Running our first program. My office is along the corridor before the Masters lab.

35 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 computing Modelling (focussing on Agent-Based Models) Opportunity to learn Python, Arduino programming, Javascript, Mobile programming, etc. Anyone interested in this course should talk to Andy Evans. It may be possible to take this course without first completing this foundation course if you already have Java experience.

36 This lecture The course Coding part one: the class. How to program
Introduction Java: what and why How to program The course Coding part one: the class.

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

38 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. Note that everything in Java is case sensitive. FilmQuiz is not the same as filmQuiz , or filmquiz . In addition, neither the class nor the file name can have any spaces. Note also that FilmQuiz.class is the FilmQuiz class in machine readable form. The mixing of words with an uppercase letter to denote the change in words is sometimes called “CamelCase”…

39 Our first Class Starting with a blank file, we add the following code… public class HelloWorld { } And save it as HelloWorld.java Won’t do anything – needs something between its brackets { }. The “public” bit isn’t always needed, but helps. Keywords Theoretically you can now use this class – but it won’t do anything. The words “public” and “class” are Keywords – that is, you can’t use them elsewhere – they are reserved for this use. Keywords are specially coloured in Notepad++. The “public” keyword sets the access of the file, that is, which bits of the computer can use it. We’ll cover this in more detail later. Theoretically you can get away without it here, but it helps to get use to putting it in.

40 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. } ‘Nesting’ is when one (or more) blocks is entirely inside another. Note how we indent code that’s nested so we can see where the nests start and end. The first thing we need is a ‘main’ block.

41 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. You don’t have to pass all the class files to the interpreter, just the first. The interpreter will find the rest if they’re in the same directory / package. Your first class may simply call another to get everything started. We’ll deal with how in a later section. If you didn’t have the main block you’d never get started.

42 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’. It’s essential you have all the bits for a ‘main’ block, but don’t worry about what they mean for the moment. This is one of the few things we’ll have to skip over for the moment and come back to.

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

44 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"). Don’t worry about the System.out.println - it uses a class directly to print to the screen. You don’t often use a class directly like this, as we’ll see when we look at Objects. We’ll cover when this happens later in the course. ‘Cheesy’ because everyone does it. As will we, this Friday.

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

46 Practical Next Lecture Running the compiler and interpreter.
“Hello You!” Next Lecture We’ll learn more about Object Orientated code. We start filling in the gaps. Next lecture we’ll start by looking at the language basics, and we’ll look more closely at what it means to be “Object Orientated”. Congratulations – your road to geekdom has begun.


Download ppt "GEOG5990M Programming for Geographical Analysis: Core Skills"

Similar presentations


Ads by Google