11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems Spring 2013 Week 1: First Program
Module Information Time: on Fridays in the spring term. A-K: lectures to lab sessions to L-Z: lab sessions to lectures to Week 11: in lab test. 11 January 2013Birkbeck College, U. London2
Assessment In Lab test: 30% Two hour examination in summer 2013: 70% The mock examination and the mock in Lab test do not contribute to the marks. 11 January 2013Birkbeck College, U. London3
Essential Text Book Cay Horstmann (2013) Java for Everyone, Wiley, 2 nd edition. This module draws on the first six chapters of JFE. The lab classes are based on exercises suggested in JFE. 11 January 2013Birkbeck College, U. London4
Syllabus First program Safe operation of computing equipment Variables and number types String types Arithmetic and Boolean operations If statement Loops Methods Arrays 11 January 2013Birkbeck College, U. London5
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. 11 January 2013Birkbeck College, U. London6
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. 11 January 2013Birkbeck College, U. London7 CPU Main memory
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. 11 January 2013Birkbeck College, U. London8
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. 11 January 2013Birkbeck College, U. London9
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. 11 January 2013Birkbeck College, U. London10
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. 11 January 2013Birkbeck College, U. London11
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. 11 January 2013Birkbeck College, U. London12
Integrated Development Environment The IDE for Java is BlueJ BlueJ facilities for Java programs: Edit Compile Run See practical session. 11 January 2013Birkbeck College, U. London13
BlueJ Console Window 11 January 2013Birkbeck College, U. London14 See
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. 11 January 2013Birkbeck College, U. London15
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 11 January 2013Birkbeck College, U. London16
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: a static method can be used without any associated object. 11 January 2013Birkbeck College, U. London17
My First Program public class HelloPrinter { public static void main(String[] args) { System.out.println(Hello, World!); } 11 January 2013Birkbeck College, U. London18
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. 11 January 2013Birkbeck College, U. London19
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. 11 January 2013Birkbeck College, U. London20
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. 11 January 2013Birkbeck College, U. London21
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. 11 January 2013Birkbeck College, U. London22
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 January 2013Birkbeck College, U. London23
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 * ** 11 January 2013Birkbeck College, U. London24