SLIDE 1 Dennis Komm
Programming and Problem Solving
Introduction to the Course
Spring 2019 – February 18, 2019
SLIDE 2
Welcome to the Course
SLIDE 3 Material
Lecture website
http://lec.inf.ethz.ch/ppl
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 1 / 52
SLIDE 4 The Team
Lecturer Dennis Komm Assistants Moritz Hoffmann Jean Kaufmann Angela Rellstab
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 2 / 52
SLIDE 5 Programming and Problem Solving
In this course, you will learn programming in Java Software development is a craftmanship Just like learning to play an instrument
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 3 / 52
SLIDE 6 Programming and Problem Solving
In this course, you will learn programming in Java Software development is a craftmanship Just like learning to play an instrument Problem: So far, no one became a pianist simply by listening
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 3 / 52
SLIDE 7 Programming and Problem Solving
In this course, you will learn programming in Java Software development is a craftmanship Just like learning to play an instrument Problem: So far, no one became a pianist simply by listening Therefore, this course gives you many opportunities to exercise Use them!
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 3 / 52
SLIDE 8 Programming and Problem Solving
In this course, you will learn problem solving with appropriate algorithms and data structures Language-independent basic knowledge Analogous to rhythm theory, musical scale, and reading notes
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 4 / 52
SLIDE 9 Programming and Problem Solving
In this course, you will learn problem solving with appropriate algorithms and data structures Language-independent basic knowledge Analogous to rhythm theory, musical scale, and reading notes Problem: This is only limited fun without a musical instrument
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 4 / 52
SLIDE 10 Programming and Problem Solving
In this course, you will learn problem solving with appropriate algorithms and data structures Language-independent basic knowledge Analogous to rhythm theory, musical scale, and reading notes Problem: This is only limited fun without a musical instrument Therefore, we combine all this with learning to program in Java
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 4 / 52
SLIDE 11 Goal of Today’s Lecture
Introduction / repetition computer model and algorithms General information about the lecture The first Java program
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 5 / 52
SLIDE 12
Introduction to the Course
Computer Model and Algorithms
SLIDE 13 Computer – Concept
What do computers have to be able to do to compute? Does it have to be able to multiply? Is it not sufficient to be able to add?
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 6 / 52
SLIDE 14 Computer – Concept
What do computers have to be able to do to compute? Does it have to be able to multiply? Is it not sufficient to be able to add? Turing Machine [Alan Turing, 1936] Finite number of states Memory consisting of arbitrarily many cells Pointer to current cell Pointer can change cell’s content and move left or right
Alan Turing [Wikimedia] Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 6 / 52
SLIDE 15 Computer – Implementation
Analytical Engine – Charles Babbage (1837) Z1 – Konrad Zuse (1938) ENIAC – John von Neumann (1945)
Charles Babbage [Wikimedia] Konrad Zuse [Wikimedia] John von Neumann [Wikimedia] Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 7 / 52
SLIDE 16 Computer – Implementation
Components of von Neumann Architecture Memory for programs und data, random access memory (RAM) Processor (CPU) for processing of programs and data I/O components to communicate with periphery Processor – CPU
Arithemtic unit ALU Control unit
BUS Memory In-/Output
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 8 / 52
SLIDE 17 Memory for Program and Data
Sequence of bits Program data: Values of all bits Combination of bits to memory cells (8 bits = 1 byte)
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 9 / 52
SLIDE 18 Memory for Program and Data
Sequence of bits Program data: Values of all bits Combination of bits to memory cells (8 bits = 1 byte) Every memory cell has an address Random access: Access time (almost) independent of address 0 1 0 0 1 1 0 1 0 0 1 0 1 1 1 0 Address: 17 Address: 18
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 9 / 52
SLIDE 19 Algorithm: Central Notion of Computer Science
Algorithm Method for step-by-step solution of a problem
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 10 / 52
SLIDE 20 Algorithm: Central Notion of Computer Science
Algorithm Method for step-by-step solution of a problem Execution does not require intellect, only accuracy
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 10 / 52
SLIDE 21 Algorithm: Central Notion of Computer Science
Algorithm Method for step-by-step solution of a problem Execution does not require intellect, only accuracy after Muhammed al-Chwarizmi; author of a Persian math book (around 825)
"‘Dixit algorizmi. . . "’ Latin translation [Wikimedia] Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 10 / 52
SLIDE 22 “The Oldest (Known) Non-Trivial Algorithm”
Euclid’s Algorithm
from Euclid’s Elements, 300 BC
Input: integers a > 0, b > 0 Output: gcd of a and b
Input: a and b while b = 0 if a > b then a = a − b else b = b − a Output: a
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 11 / 52
SLIDE 23 “The Oldest (Known) Non-Trivial Algorithm”
Euclid’s Algorithm
from Euclid’s Elements, 300 BC
Input: integers a > 0, b > 0 Output: gcd of a and b
Input: a and b while b = 0 if a > b then a = a − b else b = b − a Output: a
a b
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 11 / 52
SLIDE 24 “The Oldest (Known) Non-Trivial Algorithm”
Euclid’s Algorithm
from Euclid’s Elements, 300 BC
Input: integers a > 0, b > 0 Output: gcd of a and b
Input: a and b while b = 0 if a > b then a = a − b else b = b − a Output: a
a b a b
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 11 / 52
SLIDE 25 “The Oldest (Known) Non-Trivial Algorithm”
Euclid’s Algorithm
from Euclid’s Elements, 300 BC
Input: integers a > 0, b > 0 Output: gcd of a and b
Input: a and b while b = 0 if a > b then a = a − b else b = b − a Output: a
a b a b a b
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 11 / 52
SLIDE 26 “The Oldest (Known) Non-Trivial Algorithm”
Euclid’s Algorithm
from Euclid’s Elements, 300 BC
Input: integers a > 0, b > 0 Output: gcd of a and b
Input: a and b while b = 0 if a > b then a = a − b else b = b − a Output: a
a b a b a b a b
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 11 / 52
SLIDE 27 Computing Speed
In the avergage time my voice travels to you. . .
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 12 / 52
SLIDE 28 Computing Speed
In the avergage time my voice travels to you. . . 30 m a desktop computer executes more than
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 12 / 52
SLIDE 29 Computing Speed
In the avergage time my voice travels to you. . . 30 m = more than 100 000 000 instructions a desktop computer executes more than 100 million instructions
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 12 / 52
SLIDE 30 Java
Java is based on a virtual machine (with von Neumann architecture)
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 13 / 52
SLIDE 31 Java
Java is based on a virtual machine (with von Neumann architecture)
program code is tranlated to intermediate code
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 13 / 52
SLIDE 32 Java
Java is based on a virtual machine (with von Neumann architecture)
program code is tranlated to intermediate code Intermediate code runs a simulated environment, interpreted through special interpreter
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 13 / 52
SLIDE 33 Java
Java is based on a virtual machine (with von Neumann architecture)
program code is tranlated to intermediate code Intermediate code runs a simulated environment, interpreted through special interpreter Optimization: Just-in-Time (JIT) compilation of frequently used code: virtual machine ➯ physical machine
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 13 / 52
SLIDE 34 Java
Java is based on a virtual machine (with von Neumann architecture)
program code is tranlated to intermediate code Intermediate code runs a simulated environment, interpreted through special interpreter Optimization: Just-in-Time (JIT) compilation of frequently used code: virtual machine ➯ physical machine
Implication and declared goal of the Java developers: portability
“write once – run anywhere”
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 13 / 52
SLIDE 35
Introduction to the Course
Project
SLIDE 36 Ready, Set, Go!
You have seven weeks to complete a non-trivial project After the first few weeks you will already know the basic concepts, to implement the project
1 2 3 4 5 6 7 learning curve
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 14 / 52
SLIDE 37 Ready, Set, Go!
You have seven weeks to complete a non-trivial project After the first few weeks you will already know the basic concepts, to implement the project
1 2 3 4 5 6 7 learning curve project
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 14 / 52
SLIDE 38 Java Tutorial
In the first weeks, you independently work on our Java tutorial Easy introduction to Java, no previous knowledge necessary Time needed: roughly two hours In the second week, there will be a self-assessment
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 15 / 52
SLIDE 39 Java Tutorial
In the first weeks, you independently work on our Java tutorial Easy introduction to Java, no previous knowledge necessary Time needed: roughly two hours In the second week, there will be a self-assessment
➯ This time is well invested
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 15 / 52
SLIDE 40 Java Tutorial
In the first weeks, you independently work on our Java tutorial Easy introduction to Java, no previous knowledge necessary Time needed: roughly two hours In the second week, there will be a self-assessment
➯ This time is well invested
Tutorial website
https://frontend-1.et.ethz.ch/sc/WKrEKYAuHvaeTqLzr
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 15 / 52
SLIDE 41 Project
You choose a project from a list of predefined projects The project is worked on independently Pattern: Data ➯ read in ➯ process ➯ output Information is continuously updated on course website Exericse: Wednesday, 17–18, HG 26.5 (right after the lecture) Supervised by Moritz Hoffmann, CAB F 69 (moritz.hoffmann@inf.ethz.ch), office hours: Monday, 15–17
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 16 / 52
SLIDE 42 Project – Team Work is Key
You work in teams of two Both partners contribute provably half Projects are graded per team
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 17 / 52
SLIDE 43 Project – Team Work is Key
You work in teams of two Both partners contribute provably half Projects are graded per team
➯ Contact us as soon as there are any problems or
disagreements
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 17 / 52
SLIDE 44 Project – Milestones
Milestones have to be complied with Compliance with the milestone will be part of the grade Every milestone corresponds to some output (document or code)
Week 2
Team assembled Project chosen Tools installed First GIT commit
Week 3
Project plan handed in Solution design fixed
Week 6
All features ready Test plan done
Week 7
Handing in all materials Presentation
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 18 / 52
SLIDE 45 Graded Semester Performance
Presentation of the project on April 3 and 10 Duration: Around 10 minutes, depending on the number of projects handed in Handing in all materials All milestones have to be complied with
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 19 / 52
SLIDE 46 Discussion Platform
You can sign in to our Slack forum
https://bit.ly/2UXaWzg
General discussion of course Search for team partners . . .
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 20 / 52
SLIDE 47
Introduction to Java
SLIDE 48 Programming Tools
Editor: Program to modify, edit and store Java program texts
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 21 / 52
SLIDE 49 Programming Tools
Editor: Program to modify, edit and store Java program texts Compiler: Program to translate a program text into machine language (intermediate code, respectively)
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 21 / 52
SLIDE 50 Programming Tools
Editor: Program to modify, edit and store Java program texts Compiler: Program to translate a program text into machine language (intermediate code, respectively) Computer: Machine to execute machine language programs
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 21 / 52
SLIDE 51 Programming Tools
Editor: Program to modify, edit and store Java program texts Compiler: Program to translate a program text into machine language (intermediate code, respectively) Computer: Machine to execute machine language programs Operating System: Program to organize all procedures such as file handling, editing, compiling, and program execution
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 21 / 52
SLIDE 52 English vs. Programming Language
English “Science is what we understand well enough to explain to a computer. Art is everything else we do. ”
DONALD KNUTH
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 22 / 52
SLIDE 53 English vs. Programming Language
English “Science is what we understand well enough to explain to a computer. Art is everything else we do. ”
DONALD KNUTH
Java / C / C++
// computation int b = a * a; // b = a^2 b = b * b; // b = a^4
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 22 / 52
SLIDE 54 Syntax and Semantics
Like our language, programs have to be formed according to certain rules
Syntax: Connection rules for elementary symbols (characters) Semantics: Interpretation rules for connected symbols
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 23 / 52
SLIDE 55 Syntax and Semantics
Like our language, programs have to be formed according to certain rules
Syntax: Connection rules for elementary symbols (characters) Semantics: Interpretation rules for connected symbols
Corresponding rules for a computer program are simpler but also more strict because computers are relatively stupid
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 23 / 52
SLIDE 56 Kinds of Errors Illustrated with English Language
The car drove too fast.
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 57 Kinds of Errors Illustrated with English Language
The car drove too fast.
Syntactically and semantically correct
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 58 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat.
Syntactically and semantically correct
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 59 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat.
Syntactically and semantically correct Syntax error: word building
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 60 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat. Red the car is.
Syntactically and semantically correct Syntax error: word building
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 61 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat. Red the car is.
Syntactically and semantically correct Syntax error: word building Syntax error: word order
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 62 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat. Red the car is. I find inspiration in cooking my dog and my cat
Syntactically and semantically correct Syntax error: word building Syntax error: word order
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 63 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat. Red the car is. I find inspiration in cooking, my dog, and my cat.
Syntactically and semantically correct Syntax error: word building Syntax error: word order Syntax error: missing punctuation marks
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 64 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat. Red the car is. I find inspiration in cooking my dog and my cat She is not tall and red-haired.
Syntactically and semantically correct Syntax error: word building Syntax error: word order Syntax error: missing punctuation marks
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 65 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat. Red the car is. I find inspiration in cooking my dog and my cat She is not tall and red-haired.
Syntactically and semantically correct Syntax error: word building Syntax error: word order Syntax error: missing punctuation marks Syntactically correct, but ambiguous [no analogon]
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 66 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat. Red the car is. I find inspiration in cooking my dog and my cat She is not tall and red-haired. I own an red car.
Syntactically and semantically correct Syntax error: word building Syntax error: word order Syntax error: missing punctuation marks Syntactically correct, but ambiguous [no analogon]
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 67 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat. Red the car is. I find inspiration in cooking my dog and my cat She is not tall and red-haired. I own an red car.
Syntactically and semantically correct Syntax error: word building Syntax error: word order Syntax error: missing punctuation marks Syntactically correct, but ambiguous [no analogon] Syntactically correct, but gramatically and semanti- cally wrong: wrong article [type error]
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 68 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat. Red the car is. I find inspiration in cooking my dog and my cat She is not tall and red-haired. I own an red car. The bike gallops fast.
Syntactically and semantically correct Syntax error: word building Syntax error: word order Syntax error: missing punctuation marks Syntactically correct, but ambiguous [no analogon] Syntactically correct, but gramatically and semanti- cally wrong: wrong article [type error]
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 69 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat. Red the car is. I find inspiration in cooking my dog and my cat She is not tall and red-haired. I own an red car. The bike gallops fast.
Syntactically and semantically correct Syntax error: word building Syntax error: word order Syntax error: missing punctuation marks Syntactically correct, but ambiguous [no analogon] Syntactically correct, but gramatically and semanti- cally wrong: wrong article [type error] Syntactically and gramatically correct, but semanti- cally wrong [run-time error]
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 70 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat. Red the car is. I find inspiration in cooking my dog and my cat She is not tall and red-haired. I own an red car. The bike gallops fast. We saw her duck.
Syntactically and semantically correct Syntax error: word building Syntax error: word order Syntax error: missing punctuation marks Syntactically correct, but ambiguous [no analogon] Syntactically correct, but gramatically and semanti- cally wrong: wrong article [type error] Syntactically and gramatically correct, but semanti- cally wrong [run-time error]
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 71 Kinds of Errors Illustrated with English Language
The car drove too fast. Thecar drove too fsat. Red the car is. I find inspiration in cooking my dog and my cat She is not tall and red-haired. I own an red car. The bike gallops fast. We saw her duck.
Syntactically and semantically correct Syntax error: word building Syntax error: word order Syntax error: missing punctuation marks Syntactically correct, but ambiguous [no analogon] Syntactically correct, but gramatically and semanti- cally wrong: wrong article [type error] Syntactically and gramatically correct, but semanti- cally wrong [run-time error] Syntactically and sematically correct, but ambiguous [no analogon]
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 24 / 52
SLIDE 72
Introduction to Java
A First Java Program
SLIDE 73 A First Java Program
// input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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);
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 25 / 52
SLIDE 74 A First Java Program
public static void main(String[] args) { // input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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); }
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 25 / 52
SLIDE 75 A First Java Program
public class Main { public static void main(String[] args) { // input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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); } }
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 25 / 52
SLIDE 76 A First Java Program
public class Main { public static void main(String[] args) { // input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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); } } Method: named sequence of statements Class: a program
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 25 / 52
SLIDE 77 Behavior of a Program
At compile time Program accepted by the compiler (syntactically correct) Compiler error
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 27 / 52
SLIDE 78 Behavior of a Program
At compile time Program accepted by the compiler (syntactically correct) Compiler error During runtime correct result incorrect result program crashes program does not terminate (endless loop)
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 27 / 52
SLIDE 79 Comments
public class Main { public static void main(String[] args) { // input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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); } }
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 28 / 52
SLIDE 80 Comments
public class Main { public static void main(String[] args) { // input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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); } } Comments
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 28 / 52
SLIDE 81 Comments and Layout
The compiler does not care. . .
public class Main{public static void main(String[] args){Out.print ("Compute a^8 for a= ?");int a;a = In.readInt();int b = a*a;b = b * b;Out.println(a + "^8 = " + b * b);}}
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 30 / 52
SLIDE 82 Comments and Layout
The compiler does not care. . .
public class Main{public static void main(String[] args){Out.print ("Compute a^8 for a= ?");int a;a = In.readInt();int b = a*a;b = b * b;Out.println(a + "^8 = " + b * b);}}
. . . but we do
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 30 / 52
SLIDE 83
Introduction to Java
Statements
SLIDE 84 Statements
public class Main { public static void main(String[] args) { // input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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); } }
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 31 / 52
SLIDE 85 Statements
public class Main { public static void main(String[] args) { // input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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); } } Expression statements
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 31 / 52
SLIDE 86 Statements – Values and Effects
public class Main { public static void main(String[] args) { // input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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); } }
Effect: Output of the string Compute ... Effect: Input of number and store it in a Effect: Store the computed value of a*a in b Effect: Store the computed value of b*b in b Effect: Output of the value of a, the string ˆ8 = , and the calculated value of b*b
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 35 / 52
SLIDE 87
Introduction to Java
Declaration
SLIDE 88 Variable Definitions
public class Main { public static void main(String[] args) { // input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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); } }
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 37 / 52
SLIDE 89 Variable Definitions
public class Main { public static void main(String[] args) { // input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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); } } Declaration statement Type names
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 37 / 52
SLIDE 90
Introduction to Java
Variables and Objects
SLIDE 91 Variables
Variables represent (varying) values have
name type value address
are “visible” in the program context
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 42 / 52
SLIDE 92 Variables
Variables represent (varying) values have
name type value address
are “visible” in the program context Example
int a; defines a variable with
name: a type: int value: (initially) undefined address: determined by compiler
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 42 / 52
SLIDE 93 Objects
Objects represent values in main memory have type, address and value (memory content at the address) can be named . . . . . . but also anonymous
Remark A program has a fixed number of variables. In order to be able to deal with a variable number of values, it requires “anonymous” addresses that can be addressed via temporary names.
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 43 / 52
SLIDE 94
Introduction to Java
Expressions
SLIDE 95 Expressions
Expressions represent computations
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 45 / 52
SLIDE 96 Expressions
Expressions represent computations are either primary (b)
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 45 / 52
SLIDE 97 Expressions
Expressions represent computations are either primary (b)
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 45 / 52
SLIDE 98 Expressions
Expressions represent computations are either primary (b)
. . . from different expressions by operators
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 45 / 52
SLIDE 99 Expressions
// input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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 );
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 46 / 52
SLIDE 100 Expressions
// input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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 ); Variable name, primary expression Variable name, primary expression
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 46 / 52
SLIDE 101 Expressions
// input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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 ); Composite expression
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 46 / 52
SLIDE 102
Introduction to Java
Operators and Operands
SLIDE 103 Operators and Operands
// input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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 );
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 49 / 52
SLIDE 104 Operators and Operands
// input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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 ); Left operand (variable) Right operand (expression)
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 49 / 52
SLIDE 105 Operators and Operands
// input Out.print("Compute a^8 for a= ?"); int a; a = In.readInt(); // computation 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 ); Left operand (variable) Right operand (expression) Assignment operator Multiplication operator
Programming and Problem Solving – Introduction to the Course Spring 2019 Dennis Komm 49 / 52
SLIDE 106
Thanks for your attention