Programming Basics. Digital Urban Visualization. People as Flows. ia

Similar documents
JAVA OPERATORS GENERAL

Java Simple Data Types

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Java Simple Data Types

Key Java Simple Data Types

Prof. Navrati Saxena TA: Rochak Sachan

Selenium Class 9 - Java Operators

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

PROGRAMMING FUNDAMENTALS

Building Java Programs

Building Java Programs

Object-Oriented Programming

Fall CS 101: Test 2 Name UVA ID. Grading. Page 1 / 4. Page3 / 20. Page 4 / 13. Page 5 / 10. Page 6 / 26. Page 7 / 17.

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

Topic 4 Expressions and variables

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

CS 106 Introduction to Computer Science I

Software and Programming 1

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

CS 231 Data Structures and Algorithms, Fall 2016

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Datatypes, Variables, and Operations

AP COMPUTER SCIENCE A

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

More on methods and variables. Fundamentals of Computer Science Keith Vertanen

Building Java Programs

Java Tutorial. Saarland University. Ashkan Taslimi. Tutorial 3 September 6, 2011

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

Introduction to the Java Basics: Control Flow Statements

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Lec 3. Compilers, Debugging, Hello World, and Variables

CS 455 Midterm Exam 1 Fall 2016 [Bono] Thursday, Sept. 29, 2016

Course Outline. Introduction to java

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 88 / 133

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

Java Basic Programming Constructs

CIS133J. Working with Numbers in Java

Lecture Set 4: More About Methods and More About Operators

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Arithmetic Compound Assignment Operators

Lesson 26: ArrayList (W08D1)

Fall 2017 CISC124 9/16/2017

Object-oriented programming in...

Software and Programming 1

CSE 142, Spring 2009, Sample Final Exam #2. Good luck!

Logic is the anatomy of thought. John Locke ( ) This sentence is false.

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

Introduction Basic elements of Java

JAVA PROGRAMMING LAB. ABSTRACT In this Lab you will learn to define and invoke void and return java methods

Program Fundamentals

COMP 202 Java in one week

