welcome to comp 10110 computer programming 1 lecturer: fintan costello

23
Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Post on 22-Dec-2015

243 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Welcome to COMP 10110Computer Programming 1

Lecturer: Fintan Costello

Page 2: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Where you are

• COMP 10110:

ComputerProgramming (Tues@10 / Thurs@10)

• This class is for

Computer Science Denominated Entry students

Page 3: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

What will you learn on this course?

How to make a computer do what you want:•Write computer programs to respond to different situations

You will also develop a useful attitude:•Not to be afraid when things go wrong

•Write computer programs to manipulate data in various ways

•Write computer programs to solve problems.

•To try things on your computer and learn from mistakes

•To solve problems in a clear, unambiguous & precise way.

Page 4: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

What is a computer program?A general definition:

A computer program is a set of written instructions which, when running (executing) on a computer, allow the computer

• Remember things (by placing them in ‘variables’)

• Produce new things by combining things in different ways

• Make decisions

• Work until an aim is achived

• Communicate with the user (or other programs)

These are all things we do when thinking. There is an interesting relationship between computer programs & our thought processes.

Page 5: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Why learn to program?

By learning to program, we gain the ability to control and understand the most complex, sophisticated, and flexible tools humanity has ever created, and ones which dominate many aspects of our lives.

The technological reason:To make useful & interesting things

The scientific reason: To gain understanding of our world

The psychological reason: To gain understanding of ourselves

By learning to program, we gain tools that contribute to scientific understanding of our world (simulation of things around us, data analysis, applying the computing viewpoint to things around us)

By learning to program, we learn to think about our own thought processes, to examine how we think in different situations, and to transfer written versions of those thought processes into a computer.

Page 6: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

COMP10110 intro to programming: what to do

Attend programming lectures twice weekly

Attend practicals once weekly (1 * 2 hours) Practicals start next week in Comp Sci building

Go through some of the textbook yourself (The book is “Java Concepts”, by Cay Horstmann)

Marks based on practical work (40%) plus an exam at christmas (60%).

Practical work must be submitted each week. Practical work must be satisfactory to pass

COMP 10110

Page 7: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

What is programming?

• A computer program is a set of written instructions which explain how to do a certain task or solve a certain problem.

• These instructions are written in a ‘programming language’ which the computer can understand.

• When we give these instructions to a computer it can carry out that task or solve that problem.

• A computer can carry out lots of tasks and solve lots of problems, because it can run lots of different programs.

Page 8: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

What’s so special about programming?

• When we give people instructions on how to solve a problem, we can rely on their natural intelligence to understand any steps we’ve fogotten to mention.

• With a computer, we must fully specify every single step: we must give an absolutely complete explanation of how to solve the problem.

• This need to competely specify our solutions to problems is unique to programming.

• Programming, therefore, is

the study of problem-solving.

Page 9: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Programming languages.• Just as there are lots of different natural languages

(French, English, Swahili…) there are lots of different programming languages.

• We’re going to learn a language called Java (a widely-used and powerful general-purpose language).

• Our aim in studying Java is not simply to learn the language; instead, we’re going to learn how to solve problems, and will use this language to write and test our solutions.

• But first: How can a computer understand a language anyway?

Page 10: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

How do computers understand instructions?

• A computer is made out of rock (silicon; that is, sand) and metal. How can it understand instructions in a language?

• The basic idea is simple: instructions are represented by codes or patterns of switches (patterns like ‘on-off-on-on-off-off-on-on’)

• A given code or pattern not only stands for a given instruction; when certain switches deep inside the computer (in the ‘central processing unit’: the CPU) are set in that pattern, they cause the action required by that instruction to be carried out.

• In other words, the code representing the instruction (‘on-off-on-on-off-off-on-on’) also produces the action required by that instruction, when the switches are set in that code.

• So, for example ‘on-off-on-on-off-off-on-on’ (or 10110011: we usually represent these codes by 1s and 0s) could mean ‘start a new page on the printer’ because when the switches in CPU are set in that code, they cause a new page to be fed to the printer.

Page 11: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

off off

switches

wires

wheel motors

A very simple example: a toy car.

Codes:

on-on means ‘go forward’

on-off means ‘go left’

off-on means ‘turn right’

off-off means ‘do nothing’

0 0

Rather than using the words ‘on’ and ‘off’ to represent switches turned on or off, we use the numbers 1 and 0.

If this switch is on, an electric signal goes down these wires, activating the wheel motors

Page 12: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

0 0 1 01 0

1 00 1 0 1

