24
Welcome to COMP 10010 Introduction to Computer Programming 1 Lecturer: Fintan Costello

Welcome to COMP 10010

  • Upload
    newman

  • View
    64

  • Download
    0

Embed Size (px)

DESCRIPTION

Welcome to COMP 10010. Introduction to Computer Programming 1 Lecturer: Fintan Costello. Where you are. COMP 10010: Introduction to Programming (Tues@10 / Thurs@10) This class includes: Science students Arts students Computer Science Denominated Entry students - PowerPoint PPT Presentation

Citation preview

Page 1: Welcome to COMP 10010

Welcome to COMP 10010Introduction to Computer

Programming 1

Lecturer: Fintan Costello

Page 2: Welcome to COMP 10010

Where you are

• COMP 10010:

Introduction to Programming (Tues@10 / Thurs@10)

• This class includes:

Science students

Arts students

Computer Science Denominated Entry students

BA Computer Science students

Actuarial & Financial students

Page 3: Welcome to COMP 10010

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 10010

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 10010

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 10010

Ireland needs programmers!

From Fourth Report of the Expert Group on Future Skills Needs presented to the Tánaiste and Minister for Enterprise, Trade and Employment and Minister for Education and Science, October 2003.

Year Predicted Demand for New Computer Graduates

Predicted Gap between Number of Computer

Graduates and Demand

2006 2,807 941

2007 3,094 1,511

2008 3,417 1,854

2009 3,777 2,242

Page 7: Welcome to COMP 10010

COMP10010 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 10010

Page 8: Welcome to COMP 10010

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 9: Welcome to COMP 10010

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 10: Welcome to COMP 10010

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 11: Welcome to COMP 10010

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 12: Welcome to COMP 10010

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 13: Welcome to COMP 10010

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 14: Welcome to COMP 10010

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 10010

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 16: Welcome to COMP 10010

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 17: Welcome to COMP 10010

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 18: Welcome to COMP 10010

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 19: Welcome to COMP 10010

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 20: Welcome to COMP 10010

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 21: Welcome to COMP 10010

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 22: Welcome to COMP 10010

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 23: Welcome to COMP 10010

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 24: Welcome to COMP 10010

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/COMP10010/