Transcript GP1

Introductory Programming (GP)
Spring 2006
Lecture 1 – Welcome!
We start at 16:00
Slides are available from the course home page
Feel free to print them now
www.itu.dk/courses/GP/F2006
Martin Lillholm
GP1, Martin Lillholm
1
Language ...
This course is taught in English
• Using an English book and
• supplementary notes in English
Do, however, feel free to ask questions in Danish
• We’ll translate them and
• answer in English
GP1, Martin Lillholm
2
Who are we ?
Lecturer and course responsible
• Associate Professor Thomas Hildebrandt
• Office: 4C12
• e-mail: [email protected]
Lecturer
• Assistant Professor Martin Lillholm
• Office: 4D16,
• e-mail: [email protected]
Teaching assistants:
• Mads Troest,
• Priyadarsini Seetharaman
• Jesper Mouritzen
GP1, Martin Lillholm
3
Today's Agenda
• Motivation, prerequisites, and course contents
• Practical information
– Lectures, lab work/exercises, mandatory assignments,
home page, …
• Computers
– Hardware, software, networks
– Programming
– Tasks, algorithms, data structures
– Programming languages, syntax and semantics
– Program development
• Java
– On Java and Java programs
– Hello.java, HelloGUI.java og HelloApplet.java
GP1, Martin Lillholm
4
Motivation
Why would I want to learn how to program ?
• Prerequisite for most technical courses on the ITU
• Solid basis for understanding possibilities and indeed
limitations of modern information technology (IT)
• Even though you may not end having a technical
job/educational profile
• There’re still quite a good job possibilities for skilled
programmers
• Structured analysis
Why Java ?
• Solid, modern high level object oriented programming
language.
• Well suited for graphical and internet applications
• Not too bad as a first programming language
• You’ll not only learn Java … although it may seem like it 
GP1, Martin Lillholm
5
Prerequisites
• User level computer skills
• e-mail, browsers (e.g. Outlook and Internet Explorer)
• Some word processing (e.g. Word)
• Files, directories, ..
• Familiarity with ITU computer systems corresponding to the
introduction given by Sysadm.
• Don’t despair …
• Are you already a (skilled) programmer?
(forces course completion, shopping-period)
GP1, Martin Lillholm
6
GP contents (1)
Three tracks:
Programming
Object oriented
paradigm
GUIs
(Graphical User
Interfaces)
GP1, Martin Lillholm
7
GP contents (2)
• Computer systems basics
• The programming process:
Design, algorithms, data structures, programming, testing
• Variables, identifiers
• Flow of control (if-then-else, for, while, do...)
• Data types (int, float, double, classes, ...)
• Algorithms (searching and sorting)
• Object oriented programming 101 (classes, objects,
abstraction, encapsulation, inheritance, and polymorphism)
• Exceptions
• Graphical User Interfaces (GUIs)
• Applets (program that can run in a browser)
GP1, Martin Lillholm
8
Lectures
• 12 Mondays during spring 2006 from 13:00–16:00 in Aud 2
– First time today, Monday January 30.
– Mondays until and including April 24
(Excluding Easter break – April 17.)
– All in all approx. 36 hours –
First half by me and second half by Thomas
• Lectures will present main points from the curriculum
– Read before lectures
– And again afterwards …
• ALL questions are welcome – you’re here to learn!
• Breaks … probably 15 minutes per lecture hour … up to you
GP1, Martin Lillholm
9
Lab work/exercises
• 11 Mondays in spring 2005 from 9-12 or from 16-19
In one of the following rooms: (4A54, 4A56, 4A58)
– Same day as the lectures – not today though.
– All in all 33 hours
– Additional help before assignment deadline ??
• Exercises, programming, mandatory assignments:
– Assignments sheets will be made public on Fridays
– Teachings assistants (TAs) – use them, ask questions!
– Computers with Java, editors, IDEs and Word etc.
available on the ITU and in the lab rooms
– Printers
• Distribution in lab classes will be made public during this
week.
• How do I change class ?
GP1, Martin Lillholm
10
Mandatory Assignments
10 mandatory assignments will be given.
Evaluated as either pass or not pass.
Can (should) be solved in groups of two.
At least 8 of 10 assignments must get a pass to qualify for the final exam.
Assignment must be handed in on time!
Don’t cheat 
Skill training; you do not learn to program by reading a book!
No mandatory assignment this first week.
Made public on Fridays – should be handed in the following Friday.
Feedback at lab classes.
GP1, Martin Lillholm
11
Expected efforts …
• Read before and after lectures
• Solve assignments both mandatory and voluntary ones!
• Write programs like mad ...
• All in all at least 77 hours ... That is at least 6.5 hours per
week beyond lectures and lab classes.
• Access to ITU and computers 24x7
GP1, Martin Lillholm
12
Final Exam
• Monday den June 12 2006
–
–
–
–
Four hours written exam
All written but non-electronic aids are allowed
Evaluated using the Danish 13-scale …
Examples of old exam sets can be found on the course
home page
• “Trial-exam” at the end of the semester. One of the
mandatory assignments
GP1, Martin Lillholm
13
Home page and curriculum
http://www.itu.dk/courses/GP/F2006/index.html
•
•
•
•
•
Primary source of information
Curriculum, lectures
Assignment sheets
Additional materials
Mandatory assignment status
Curriculum:
• Java Software Solutions, Foundations of Program Design
John Lewis og William Loftus, ISBN: 0321312465
• 2 notes by Peter Sestoft (see home page)
• More notes if needed
GP1, Martin Lillholm
14
GP and my computer …
Do I need a computer to take GP?
• No is the short answer
• Computers with Java, editors, IDE’s and Word available at the
ITU.
• Access ITU 24x7
Is it an advantage to have my own computer?
• Really depends on your working habits …
How can I use my own computer for GP?
• All programs you need is available for download – all are free
• Some of them can be found on the CD that comes with the
book
• Otherwise check course home page
GP1, Martin Lillholm
15
Is GP a difficult course ... ?
GP1, Martin Lillholm
16
4-weeks projects
• 4 weeks after this course
• Topic of your choice
• Typically inspired by one or more
the courses you’ve followed
12
4
7,5
E
C
T
S
GP
7,5
E
C
T
S
?
7,5
E
C
T
S
?
7,5 ECTS
• Find groups and think of a topic!
• Intro lecture later this semester
• The sooner we/you agree on topic the better your project will
be 
GP1, Martin Lillholm
17
What do I do ?
Observed world (3D)
Girl sitting on grass in
front of building.
Tanja?
ITU Ørestaden
Inference (machine)
• Abstraction
• Prior knowledge (models)
GP1, Martin Lillholm
18
Computer Vision / Image Analysis
GP1, Martin Lillholm
19
What is a computer anyway?
Hard disc
Hardware :
• chips
• hard discs
• keyboard
• monitor
• memory
• CPU (Central Processing Unit)
• ...
Software:
• Operating system:
Windows *, Linux, Unix, Mac OS, ...
• Applications:
Word, Notepad, Matlab, browsers, ...
Computers:
PCs, Laptops, mobile phones, dish
washers, cars ...
GP1, Martin Lillholm
20
A very simple computer
”Machine language”
Monitor
External “memory”
CPU
Keyboard
Harddisk
Internal
memory
Programmer og data i filer
Floppy disc
Input og output
GP1, Martin Lillholm
21
What is a computer program anyway?
• A program is a collection of internal commands
(machine code) for a computer
• Internal commands can yield external and visible results
• E.g. calculation and displaying of a graph, . .
• The execution of a program is completely automatic
• A program can solve a lot of problem of a specific kind but …
• Programs are stupid … they can’t improvise, adapt, or
understand … however …
• Computers are really fast and sometimes programs come
across as being smart … it’s all an illusion 
GP1, Martin Lillholm
22
How does a computer execute a program?
• The program is read into memory from disc or some other
storage media
• The first instruction is fetched from memory to the CPU
• The CPU executes that instruction
• The CPU perhaps saves the result of the calculation in
memory or on the hard drive
• The CPU may write the result of the calculation on the
monitor
• The CPU may receive some input from the user
• And then the next instruction …
Monitor
Keyboard
CPU
Hard disc
Internal
memory
Floppy disc
GP1, Martin Lillholm
23
Networks
• A network connects two or more computers or units (printers,
scanners, ...)
• Today’s standard have almost all computers connected to
some kind of network
• Clients
– Thin clients (dependent on a server)
– “Thick” clients (independent computer)
• Servers
– File servers (h:)
– Web servers
– Database servers
– Print servers
– Mail servers
– …
GP1, Martin Lillholm
24
Local Area Network (LAN)
• A LAN typically connects computers
in the same building or area.
Wireless
(typically 11+Mbit)
Hubs, switches, …
LAN
Clients
Typically 100+Mbit
Back bone
(Gbit or fiber)
Server
(e.g. file server H:)
GP1, Martin Lillholm
25
Wide-Area Networks (WANs)
• A WAN typically connects two or more LANs.
The best known one being the Internet ...
A network of networks (internetworking)
• The basic internet is just connections between
a whole lot of networks and a simple
communication protocol …
LAN
LAN
GP1, Martin Lillholm
26
WWW, URL, HTML, ...
• The internet is used for communication, exchange of services
and information: e-mail, images, sound, text, number or in
short data.
• Wasn’t always as easy a today: ftp, telnet, nn, ...
• World Wide Web (WWW) is a system that uses the internet to
ease the communication of especially text, images, and
sound.
• Hypertext and hypermedia ... browsers
(Mosaic, Netscape, Internet Explorer, Mozilla, ...)
• HTML (HyperText Markup Language) is used to write pages for
the WWW – “home pages”.
GP1, Martin Lillholm
27
More HTML ...
• Text and media formatting language ... text (ASCII)
documents (.html, .htm)
<HTML>
<HEAD>
...
</HEAD>
<BODY>
...
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>Our first HTML document</TITLE>
</HEAD>
<BODY>
Here’s the beginning of the main text.
<H1>Here’s a heading</H2>
More main text and an
<IMG SRC="C:\home\figs\746.jpg">image</A>
Start tag
End tag
<P>and now a new paragraph with a
<A HREF =
"http://www.itu.dk/courses/GP/F2006/">link</A>
</BODY>
URL
</HTML>
(Uniform Resource Locator)
Reference or link to text, images, sound ...
GP1, Martin Lillholm
28
A few words on files
• Text files
– ASCII (typically)
– Extensions - .txt .html .htm . Java
– Conventions ~ content
• Binary filer
– .zip, .doc, .mp3, .wav
GP1, Martin Lillholm
29
Programming Languages
• How do we move from own
ideas/task to a completed program
that can run on a computer?
• Using programming languages as
e.g. Java.
Haskell
Miranda
Prolog
C#
BASIC
C++
LISP
C
ML
Fortran
Pascal
Assembler
Maskinkode
GP1, Martin Lillholm
30
More on programming and analysis
• Problem / task
• Analysis
• Algorithms + data structures
• Design (perhaps object oriented – OO)
• Implementation (i e.g. Java)
• Compilation
• Testing
Find the largest of 10 positive numbers
GP1, Martin Lillholm
31
What is a programming language?
A (high level) programming language is a set of rules for how to
write instructions for the computer. Syntax vs. semantics.
You have to be very careful with computer instructions:
Correct:
label.setForeground(Color.blue);
Completely wrong:
label.setforeground(color.blue);
(Java differentiates between upper and lower case letters!)
How do we make the transition from an arbitrary high level language to
machine instructions – the only thing a computer can actually execute?
GP1, Martin Lillholm
32
From source code to executable program (general case)
Using a compiler:
Source
Compiler
Linker
(Editor)
Hello.c
(text/ASCII)
Hello.o
(machine code)
Hello.exe
Result
Using an interpreter:
Source
• The interpreter simplifies the
process but will add an extra
layer during execution
Interpreter
(Editor)
• Normally lowers execution
speed
Hello.c
Resultat
GP1, Martin Lillholm
33
From source code to executable program (java)
Using a compiler and an interpreter:
Source
Compiler
(Editor)
Hello.java
text (ASCII)
Java Virtual Machine
Interpreter
(JVM)
Hello.class
(Java bytecode)
Resultat
Why all this … ?
• Platform independence (platform = architecture + operating system)
• PC (AMD/Intel) + Windows
• PC (AMD/Intel) + Linux
• Mac + Mac OS X
• Browser
• Arbitrary gadget med JVM
• Faster than end anormal interpreter (bytecode is quite close to machine code)
GP1, Martin Lillholm
34
An example …
Source i Notepad
Hello.java
Compiled using
javac Hello.java
Executed using
java Hello
Command Line
Interpreter/prompt
SUN’s Java compiler
SUN’s JVM
(runtime environment)
GP1, Martin Lillholm
35
Integrated Development Environments (IDEs)
• An
–
–
–
IDE combines
Text editing (and often enhances it)
Compilation to bytecode
Excution
– Often has support for e.g. a language’s syntax.
BlueJ
JCreator
GP1, Martin Lillholm
36
The structure of a Java program
• When we choose Java, we also choose a set of rules (or a
notation) for how can/must write programs.
• These rules are often called a language’s Syntax.
• The meaning/result of a command or an entire program is
called a language’s Semantics.
• A Java-program consists of
– One or more classes
– A class can have 0 or more attributes
– A class can have 0 or more methods
– A method can have 0 or more commands/instructions
• If a class has a main method, it’s (roughly speaking)
executable. Execution starts a the first instruction in the main
method.
GP1, Martin Lillholm
37
The structure of a Java program
blok start
//
comments about the class
public class MyProgram
{
//
comments about the method
public static void main (String[] args)
{
class
body
method body
method header
}
}
blok end
GP1, Martin Lillholm
38
White spaces and layout
• Space, tab stops and, newlines.
• All used to separate instructions, characters, and words.
• At least one is necessary; the rest is matter of convention
individual taste, and indentation:
– Lincoln.java – L&L side 28
– Lincoln2.java – L&L side 34
– Lincoln3.java – L&L side 35
GP1, Martin Lillholm
39
Comments in programs
• Describes and documents the inner workings of a program
• Do not affect what the program does
• All classes and methods should be described using short
comments …
• Do use comments that is on a higher level of abstraction than
the code their describing …
• To types exist:
// A single line comments
// Another one …
/* A multiple line
comment */
• Avoid unnecessary comments
x = x + 2 // Two is added to x
GP1, Martin Lillholm
40
Reserved words
• Java’s reserved words …
abstract
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum
extends
false
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
true
try
void
volatile
while
GP1, Martin Lillholm
41
Three types of Java programs
• ”Ordinary”
– Input and output using keyboard, files, and/or the
command line.
• Programs with GUI
– Programs with Graphical User Interfaces
– Windows, dialogue boxes, buttons, ...
– Mouse, keyboard
• Applets
– Programs that can be executed in a browser
– Apart from that … often like GUI programs
– Why applets?
GP1, Martin Lillholm
42
Hello.java
– Hello.java (see home page)
GP1, Martin Lillholm
43
HelloGUI.java
– HelloGUI.java (see home page)
GP1, Martin Lillholm
44
HelloApplet.java
– HelloApplet.java (se home page)
– HelloApplet.html (se home page)
GP1, Martin Lillholm
45
Object Oriented Programming 101
• A program is typically written to solve a problem
• How do understand the problem and then design a solution
that’s suited for computer implementation
• The OO paradigm is an option…
• One of the basic ideas is to not only write code that solves
the problem but also to model the problem domain
GP1, Martin Lillholm
46
More OO - objects
•
Objects are well suited to model things from any problem domain
– State
– “Behaviour”, services (methods)
•
A bank account has:
– A balance
– An interest rate
– A credit limit
– …
•
Given a bank account you can:
– Get the balance
– Deposit money
– Withdraw money
– Max out your credit limit ...
•
Methods can and often will change the state of an object
•
The largest of ten numbers again …
GP1, Martin Lillholm
47
More OO – Classes
• Classes serve as the blueprint of objects
• Classes are abstractions/concepts and actually seldom used
in running programs per se.
• Objects are realisations/instances of classes and the bread
and butter of running programming but again
• Object’s attributes and methods are described using classes
• From one class one can make several objects/instances
• More OO later in this course … except for the simple bank
account example …
GP1, Martin Lillholm
48
More OO – an example
A class
(concept)
An object
(realisation)
Bankkonto
John’s bank account
Balance: 5257,-
Several object
from one class
Jens’ bank account
Balance: 1245069,Tanja’s bank account
Balance: -16833,-
GP1, Martin Lillholm
49