Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
10-Jun-15 Introduction to Primitives. 2 Overview Today we will discuss: The eight primitive types, especially int and double Declaring the types of variables.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 5: Program Logic and Indefinite Loops.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
Some basic I/O.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Java An introduction. Example 1 public class Example1 { public static void main (String [] args) { System.out.println (“This is the first example”); int.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
A Short Introduction to JAVA
CS107 Introduction to Computer Science Java Basics.
G51PRG-Sem2 A Reality Check a sort of quiz Dave Elliman.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
CSE 131 Computer Science 1 Module 1: (basics of Java)
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
“Introduction to Programming With Java”
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
EXAM 1 REVIEW. days until the AP Computer Science test.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
SOFTWARE TECHNOLOGY - I JAVA/OOP Wickramanayake HMKSK Department of Electrical & Electronic Engineering Faculty of Engineering University.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Repetition Statements while and do while loops
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Prof. Lars-Erik Cederman ETH - Center for Comparative and International Studies (CIS) Seilergraben 49, Room G.2, Nils.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
Building java programs, chapter 3 Parameters, Methods and Objects.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Introduction to Java John Lewis. Course Overview Introduction Object Orientated Programming Java Structure and Syntax Using the Java Platform Advanced.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
int [] scores = new int [10];
Introduction to Computational Modeling of Social Systems Nils Weidmann Center for Comparative and International Studies (CIS) Seilergraben 49, Room E.3,
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Object Oriented Programming Lecture 2: BallWorld.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
Intro to Programming STARS College of Communication and Information Florida State University Written by: Hannah Brock Alissa Ovalle Nicolaus Lopez Martin.
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Chapter No. : 1 Introduction to Java.
Lecture 5: Some more Java!
Data types and variables
Programming Language Concepts (CIS 635)
An Introduction to Java – Part I
Writing Methods.
OPERATORS (2) CSC 111.
The for-loop and Nested loops
An Introduction to Java – Part I, language basics
Java so far Week 7.
int [] scores = new int [10];
CIS 110: Introduction to Computer Programming
Introduction to Primitives
Introduction to Primitives
Names of variables, functions, classes
Building Java Programs
Review of Previous Lesson
Presentation transcript:

Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49, Room G.2, Nils Weidmann, CIS Room E.3 Lecture, November 9, 2004 Java Primer I

2 Today’s agenda Introduction to Java –Historical background –Basic features –Syntax –Homework A

3 The origins of Java Conceived by Sun in the early 1990s Became the new standard for the web thanks to platform-independence C C++ syntax object model simple object-oriented robust portable secure dynamic

4 The Java solution to platform- independence myProgram.java myProgram.class Compiler Interpreter Windows Mac Interpreter old, platform- dependent method

5 First example public class TwoPlusTwo { public static void main (String args[]) { int a = 2; int b = 2; int c; c = a + b; System.out.println(“Two plus two is “ + c); } declaration and initialization declaration assignment main method

6 int a = 12; double d = 3.14; boolean b = false; char c = 'X'; Primitive types hold variables rather than objects Never mix types (except in print statements)! More on primitive types in Eckel Ch. 2 or Schildt Module 2! Primitive types

7 int a = 12; int b = -3; a = a/4 + (b*2 - 1)*3; a++; // same as a = a + 1; b--; // same as b = b - 1; Operators: +, -, *, /, ++, -- For more, see Eckel ch. 3 Primitive types: integers

8 Primitive types: doubles double a = 3.14; double b = 12.7; a = a / (b * )*3.2; a++; // same as a = a + 1.0; b--; // same as b = b - 1.0; Operators: +, -, *, /, ++, --

9 boolean a = false; boolean b = true; a = (a && b) || true; a = !b; b = (3 > 2); a = (2.0 == 2.0); Logical operators: !, &&, || Relational operators: ==, !=, >, =, <= Primitive types: booleans

10 If-statements if (a > 2) { a = 0; } if (!x) { a = 0; } else { a = -1; b = 2; } if (!x && (a == b)) { a = 0; } else if (c > d) { a = -1; b = 2; } condition statement NOT ‘=‘ !

11 7 Mini-Poker Pair Three of a kind a b c

12 Mini-Poker: Code public class Minipoker { public static void main (String args[]) { int a = 3; int b = 2; int c = 3; if ((a == b) && (a == c)) System.out.println("Three of a kind!"); else if ((a == b) || (a == c) || (b == c)) System.out.println("Pair!"); else System.out.println(“Nothing!"); }

13 For-loops int i; for (i = 0; i < 5; i++) { System.out.println(i); } for (int i = 0; i < 5; i++) { for (int j = 0; j <= i; j++) { System.out.print("*"); } System.out.println(); } initialization condition iteration body of loop Note that loop variables are only visible inside loop body!

14 Output: * ** *** **** *****

15 Fibonacci's series

16 Fibonacci: Code public class Fibonacci { public static void main (String args[]) { int n = 10; int x = 1; int y = 1; int z; System.out.print("1 1 "); for (int i = 0; i < n; i++) { z = x + y; x = y; y = z; System.out.print(z + " "); }

17 Example: Russian Roulette A B A B A 1/6 5/6 2/6 4/6 3/6 4/6 B 5/6 3/6 2/6 1/6

18 Russian Roulette (cont’d) 0: A B 1: A B A B 2: A B 3: A B 4: A 5: A B 6: A B A B A 7: A B A B 8: A B 9: A B A B 10: A B A B A B 11: A B A 12: A B 13: A B A 14: A B 15: A B A B 16: A B A B... A's survival prob.= ,000,000 replications:

19 Russian Roulette: Code public class RussianRoulette { public static void main(String[] args) { int n = ; //the number of replications int sum = 0; //the number of replications where player 0 dies Random rand = new Random(); //the random number generator for (int replications = 0; replications < n; replications++) { int player = 1; int i = 0; boolean shot = false; do { i++; player = Math.abs(player-1); shot = rand.nextDouble() < (double) i / 6.0; } while (!shot); if (player == 0) sum++; } double survivalProb0 = 1 - (double) sum / n; System.out.println("Player 0's probability of surviving is "+survivalProb0); }

20 Exercise A1 (Primer I) Using a double for-loop, write a program that prints a cross on the screen: x x x x x x x

21 Exercise A2 (Primer I) Write a program that prints a series of factorials, n! = 1 x 2 x 3 x... x n for n = 1 to 10:

22 Exercise A3 (Primer I) a) Modify the Mini-Poker program so that it also recognizes a straight (e.g. three cards in rank order) assuming that the cards are sorted from smaller to larger (a < b < c). b*) Modify the Mini-Poker program so that three random cards (with values 1 through 9) are drawn and that a straight can be recognized.

23 Exercise A4 (Primer II) Write a program featuring a Die class with an instance variable holding the number of dots and methods for casting and retrieving the number of dots. The main program should cast two dice until two sixes appear. Tip: Use a while loop.