dcs/100: procedural programming - eecspc/teaching/introprogramming/... · dcs/100: procedural...

42
DCS/100: Procedural Programming Week 1 Paul Curzon Queen Mary, University of London DCS/100: wk 1 – p.1/41

Upload: vandung

Post on 21-Jun-2018

265 views

Category:

Documents


1 download

TRANSCRIPT

DCS/100: Procedural ProgrammingWeek 1

Paul Curzon

Queen Mary, University of London

DCS/100: wk 1 – p.1/41

People

Paul Curzon

Fabrizio Smeraldi

Others: TA’s,. . .

DCS/100: wk 1 – p.1/41

This course teaches you to write

SIMPLE PROGRAMS.

Programming is a SKILL.

You learn skills through practice.

Anyone can learn to writesimple programs.

DCS/100: wk 1 – p.2/41

Objectives

By the end of the course you will be able to:

write simple programs from scratch

write programs in steps based on existing ones

work out what a program does without executing it

test programs to ensure they work correctly

explain programming constructs to others

DCS/100: wk 1 – p.3/41

Resources

Book:Programming for Everyone in Javaby Per Brinch Hansen, Springer, 1999 ISBN 0-387-98683-9

Intranet Site (password protected)

https://www.dcs.qmul.ac.uk/intranet/courses/ug/coursenotes/DCS100/

DCS/100: wk 1 – p.4/41

Every Week

2hrs Lab Class ITLCome to assigned slot

2hrs Lecture Drapers2hrs reading, revision, preparation

2hrs completing lab exercise sheets

2hrs assessed coursework

Total: 10 hours

DCS/100: wk 1 – p.5/41

Course Assessment

Short Programming Exercises (8%)

Programming Mini-project (12%)

Two in-class programming tests (80%)

N.B. You MUST pass the SHORT PROGRAMS,MINI-PROJECT and TESTS

THERE IS NO FINAL EXAM

DCS/100: wk 1 – p.6/41

If you fail the first time

If you fail the first time you will not get more than 40%for the course.

If you fail the courseworks you can resubmit thecourseworks later.

If you fail the tests you will have to do a resit during theexam session.

FAILING IS NOT A GOOD IDEA!

You need to work hard from the start to avoid it.

DCS/100: wk 1 – p.7/41

Java and Linux-GNU

Java developed by Sun in 90’s for web programming

idea: can run Java programs safely on anything(actually Java applets not programs)

contrasts with C,C++, etc:neither safe nor cross-platform

real language, becoming widely used (see job ads)

Linux-GNU

Free communally developed version of the Unixoperating system for PC’s.

DCS/100: wk 1 – p.8/41

Week 1

Simple straight-line programs

By the end of the week you will be able to:

write and run simple programs

print out a message to the user

read input from the user and store it in a variable

print out messages that include the contents ofvariables.

Reading: Chapter 1 of Brinch Hansen

DCS/100: wk 1 – p.9/41

What is a program?

An algorithm is a series of instructions that whenfollowed achieve some task.

It also says what order the instructions should befollowed.

A program is an algorithm written in a special language.

It is a piece of text written in a formal language that thecomputer can understand.

There are many ways (algorithms) to achieve one task(eg Obtaining Pizza)

A computer can appear to be intelligent just by followinginstructions blindly (eg Noughts and Crosses)

DCS/100: wk 1 – p.10/41

Noughts and Crosses Instructions

1. Go in a corner.

2. If the other player went in the opposite cornerthen go in a free corner.Otherwise go in that opposite corner.

3. If there are two Xs and a space in a rowthen go in that space.Otherwise if there are two Os and a space in a rowthen go in that space.Otherwise go in a free space.

4. Repeat Instruction 3 above.

5. Go in the free space.

DCS/100: wk 1 – p.11/41

Example Java Program

class hello extends basic{

public static void main (String param[])throws Exception

{

output out = new output();

out.writeln("Hello World!");

out.close();

}}

DCS/100: wk 1 – p.12/41

To run a program

1. create it using a text editor (gedit or emacs)

2. save it as a file

3. compile the file(convert the instructions in the program to a form thecomputer can follow)javac hello.java

4. run the compiled program(tell the computer to follow the instructions in thecompiled program) java hello

If you have problems with steps 3 or 4 - go back to 1.

DCS/100: wk 1 – p.13/41

Structure: Name

The name of the program is hello:

class hello extends basic{

public static void main (String param[])throws Exception

{output out = new output();

out.writeln("Hello World!");

out.close();}

}

DCS/100: wk 1 – p.14/41

Structure: Actual program

The actual program is in red:

class hello extends basic{

public static void main (String param[])throws Exception

{output out = new output();out.writeln("Hello World!");out.close();

}}

DCS/100: wk 1 – p.15/41

Structure: Java garbage

The rest is just stuff that Java needs to get going. Put it inevery program you write:

class hello extends basic{

public static void main (String param[])throws Exception

{output out = new output();

out.writeln("Hello World!");

out.close();}

}DCS/100: wk 1 – p.16/41

Some common pitfalls. . .

Spot these in lectures!

name of file MUST be “name of program”.java(Java puts the executable in “name of program”.classnot “name of file”.class)

change but don’t save(If you don’t save the file you recompile the old version)

change but don’t recompile(If you don’t recompile, you run the old executable)

DCS/100: wk 1 – p.17/41

Some terminology

method: a named operation(like writeln)

class: a named group of related methods(output, hello)

keyword: (or reserved word) a word thathas a special meaning to Javaand cannot be used for anything else,e.g. if, new, return, while

DCS/100: wk 1 – p.18/41

Input and Output

Unfortunately input and output in Java is complicated.This is because it is designed to communicate objectsbetween computers, not words to people.Most initial courses use their own (simpler)input libraries.Java’s object-oriented nature makes this quite easy to do.Brinch Hansen has his own library. . .

