1 introduction
play

1. Introduction Welcome to the Lecture Series! Hermann Lehner, - PowerPoint PPT Presentation

1. Introduction Welcome to the Lecture Series! Hermann Lehner, Felix Friedrich https://www.mentimeter.com/s/54775dbcef2827005cfcaa8e80bff221 Computer Science I Course at D-BAUG at ETH Zurich Autumn 2019 1 Programming and Problem Solving


  1. 1. Introduction Welcome to the Lecture Series! Hermann Lehner, Felix Friedrich https://www.mentimeter.com/s/54775dbcef2827005cfcaa8e80bff221 Computer Science I Course at D-BAUG at ETH Zurich Autumn 2019 1 Programming and Problem Solving Mathematics used to be the lingua franca of the natural sciences In this course you learn how to program using Java on all universities. Today this is computer science. Software development is a handicraft Lino Guzzella, president of ETH Zurich 2015-2018, NZZ Online, 1.9.2017 Analogy: learn to play a musical instrument The problem: nobody has become a pianist from listening to music. Hence this course offers several possibilities, to train. Make use of it! ((BTW: Lino Guzzella is not a computer scientist, he is a mechanical engineer and prof. for thermotronics ) 2 3

  2. Programming and problem solving Course Content Programming using Java In this course you learn to solve problems with selected algorithms and arrays introduction data structures statements and expressions methods and recursion Fundamental knowledge independent of the language number representations types, classes and objects Comparison: musical scale, read music, rythm skills. control flow inheritance and polymorphy The problem: without musical instrument this is no fun. Hence we combine learning problem solving with learning the Algorithmen programming language Java. Searching and Sorting 4 5 Goal of today’s Lecture 1.1 Computer Science and Algorithms Introduction of computer model and algorithms Computer Science, Euclidean Algorithm Writing a first program General informations to the course 6 7

  3. Algorithm: Fundamental Notion of Computer Sci- Oldest Nontrivial Algorithm ence Euclidean algorithm (from the elements from Euklid, 3. century B.C.) Algorithm: Instructions to solve a problem step by step Execution does not require any intelligence, but precision (even Input: integers a > 0 , b > 0 computers can do it) Output: gcd of a und b according to Muhammed al-Chwarizmi , http://de.wikipedia.org/wiki/Algorithmus author of an arabic While b � = 0 computation textbook (about 825) If a > b then a ← a − b else: b ← b − a Result: a . a b a b a b a b “Dixit algorizmi...” (Latin translation) 8 9 Euklid in the Box Speicher 0 1 2 3 4 5 6 7 8 9 1.2 Computer Model R > L ? L = 0? L − R R − L [ 8 ] [ 9 ] springe springe springe a b stop → [ 8 ] → [ 9 ] → L → R zu 0 zu 0 zu 6 Turing Machine, Von Neumann Architecture Programmcode Daten Daten While b � = 0 If a > b then L inks R echts a ← a − b else: a b b ← b − a Ergebnis: a . Register 10 11

  4. Computers – Concept Computer – Implementation An bright idea: universal Turing machine (Alan Turing, 1936) Z1 – Konrad Zuse (1938) http://commons.wikimedia.org/wiki/File:John_von_Neumann.jpg ENIAC – John Von Neumann (1945) http://www.hs.uni-hamburg.de/DE/GNT/hh/biogr/zuse.htm http://en.wikipedia.org/wiki/Alan_Turing Konrad Zuse Alan Turing John von Neumann 12 13 Computer Memory for data and program Sequence of bits from { 0 , 1 } . Program state: value of all bits. Aggregation of bits to memory cells (often: 8 Bits = 1 Byte) Ingredients of a Von Neumann Architecture Memory (RAM) for programs and data Every memory cell has an address. Processor (CPU) to process programs and data Random access: access time to the memory cell is (nearly) independent of its address. I/O components to communicate with the world 14 15

  5. Processor Computing speed In the time, onaverage, that the sound takes to travel from from my mouth to you ... The processor (CPU) executes instructions in machine language has an own "fast" memory (registers) 30 m � = more than 100 . 000 . 000 instructions can read from and write to main memory features a set of simplest operations = instructions (e.g. adding to register values) a contemporary desktop PC can process more than 100 millions instructions 1 1 Uniprocessor computer at 1 GHz. 16 17 Programming Programming Languages With a programming language we issue commands to a computer such that it does exactly what we want. The sequence of instructions is the The language that the computer can understand (machine language) is (computer) program http://en.wikipedia.org/wiki/Harvard_Computers very primitive. Simple operations have to be disassembled into many single steps The machine language varies between computers. The Harvard Computers , human computers, ca.1890 18 19

  6. Higher Programming Languages Java is based on a virtual machine (with von-Neumann architecture) Program code is translated into intermediate code can be represented as program text that Intermediate code runs in a simulated computing envrionment, the can be understood by humans intermediate code is executed by an interpreted Optimisation: Just-In-Time (JIT) compilation of frequently used code: is independent of the computer model virtual machine → physical machine → Abstraction! Consequence, and manifested goal of the Java developers: portability write once – run anywhere 20 21 Programming Tools 2. Introduction to Java Editor: Program to modify, edit and store Java program texts Compiler: program to translate a program text into machine language Programming – a first Java Program Computer: machine to execute machine language programs Operating System: program to organize all procedures such as file handling, editor-, compiler- and program execution. 22 23

  7. German vs. Programming Language Syntax and Semantics Deutsch Es ist nicht genug zu wissen, Like our language, programs have to be formed according to certain man muss auch anwenden. rules. (Johann Wolfgang von Goethe) Syntax : Connection rules for elementary symbols (characters) Semantics : interpretation rules for connected symbols. Java / C / C++ Corresponding rules for a computer program are simpler but also more strict because computers are relatively stupid. // computation int b = a * a; // b = a^2 b = b * b; // b = a^4 24 25 Syntax and Semantics of Java First Java Program // Program to raise a number to the eighth power Class : a program public class Main { Syntax Method : named sequence public static void main(String[] args) { What is a Java program? of statements. Is it grammatically correct? // input Out.print("Compute a^8 for a= ?"); int a; Semantics a = In.readInt(); What does a program mean ? // computation What kind of algorithm does a program implement? int b = a * a; // b = a^2 b = b * b; // b = a^4 // output b * b, i.e. a^8 Out.println(a + "^8 = " + b * b); } 26 27 }

  8. Java Classes Behavior of a Program A Java program comprises at least one class with main-method. The At compile time: sequence of statements in this method is executed when the program starts. program accepted by the compiler (syntactically correct) Compiler error public class Main{ // Potentiell weiterer Code und Daten During runtime: correct result public static void main(String[] args) { // Hier beginnt die Ausfuehrung incorrect result ... program crashes } program does not terminate (endless loop) } 28 29 Comments and Layout Comments // Program to raise a number to the eighth power Comments public class Main { are contained in every good program. public static void main(String[] args) { Kommentare // input document what and how a program does something and how it should Out.print("Compute a^8 for a= ?"); be used, int a; are ignored by the compiler a = In.readInt(); Syntax: “double slash” // until the line ends. // computation int b = a * a; // b = a^2 The compiler ignores additionally b = b * b; // b = a^4 Empty lines, spaces, // output b * b, i.e. a^8 Indendations that should reflect the program logic Out.println(a + "^8 = " + b * b); } } 30 31

  9. Comments and Layout Statements // Program to raise a number to the eighth power public class Main { public static void main(String[] args) { The compiler does not care... // input Out.print("Compute a^8 for a= ?"); int a; public class Main{public static void main(String[] args){Out.print Ausdrucksanweisungen a = In.readInt(); ("Compute a^8 for a= ?");int a;a = In.readInt();int b = a*a;b = // computation b * b;Out.println(a + "^8 = " + b * b);}} int b = a * a; // b = a^2 b = b * b; // b = a^4 ... but we do! // output b * b, i.e. a^8 Out.println(a + "^8 = " + b * b); } } 32 33 Expression Statements Statements have the following form: building blocks of a Java program expr; are executed (sequentially) where expr is an expression end with a semicolon Effect is the effect of expr , the value of expr is ignored. Any statement provide an effect (potentially) b = b * b; 34 35

Recommend


More recommend