1 / 24

Introduction to Programming

Introduction to Programming. Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2014 Week 1: First Program. Module Information. Time: 18.00-21.00 on Fridays in the spring term. A-K: lectures 18.00 to 19.20

blaze
Download Presentation

Introduction to Programming

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. Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2014 Week 1: First Program Birkbeck College, U. London

  2. Module Information • Time: 18.00-21.00 on Fridays in the spring term. • A-K: lectures 18.00 to 19.20 lab sessions 19.40 to 21.00 • L-Z: lab sessions 18.00 to 19.20 lectures 19.40 to 21.00 • Week 11: in lab test. Birkbeck College, U. London

  3. Assessment • In Lab test: 30% • Two hour examination in summer 2014: 70% • The mock examination and the mock in Lab test do not contribute to the marks. Birkbeck College, U. London

  4. Essential Text Book • Cay Horstmann (2013) Java for Everyone, Wiley, 2nd edition. • This module draws on the first six chapters of JFE. • The lab classes are based on exercises suggested in JFE. Birkbeck College, U. London

  5. Syllabus • First program • Safe operation of computing equipment • Variables and number types • String types • Arithmetic and Boolean operations • If statement • Loops • Methods • Arrays Birkbeck College, U. London

  6. This Lecture • Based on Ch. 1 of JFE • Aim 1: provide background information on computing • Aim 2: provide enough information to write a first Java program. Birkbeck College, U. London

  7. Structure of a Computer • Central Processing Unit: executes instructions. • Main memory (= primary storage): stores programs and data ready for the CPU. • Bus: connects the CPU and the main memory. Main memory CPU Birkbeck College, U. London

  8. Problem • The CPU of a computer is designed to carry out a fixed set of simple instructions. • It takes too much time to write programs using these instructions. Birkbeck College, U. London

  9. Solution • Write programs in a special high level programming language. • Then use another program (a compiler) to convert each high level program into a list of instructions for the CPU. Birkbeck College, U. London

  10. Java • First version developed in 1991 by James Gosling and Patrick Naughton for use in consumer products, eg. set top boxes. • Java based browser presented in 1995 at the SunWorld exhibition. • A browser can run Java code (applets) obtained from anywhere on the Internet. Birkbeck College, U. London

  11. Advantages of Java • Portable: the same Java program can be run without change on Windows, UNIX, Linux or Macintosh. • Safe: a browser can run a Java program without endangering the PC running the browser. • Very large libraries for graphics, user interface, cryptography, networking, etc. Birkbeck College, U. London

  12. Java Portability • The Java compiler converts a Java program to Java byte code. • The Java byte code is executed by a Java Virtual Machine. • The JVM is not portable: different JVMs are needed for different platforms. • The Java program is portable given the right JVM. Birkbeck College, U. London

  13. Integrated Development Environment • Our IDE for Java is BlueJ • BlueJ facilities for Java programs: Edit Compile Run • See practical session. Birkbeck College, U. London

  14. BlueJ Console Window See http://en.wikipedia.org/wiki/File:BlueJ_screenshot.png Birkbeck College, U. London

  15. Classes, Objects and Methods • A class describes a set of objects with the same behaviour. • An object consists of data, together with methods which act on the data in that object. • A method is a sequence of instructions with a name. Birkbeck College, U. London

  16. The String Class • Objects (string literals): “Hello”, “World”. • Methods: the object “Hello” contains methods length() and substring() which act on the data in “Hello”: “Hello”.length(); // 5 “Hello”.substring(0, 2); // “He” Birkbeck College, U. London

  17. Java Programs • Java programs consist of classes. • When a Java program runs, objects are created and the methods act on the data in the objects. • Exception: static methods can be defined on their own, i.e. not as part of an object. Birkbeck College, U. London

  18. My First Program public class HelloPrinter { public static void main(String[] args) { System.out.println(“Hello, World!”); } } Birkbeck College, U. London

  19. Commentary 1 • MFP contains a single class: HelloPrinter • The class has no associated objects. • The class contains one method, main(), which can be invoked without first creating any object. Birkbeck College, U. London

  20. Commentary 2 • public class: HelloPrinter and main() are available to any user • static: main() can be invoked without any associated object. • void: no value is returned by main(). • public, class, static, void are reserved words. Birkbeck College, U. London

  21. System.out.println(“Hello”) • System.out is an object • println() is a method which is applied to data in System.out. • The string “Hello” is a parameter for the method println(). • Note how the details of System.out are hidden from the user. This is an example of encapsulation. Birkbeck College, U. London

  22. Java Syntax • Braces (brackets) occur in matching pairs, {…}. • Every statement must end in a semicolon. • Upper case and lower case are distinguished. • Every Java application must have a main method. • Reserved words cannot be used for other purposes. • A method can have any number of parameters, including none. Birkbeck College, U. London

  23. String Concatenation • “Hello, ”+”World!” produces “Hello, World!” • “Hello”+7 produces “Hello7” (The number 7 is converted to a string.) • Similarly, 7+”Hello” produces “7Hello” • 7+7 produces 14. Birkbeck College, U. London

  24. Escape Sequences • On printing “He said \”Hello\”” the result is He said “Hello” The pair \” is an escape sequence. • The escape sequence \n denotes new line, for example System.out.print(“*\n**\n”); produces * ** Birkbeck College, U. London

More Related