DCS/100: wk 1 – p.19/41

The three stages of output

Stage 1: Open an output channel

output out = new output();

out is the name of the output channel.

An output channel is just a place to send information (herethe screen).

The rest is a magic incantation (for now).

DCS/100: wk 1 – p.20/41

The three stages of output

Stage 2: Do the outputout.writeln("something");

writeln is the output command. It tells the computer wewant it to output something.

out is the name of the output channel. It tells the computerwhere we want the thing to be written.

"something" is what gets written.

DCS/100: wk 1 – p.21/41

The three stages of output

Stage 2 cont

There are two output commands:

writeln puts its argument on a line, and then goes on tothe next.

write does not go on to the next line. (Useful if you want toput something else on the same line).

DCS/100: wk 1 – p.22/41

The three stages of output

Stage 3: close the output channelout.close();

out is the name of your output channel.

close is the command to close it.

The (); makes the command run.

(Actually you don’t strictly have to do this, the Java system

will close the channel automatically when the program fin-

ishes. It’s just good practice.)

DCS/100: wk 1 – p.23/41

Output Exercise

Write a program that prints out

Hello my name is C3PO

to the screen.

DCS/100: wk 1 – p.24/41

Input

Input also has three stages,

open,

do the input, and

close.

Just as you need an output channel, you also need an inputchannel.

DCS/100: wk 1 – p.25/41

The three stages of input

Stage 1: Open an input channel

input in = new input();

in is the name of the input channel.

An input channel is just a place to get information from(here the keyboard).

The rest is a magic incantation (for now).

DCS/100: wk 1 – p.26/41

The three stages of input

Stage 2: Do the inputname = in.readline();

in is the name of the input channel.

readline is an input command.

This command reads a line of input, and stores it in avariable ch.

Most of the time you read something in, you will want to store

it somewhere so that you can use it later.

DCS/100: wk 1 – p.27/41

The three stages of input

Stage 2 cont

There are lots of input commands, e.g.:

readline reads the rest of the current line of input.

read reads a single character of input

readln also reads the rest of the current line of input, butdoes not tell you what it is! (Confusing, but useful if you justwant to throw the rest of a line away.)

DCS/100: wk 1 – p.28/41

The three stages of input

Stage 3: close the input channelin.close();

in is the name of your input channel.

close is the command to close it.

The (); makes the command run.

(Again, actually you don’t have to do this, the Java systemwill close the channel automatically when the programfinishes. It’s just good manners.)

DCS/100: wk 1 – p.29/41

Exercise: What does this program do?

class hello2 extends basic{

public static void main (String param[])throws Exception

{input in = new input();output out = new output();String name;

out.writeln("What’s your name?");name = in.readline();out.writeln("Hello " + name);in.close(); out.close();

}}

DCS/100: wk 1 – p.30/41

Input Exercise

Write a program that asks the user for their favourite film,then prints out that it likes it too eg:

What is your favourite film?The MatrixThe Matrix! I like that too

where the red part is typed by the user,

the rest is typed by the computer.

DCS/100: wk 1 – p.31/41

Variables

Storing things for use later

Variables are what languages use for storing information ina way that lets you get at it again later.

A variable is like a box into which you can put a piece ofdata.

Different sorts of data need different shaped boxes.

String name;

int year;

DCS/100: wk 1 – p.32/41

Variables

If you want to use a box to store something in, you have todo two things:

get a box of the right kind/size

put something in it

It’s the same with variables.

declare the variable(creates the kind of box you asked for)

initialize it(put something in)

DCS/100: wk 1 – p.33/41

Declaring Variables

You include a statement in the program introducing thevariable:

int X;String name;

These introduce two variables called X and name.

The computer is told that X is an integer (a number) (int),and name is a string of characters (String).

These declare the variables X and name

DCS/100: wk 1 – p.34/41

Putting values in Variables

The commands:

X = 1;name = "fred";

puts 1 in Xand "fred" in name“Box X gets the number 1”“Box name gets the string "fred" ”

DCS/100: wk 1 – p.35/41

All at once

It’s bad manners (and dangerous) to leave variables aroundwithout values in them. So you will often want to declare avariable, and immediately store something in it.Most languages let you do this all at once:

int X = 1;String name = "fred";

These declare the variable and intialise it to contain aninitial value.

DCS/100: wk 1 – p.36/41

Declaring Variables

output out = new output();

output is the name given to output channels

output out declares a new output channel variablecalled out

new output() creates a new output channel to thescreen

output out = new output();declares a new output channel variable called out andinitialises it with a new output channel to the screen.

DCS/100: wk 1 – p.37/41

Assignment

You store something in a variable by assignment

name = in.readline();stores the result of the method call in.readline() inthe variable name.

age = age+1;adds one to the variable age.

year = 2003 - age;sets the variable year to be 2003 minus the value inthe variable age.

DCS/100: wk 1 – p.38/41

Brinch Hansen Library

A common mistakeOutput commands (write(), writeln()), shouldusually have something in the brackets.They take arguments.

Input commands (read(), readln(), readline())NEVER have anything in the brackets.They do not take arguments.

ch = in.read();

NOT

in.read(ch);

DCS/100: wk 1 – p.39/41

Brinch Hansen Library

Another common mistakeThe one you want is

readline()NOT readln()(confusing)

To use readline() to any extent, you’ll want to stick the

result in a variable.

DCS/100: wk 1 – p.40/41

By the end of the week

Once you have done the reading and the exercises youshould be able to:

write and run simple programs

print out a message to the user

read input from the user and store it in a variable

print out messages that include the contents ofvariables.

Reading: Chapter 1 of Brinch Hansen

DCS/100: wk 1 – p.41/41