1 / 29

Programming for Beginners

Programming for Beginners. Lecture 1: Introduction: Program Structure & Java Syntax. Martin Nelson Elizabeth FitzGerald. Aims & objectives of this course. Write your own programmes in a procedural language

tracy
Download Presentation

Programming for Beginners

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. Programming for Beginners Lecture 1: Introduction: Program Structure & Java Syntax Martin Nelson Elizabeth FitzGerald

  2. Aims & objectives of this course • Write your own programmes in a procedural language • Understand basic concepts such as procedural logic, variables, flow control, input and output principles • Identify important programming concepts

  3. Course website • Course materials, exercises and useful web links can be found at: www.maths.nottingham.ac.uk/~pmzmn/GSTPFB.html

  4. Session 1 - aims & objectives • Appreciate how computer programs are constructed • Understand the differences between: • Compiled and interpreted languages • Procedural and object-oriented languages • Find out the basics of Java programming • Write a few simple programs making use of statements, comments and basic arithmetic

  5. Computer programmes • Set of instructions for the CPU • “Switch settings” • Machine language • Assembly language • Mnemonics representing binary code • Assembler • 3rd generation languages • “High level” languages • Compiled or interpreted (or both!)

  6. High Level Languages (3GL) • Examples: • Fortran • COBOL • Pascal • C • C++ • C# • Java

  7. Program construction • Computers are 'stupid' • Mathematical and logical instructions are executed very quickly and accurately • They obediently but stupidly do what you tell them to – not necessarily what you want them to! • Tiny errors in a program can cause major problems when it is executed • Careful planning is essential • Flow charts are a useful tool to represent program flow visually

  8. Algorithms • Algorithm = how you go about solving a puzzle; your solution to a task • Example: how do you make a cup of tea? • In programming, you should plan your algorithm before you start coding • Boil kettle • Put tea in cup • Pour boiling water into cup

  9. Tea-making program – 1 No Is kettle full? Fill kettle Yes Boil kettle Pour boiling water into cup Put tea in cup

  10. Fill kettle Boil kettle Place kettle under tap Plug kettle into electrical socket Open tap Switch kettle on Wait Wait No No Is kettle full? Has kettle boiled? Yes Yes Close tap Pour water into cup Elements of tea-making program

  11. Tea-making program – 2 No Is kettle full? Yes Pour boiling water into cup Put tea in cup

  12. Now for some jargon… (… but don’t worry about this too much  ) • Compiled vs interpreted languages Handy hint:If you’re not sure what these and other words mean, look at the ‘Glossary’ section of the course website

  13. Compiled languages Data storage Source code Machine code CPU/memory Compiler Program execution

  14. Interpreted languages Data storage Source code CPU/memory Interpreter Machine code Program execution

  15. Compiled vs interpreted • Compiled • Development more cumbersome • Easy to distribute • Machine code generated at compile timee.g. most high-level languages Interpreted • Easy to develop • Distribution of interpreter required • Machine code generated at runtimee.g. BASIC, LISP, Perl

  16. The Java model • Java is an object-oriented high level language • "write once, run anywhere" • It is both compiled and interpreted • Java source code has .java extension • Source code is compiled to produce a .class file (bytecode) – not human-readable • Bytecode is interpreted by the Java VM (virtual machine)

  17. How Java works Data storage Bytecode myprogram.class Source code myprogram.java CPU/memory Interpreter Compiler Machine code Program execution

  18. A bit more jargon… (… nearly finished though  ) • Procedural vs object-oriented programmes What is all this object-oriented stuff about? Does it matter? Does it mean anything?

  19. Procedural vs object-oriented Procedural • Early high-level languages • Contain functions (or sub-routines) written and used inside the main program • Cannot use external functions easily Object-oriented (OO) • Later high-level languages • Contain methods (or functions) and variables that can be written in main or external programs • Can call external functions or variables easily

  20. Some examples Procedural • Fortran • COBOL (old versions) • Pascal • C • Perl Object-oriented (OO) • COBOL (latest version) • Delphi • C++ • C# • Java • Visual Basic • Perl

  21. What’s Java all about then? • NOT the same as JavaScript • Java SDK consists of 2 components: • Java VM (Virtual Machine) • Java API (Application Programming Interface)+ accompanying documentation • If you alter your code you need to re-compile it before you can run the program • ONLY use Java version 2 and above (Java 1.2/Java 1.3/Java 1.4 etc)

  22. On with the code! • Quick intro to using Java • Then YOU start to code your first Java program!

  23. Writing your first Java program • Use a text editor to write the source code • Save it as a .java file • Compile it using Java SDK on Granby • You need to have a Granby account before you can start writing your programs – if you haven't you will need to apply for one as soon as possible!

  24. 'Hello world' program in Java class myprog { public static void main(String[] args) { System.out.println(“Hello world!”); } }

  25. Be careful what you type! • If you type something wrong, your code will either give you an error or won’t work properly. • If you get an error, check the following: • Capital letters are different to lower case – don’t mix them up. • { }, ( ) and [ ] all do different things – have you used the right one? • Some lines need to end with a semi-colon – miss them off and your code won’t work.

  26. Code Presentation Tips – 1 • Indent code inside curly braces • Every time you open a pair of curly braces, indent the next line by 1 tab or three/four spaces. • When you close braces, unindent. • You can then see straight away if you have a brace missing. class myprog { Some code Some more code { New braces, so indent again. } More code }

  27. Code Presentation Tips – 2 • You can add comments to your code to remind you (or someone else) how the code works. • Comments are ignored by the compiler. • On a single line, anything after // will be ignored. • Over many lines, anything between /* and */ will be ignored. /* This is my first java code * I really enjoyed writing it – I hope you like it */ class myprog { public static void main(String[] args) { // The following line will say hello System.out.println(“Hello!”); } }

  28. Code Presentation Tips – 3 • Comments should be brief and helpful. • No need to add comments which state the obvious. • Blank lines help to make the code readable – use them to separate each of the code’s tasks.

  29. Coming up in Session 2... • Using variables to store information. • An introduction to the range of data types available.

More Related