1 class Lecture3 { 2 3 "Selections" // Keywords 8 if, else, else if, switch, case, default. Zheng-Liang Lu Java Programming 89 / 137

Java Programming for Selenium

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Getting started with Java

Getting started with Java

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

CS251L REVIEW Derek Trumbo UNM

Zheng-Liang Lu Java Programming 45 / 79

For that purpose, java provides control structures that serve to specify what has to be done by our program, when and under which circumstances.

Java Programming Fundamentals - Day Instructor: Jason Yoon Website:

Introduction to Java

CompSci 125 Lecture 02

COMPUTER PROGRAMMING LOOPS

CIS October 16, 2018

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

1. Introduction to Java for JAS

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Computer Components. Software{ User Programs. Operating System. Hardware

CS 11 java track: lecture 1

Chapter 2: Data and Expressions

Scanner Objects. Zheng-Liang Lu Java Programming 82 / 133

McGill University School of Computer Science COMP-202A Introduction to Computing 1

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CS 251 Intermediate Programming Java Basics

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Java is an objet-oriented programming language providing features that support

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Lecture Set 4: More About Methods and More About Operators

1.00 Lecture 5. Floating Point Anomalies

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Eng. Mohammed S. Abdualal

Chapter 4 Control Structures

The Java language has a wide variety of modifiers, including the following:

1.00 Lecture 6. Java Methods

Introduction to OOP with Java. Instructor: AbuKhleif, Mohammad Noor Sep 2017

ICOM 4015 Advanced Programming Laboratory. Chapter 1 Introduction to Eclipse, Java and JUnit

AP Computer Science Homework Set 1 Fundamentals

Java Programming Language. 0 A history

Java Programming. Atul Prakash

Day 2 : Intermediate Concepts 1 Examples

Transcription:

Programming Basics Digital Urban Visualization. People as Flows. 28.09.2015 ia zuend@arch.ethz.ch treyer@arch.ethz.ch

Programming? Programming is the interaction between the programmer and the computer. The computer evaluates the formal text you write and executes the instructions of the text.

Programming? Programming means to solve problems and to give the instructions on how to solve them to the computer. Sorting: [9,2,8,1,7] [1,2,7,8,9]

From Text Files to an Application simplified explanation Java is a high-level programming language. This means, we can write a human readable set of instructions. But an interpreter/compiler is needed to translate the text (a.k.a. code) to a computer readable format. Java source code Java compiler Java byte code

Object Oriented Programming In the real world, you ll often find many individual objects all of the same kind. There may be thousands of other bicycles in existence, all of the same make and model. Each bicycle was built from the same set of blueprints and therefore contains the same components. In object-oriented terms, we say that your bicycle is an instance of the class of objects known as bicycles. A class is the blueprint from which individual objects are created. Example by: docs.oracle.com

Where it all Begins the main function The compiler needs to know, where to begin a program. This is defined by a function called main, i.e. public static void main(string[] args) { Last week we added this function automatically when we created the HelloWorld program. The compiler knows the main keyword and will start from there.

Values and Primitive Data Types Values are for example: 3.1415, 23, or Hello World!. Each value has a type and we can use variables of specific primitive data types to store values. The three main data types we will be using are: int: int (or integers) are whole numbers. double: doubles are real numbers. char: is any character. We will normally use a more advanced data structure called string which can be understood as a list of characters. boolean: has a value of either true or false.

Values and Data Types assign values to variables A variable is a name that refers to a value. In Java, you first have to define the variable and its type. When you use an assignment statement (=), Java will set the variable to the corresponding value. This accounts for any type (primitive data types and instances of classes). int a; a = 23; System.out.println(a); double b; b = 3.1415; System.out.println(b); boolean c; c = true; System.out.println(c); char d; d = a ; System.out.println(d); string e; e = Hello World! ; System.out.println(e);

Operators Operators represent a computation you want to do with values or variables. For example the operators +, *, -, / perform addition, multiplication, subtraction and division, respectively. It is important to ensure that the data type is correct when you use such an operation. double a, b, c; a = 1; b = 2; c = a / b; System.out.println(c); Output:? Output:? int a, b, c; a = 1; b = 2; c = a / b; System.out.println(c);

Operators Operators represent a computation you want to do with values or variables. For example the operators +, *, -, / perform addition, multiplication, subtraction and division, respectively. It is important to ensure that the data type is correct when you use such an operation. double a, b, c; a = 1; b = 2; c = a / b; System.out.println(c); Output: 0.5 Output:? int a, b, c; a = 1; b = 2; c = a / b; System.out.println(c);

Operators Operators represent a computation you want to do with values or variables. For example the operators +, *, -, / perform addition, multiplication, subtraction and division, respectively. It is important to ensure that the data type is correct when you use such an operation. double a, b, c; a = 1; b = 2; c = a / b; System.out.println(c); Output: 0.5 Output: 0 int a, b, c; a = 1; b = 2; c = a / b; System.out.println(c);

Lists There exist many types of lists in Java. We will use the ArrayList. To work with a list, first initialize a variable of the ArrayList type. ArrayList<double> mylist; The list can only contain one type of object/data types, thus we have to define it at initialisation, e.g. double above. To add elements, we use the add function of ArrayLists. This appends 2.3 to the end of the list. mylist.add(2.3);

Lists To add elements, we use the add function of ArrayLists. mylist.add(2.3); This appends 2.3 to the end of the list. To access an element in the list, we use the get functionality. double a; a = mylist.get(0); a has stored the value 2.3 now. Be aware that counting normally starts at zero in programming, thus the first element in the list has the index 0.

Lists To access an element in the list, we use the get functionality. double a; a = mylist.get(0); a has stored the value 2.3 now. Be aware that counting normally starts at zero in programming, thus the first element in the list has the index 0. It is also possible to delete elements for this, ArrayList has the remove functionality. mylist.remove(0); Now the list is empty again. There are many other functionalities for ArrayList, check the internet, e.g. [1]. [1]: docs.oracle.com/javase/8/docs/api/java/util/arraylist.html

Boolean Expressions Boolean expressions are either true or false. The result is of type Boolean. They are most often used to check for conditions in conditional statements. Most of the time, relational operators are used. Relational operators: ==,!=, <, >, <=, >= They stand for: equal, not equal, smaller, greater, smaller-equal, greater-equal. Do not confuse = with ==, the first is the assignment operator, the other one the relational operator!

Logical Operators Logical operators are or ( ), and (&&) and not (!). They are used to construct logical expressions. The following examples make the concept very understandable, if you read them with an is at the beginning and formulate them as a question. x < 2 && x >= 0 x == 0 x!= 0!(x == 0 x > 100)

Logical Operators Logical operators are or ( ), and (&&) and not (!). They are used to construct logical expressions. The following examples make the concept very understandable, if you read them with an is at the beginning and formulate them as a question. x < 2 && x >= 0 x == 0 x!= 0!(x == 0 x > 100) true if x between 0 and 2, excluding 2. always true true if x is smaller then 0 or if it is between 0 and 100, excluding 0.

Conditional Statements Conditional Statements are an important construct in programming, since we always need to check some condition and let the program changing its behavior accordingly. Behavior I Check Condition Behavior II

Conditional Statements The simplest form of a conditional statement is the if statement. It checks if some condition is fulfilled and executes the code if it is true. if (avalue < 2) { System.out.println( Hello World! );

Conditional Statements When you want the program to execute an alternative, if a condition is not met you can add the else statement. if (avalue < 2) { System.out.println( Hello World! ); else { System.out.println( Goodbye World! );

Conditional Statements It is also possible to chain conditional statements using the else if statement. if (avalue < 2) { System.out.println( Hello World! ); else if (avalue > 99) { System.out.println( Wow! ); else { System.out.println( Goodbye World! );

Loops The second important construct in programming is loops. This makes it possible to repeat a statement multiple times. There are two different kinds of loops, for loops and while loops.

while Loop A while loop executes the corresponding sequence of instructions until some condition is not fulfilled anymore. int a = 1; while (a < 16) { a = a + a; System.out.println(a); 2 4 8 16

for Loop A for loop allows you to repeat a task a specific number of times. for(initialization; condition; update) { statements Image source: www.tutorialspoint.com

for Loop for (int x = 0; x < 5; x = x + 1) { System.out.println(x); Image source: www.tutorialspoint.com

for Loop for (int x = 0; x < 5; x = x + 1) { System.out.println(x); 0 1 2 3 4 Image source: www.tutorialspoint.com

What is the difference? for (int x = 0; x < 4; x = x + 1) { System.out.println(x); int x = 0; while (x < 4) { x = x + 1; System.out.println(x);

What is the difference? for (int x = 0; x < 4; x = x + 1) { System.out.println(x); 0 1 2 3 int x = 0; while (x < 4) { x = x + 1; System.out.println(x);

What is the difference? for (int x = 0; x < 4; x = x + 1) { System.out.println(x); 0 1 2 3 int x = 0; while (x < 4) { x = x + 1; System.out.println(x); 1 2 3 4

Loop Keywords Very useful keywords when working with loops are break and continue. break terminates the loop and resumes after the loop construct. continue terminates the current iteration and returns to the beginning of the loop.

f o r ( i n t a = 0; a < 10; a = a + 1 ) { i f ( a < 3 ) { System. out. p r i n t l n ( a * 1 0 0 ) ; else i f ( a == 5 ) { continue ; else i f ( a > 7 ) { break ; else { System. out. p r i n t l n ( a ) ;

f o r ( i n t a = 0; a < 10; a = a + 1 ) { i f ( a < 3 ) { System. out. p r i n t l n ( a * 1 0 0 ) ; else i f ( a == 5 ) { continue ; else i f ( a > 7 ) { break ; else { System. out. p r i n t l n ( a ) ; 0 100 200 3 4 6 7

Exercise I In the first exercise, you will need to use conditional statements as well as loops. Please send the code you programmed to Dani (zuend@arch.ethz.ch) by next Sunday. This means to send the.java file located in yourworkspace yourprojectname src with all the code to Dani. You can check if it the right file with every text editor. Put all your code in the main method, the same location we put the System.out.println( Hello World! ); last week.

Exercise I information It is not allowed to hardcode the solutions, you should print only one character per time and use loops and conditional statements! ### 00 # The modulo operator (%) can be very useful for this exercise. It calculates the remainder of a division. This can be used, e.g., to find out very fast if a number is odd or even. For example 7%4 == 3; 8%2 == 0; 8%3 == 2

public s t a t i c void main ( S t r i n g [ ] args ) { f o r ( i n t i = 3; i >= 0; i = i 1) { char symbol ; i f ( i%2 == 1 ) { symbol = # ; else { symbol = 0 ; f o r ( i n t j = 0; j < i ; j = j + 1 ) { System. out. p r i n t ( symbol ) ; System. out. p r i n t l n ( ) ; ### 00 #

Editor errors and warnings The editor helps to find errors in your code. To find out what it is, hover with the mouse over the error symbol and it will give you a hint what could be wrong. The same accounts for warnings, with the yellow symbol.

Other Helpers debugging & resources The most important helpers for larger projects are two, the internet and the debugging mode. When looking for something specific for Java programming, search in your favourite search engine, beginning with Java and then your question. When you cannot figure out what is wrong with your code, check the Internet for Eclipse Java Debugging to find for example this tutorial: http://www.vogella.com/tutorials/eclipsedebugging/article.html