Download presentation
Presentation is loading. Please wait.
Published byBryan Ward Modified over 9 years ago
1
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49, Room G.2, lcederman@ethz.chlcederman@ethz.ch Nils Weidmann, CIS Room E.3 girardin@icr.gess.ethz.chgirardin@icr.gess.ethz.ch http://www.icr.ethz.ch/teaching/compmodels Lecture, November 9, 2004 Java Primer I
2
2 Today’s agenda Introduction to Java –Historical background –Basic features –Syntax –Homework A
3
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
4 The Java solution to platform- independence myProgram.java ------ ----- -- -------- myProgram.class ------ ----- -- -------- Compiler Interpreter Windows 010111001 Mac 010111001 Interpreter old, platform- dependent method
5
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
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
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
8 Primitive types: doubles double a = 3.14; double b = 12.7; a = a / 2.0 + (b * 2.2 - 1.9)*3.2; a++; // same as a = a + 1.0; b--; // same as b = b - 1.0; Operators: +, -, *, /, ++, --
9
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
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
11 7 Mini-Poker Pair Three of a kind 74 777 a b c
12
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
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
14 Output: 1 2 3 4 5 * ** *** **** *****
15
15 Fibonacci's series 1 1 2 3 5 8
16
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
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
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.= 0.478395... 1,000,000 replications: 0.478189
19
19 Russian Roulette: Code public class RussianRoulette { public static void main(String[] args) { int n = 1000000; //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
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
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: 1 2 6 24...
22
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
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.