11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank...

24
11 January 2013 Birkbeck College, U. London 1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems [email protected] Spring 2013 Week 1: First Program

Transcript of 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank...

Page 1: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

11 January 2013 Birkbeck College, U. London 1

Introduction to Programming

Lecturer: Steve Maybank

Department of Computer Science and Information Systems

[email protected] 2013

Week 1: First Program

Page 2: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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.

11 January 2013 Birkbeck College, U. London 2

Page 3: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 3

Page 4: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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.

11 January 2013 Birkbeck College, U. London 4

Page 5: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 5

Page 6: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 6

Page 7: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 7

CPUMainmemory

Page 8: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 8

Page 9: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 9

Page 10: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 10

Page 11: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 11

Page 12: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 12

Page 13: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

Integrated Development Environment

The IDE for Java is BlueJ BlueJ facilities for Java programs:

EditCompileRun

See practical session.

11 January 2013 Birkbeck College, U. London 13

Page 14: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

BlueJ Console Window

11 January 2013 Birkbeck College, U. London 14

See http://en.wikipedia.org/wiki/File:BlueJ_screenshot.png

Page 15: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 15

Page 16: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 16

Page 17: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 17

Page 18: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

My First Program

public class HelloPrinter{

public static void main(String[] args){

System.out.println(“Hello, World!”);}

}

11 January 2013 Birkbeck College, U. London 18

Page 19: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 19

Page 20: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 20

Page 21: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 21

Page 22: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 22

Page 23: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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.

11 January 2013 Birkbeck College, U. London 23

Page 24: 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.

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 2013 Birkbeck College, U. London 24