1 1 1 1 1 1 1 1 0 1

0 1 0 1 1 1 1 1 1 1 1 1 1 1

1 1

A very simple example: a toy car.

Codes:

1-1 means ‘go forward’

1-0 means ‘go left’

0-1 means ‘turn right’

0-0 means ‘do nothing’

Page 13: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Very rough structure of a computer

0 0 0 1 0 0 0 1

Memory: each switch is on/off (1 or 0)

Arithmetic unit: circuits that add together signals from memory

Control unit: different settings of switches make different things happen (select rows from memory, cause arithmetic computations, etc)

Loads signals from memory into control unit

Page 14: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Very rough structure of a computer

0 0 0 1 0 0 0 1

Memory: each switch is on/off (1 or 0)

Arithmetic unit: circuits that add together signals from memory

Control unit: different settings of switches make different things happen (select rows from memory, cause arithmetic computations, etc)

Loads signals from memory into control unit

Page 15: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Programming languages• The commands controlling a computer are sequences of 1 and 0

(switches on and off). A given set of 1s and 0s will cause the computer to carry out some specific action (because it will set the switches to produce that action).

• It’s very hard to write instructions for a computer in 1s and 0s (in “machine code”). Instead, we will write our instructions for our computers in a programming language (which is a bit like english), and use a special program to translate our programming language instructions into machine code instructions.

• In the Java language, this translation process has two stages: we’ll first use the Java “compiler” to translate our instructions into an intermediate form called “bytecode” which all computers can understand, and then use the Java “interpreter” to translate that intermediate form into the machine code which our particular computer uses.

Page 16: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Programming languages

Natural languages: French, English, Swahili…

Programming languages: C, perl, Java...

Natural languages are forgiving: if you have wrong the grammar, pronounciation, or speling, people can still understanding you be.

Computers are not as smart as people: if you don’t have your program’s grammar and spelling exactly right, the computer will not understand and will give an error.

There are two different types of error. Can you guess what they are?

Errors can arise if the compiler cannot translate your instructions into machine code (a ‘compiler error’), or if the computer cannot actually do what you have asked it to do (a ‘run-time error’).

Page 17: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Your first java programHelloTester.java (Book, p. 15)

public class HelloTester{ public static void main(String[] args) { // displays a greeting at the console

System.out.println("Welcome to Java"); }}

Page 18: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Reserved words

public class HelloTester{ public static void main(String[] args) { // displays a greeting at the console

System.out.println("Welcome to Java"); }}

The words in bold are reserved words: they mean something special in the java programming language.

Reserved words are case-sensitive: Public is wrong.

Page 19: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

What do these words mean?public class HelloTester{ public static void main(String[] args) { // displays a greeting at the console

System.out.println("Welcome to Java"); }}

class is a word identifying a class (a set) of things in Java. In this case, it’s the class of things that will print out a hello message.

public is a modifier which identifies something as publicly accessible (other programs can “call” it, which means run it)

main identifies the main part of a program (the bit that does most of the work). We’ll explain static and void later.

Page 20: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Blocks {......}

A block is a sequence of code between matching curly brackets

This block identifies everything that is in the public class HelloTester

This block identifies everything that is in the main part of the class.

public class HelloTester{ public static void main(String[] args) { System.out.println("Welcome to Java"); }}

Page 21: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Statements (commands)

A statement is an action.

A statement ends with a semi-colon ;

System.out.println("Welcome...");

println means ‘print this on a line of its own’ (you can also use print, which doesn’t start a new line).

System.out means that println is an action carried out by the output part of a java class which you can use to interact with your computer’s System.

We’ll learn more about this later when we talk about methods

Page 22: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

Comments in Java

Comments are simply notes for the programmer to remind themseleves and other programmers what a program is meant to do. Comments can contain any text you like

// this is a one line comment// and here is another

/* this comment can extend over several lines ....... */

Comments are not java commands. Always comment your programs! It helps you remember what’s going on.

Page 23: Welcome to COMP 10110 Computer Programming 1 Lecturer: Fintan Costello

A more interesting programNext time I’ll explain exactly the steps you go through to get the HelloTester.java program to run.

I’ll also introduce a more complex program: one that uses all the facilities of java to do some animation.

You can see that program running here:

http://inismor.ucd.ie/~fintanc/BouncingBallApplet.html

I’ll first explain how we get this program to run, and then use it to give you a tour around the java programming language.

You can get all lecture slides etc for this course via blackboard or at

http://inismor.ucd.ie/~fintanc/COMP101X0/