1 / 46

GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

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.

wynona
Download Presentation

GEOG5990M Programming for Geographical Analysis: Core Skills Dr Andy Evans

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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.

  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.

  6. This lecture Introduction Java: what and why What is Java? 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 syntaxalong with keywords that have specific meanings for the computer. One or more files work together to make a program.

  8. Terminology Java Applications 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.

  9. Terminology 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 won’t cover these much.

  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 of all this, GIS manufacturers are using it more and more. Knowing it will help you work with other programmers. How does it do all this?

  11. This lecture Introduction Java: what and why 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 ‘010110100110’. Early 1950’s ‘Second Generation Languages’ – used simple codes to represent operations, like ‘02A02’ for ‘2 add 2’.

  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.

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

  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

  16. Example Menu fileMenu = new Menu ("File"); MenuItem saveWeb = new MenuItem ("Save as Web Page…"); fileMenu.add(saveWeb); MenuListener a = new MenuListener(saveWeb);

  17. 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.7 or “Java 7”) A few additional bits you can turn on. Java was built by Sun, but they were bought by Oracle.

  18. This lecture 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.

  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.

  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.

  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.

  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.

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

  31. 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%).

  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.

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

  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)

  36. This lecture 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.

  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

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

  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.

  42. public class HelloWorld { public static voidmain(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’.

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

  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 Running the compiler and interpreter. “Hello You!” We’ll learn more about Object Orientated code. We start filling in the gaps. Next Lecture

More Related