230 likes | 362 Views
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. What will you learn on this course?. How to make a computer do what you want:.
E N D
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
What will you learn on this course? How to make a computer do what you want: • Write computer programs to respond to different situations • Write computer programs to manipulate data in various ways • Write computer programs to solve problems. You will also develop a useful attitude: • Not to be afraid when things go wrong • To try things on your computer and learn from mistakes • To solve problems in a clear, unambiguous & precise way.
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.
Why learn to program? The technological reason: To make useful & interesting things The psychological reason: To gain understanding of ourselves The scientific reason: To gain understanding of our world 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. 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. 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)
COMP10110intro 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
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.
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.
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?
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.
switches If this switch is on, an electric signal goes down these wires, activating the wheel motors wires wheel motors 0 0 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’ off off Rather than using the words ‘on’ and ‘off’ to represent switches turned on or off, we use the numbers 1 and 0.
0 0 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 1 1 1 1 1 1 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’
0 0 0 1 0 0 0 1 Very rough structure of a computer Memory: each switch is on/off (1 or 0) Arithmetic unit: circuits that add together signals from memory Loads signals from memory into control unit Control unit: different settings of switches make different things happen (select rows from memory, cause arithmetic computations, etc)
0 0 0 1 0 0 0 1 Very rough structure of a computer Memory: each switch is on/off (1 or 0) Arithmetic unit: circuits that add together signals from memory Loads signals from memory into control unit Control unit: different settings of switches make different things happen (select rows from memory, cause arithmetic computations, etc)
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.
Programming languages Natural languages: French, English, Swahili… Natural languages are forgiving: if you have wrong the grammar, pronounciation, or speling, people can still understanding you be. Programming languages: C, perl, Java... 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’).
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"); } }
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.
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"); } } classis 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. publicis a modifier which identifies something as publicly accessible (other programs can “call” it, which means run it) mainidentifies the main part of a program (the bit that does most of the work). We’ll explain static and void later.
This block identifies everything that is in the public class HelloTester This block identifies everything that is in the main part of the class. Blocks {......} A block is a sequence of code between matching curly brackets public class HelloTester { public static void main(String[] args) { System.out.println("Welcome to Java"); } }
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
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.
A more interesting program Next 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/