11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank...
-
Upload
nathan-mcbride -
Category
Documents
-
view
214 -
download
0
Transcript of 11 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank...
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
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
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
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
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
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
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
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
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
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
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
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
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
BlueJ Console Window
11 January 2013 Birkbeck College, U. London 14
See http://en.wikipedia.org/wiki/File:BlueJ_screenshot.png
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
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
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
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
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
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
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
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
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
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