







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The basics of programming, focusing on control flow structures such as if statements and while loops, as well as mathematical operations. It includes examples and exercises for practicing these concepts.
Typology: Slides
1 / 13
This page cannot be seen from the preview
Don't miss anything!








Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · February 10, 2009 10:07 PM 2
objects functions and modules graphics, sound, and image I/O arrays conditionals and loops Math text I/O primitive data types assignment statements equivalent to a calculator any program you might want to write 3
objects functions and modules graphics, sound, and image I/O arrays any program you might want to write to infinity conditionals and loops and beyond! Math text I/O primitive data types assignment statements 4
statement 2 statement 1 statement 4 statement 3 boolean 2^ true false statement 2 boolean 1 statement 3 false statement 1 true straight-line control flow control flow with conditionals and loops
6
if ( boolean expression ) { statement T ; } else { statement F ; } can be any sequence of statements statement T true false boolean expression statement F 7
8
public class Flip { public static void main(String[] args) { if (Math.random() < 0.5) System.out.println("Heads"); elseMath.random() < 0.5) System.out.println("Tails"); } } % java Flip Heads % java Flip Heads % java Flip Tails % java Flip Heads
13
public class PowersOfTwo { public static void main(String[] args) { // last power of two to print int N = Integer.parseInt(args[ 0 ]); int i = 0 ; // loop control counter int v = 1 ; // current power of two while (i <= N) { System.out.println(i + " " + v); i = i + 1 ; v = 2 * v; } } } % java PowersOfTwo 4 0 1 1 2 2 4 3 8 % java PowersOfTwo 6 0 1 1 2 2 4 3 8 4 16 5 32 6 64 print i and ith power of two 14
int i = 0 ; int v = 1 ; while (i <= N) System.out.println(i + " " + v); i = i + 1 ; v = 2 * v; 16
Copyright 2004, Sidney Harris, http://www.sciencecartoonsplus.com % java Sqrt 60481729
17
t 0 = 2. t 1 = 12 ( t 0 + (^) t^20 ) = 1. t 2 = 12 ( t 1 + (^2) t 1 ) = 1. t 3 = 12 ( t 2 + (^) t^22 ) = 1. t 4 = 12 ( t 3 + (^) t^23 ) = 1. t 5 = 12 ( t 4 + (^) t^24 ) = 1. computing the square root of 2
18 public class Sqrt { public static void main(String[] args) { double epsilon = 1e-15; double c = Double.parseDouble(args[ 0 ]); double t = c; while (Math.abs(t - c/t) > t*epsilon) { t = (c/t + t) / 2.0; } System.out.println(t); } } (^) % java Sqrt 2.
relative error tolerance 15 decimal digits of accuracy in 5 iterations
19
f(x) = x^2 - c to compute "c 20
Copyright 2004, FoxTrot by Bill Amend www.ucomics.com/foxtrot/2003/10/ 21
for ( init ; boolean expression ; increment ) { statement 1 ; statement 2 ; } statement 1 true false boolean expression statement 2 init increment body loop continuation condition
26
27
Conditionals enable you to do one of 2n sequences of operations with n lines.
if (a0 > 0 ) System.out.print( 0 ); if (a1 > 0 ) System.out.print( 1 ); if (a2 > 0 ) System.out.print( 2 ); if (a3 > 0 ) System.out.print( 3 ); if (a4 > 0 ) System.out.print( 4 ); if (a5 > 0 ) System.out.print( 5 ); if (a6 > 0 ) System.out.print( 6 ); if (a7 > 0 ) System.out.print( 7 ); if (a8 > 0 ) System.out.print( 8 ); if (a9 > 0 ) System.out.print( 9 ); Loops enable you to do an operation n times using only 2 lines of code. double sum = 0.0; for (int i = 1 ; i <= 1024 ; i++) sum = sum + 1.0 / i; 210 = 1024 possible results, depending on input computes 1/1 + 1/2 + ... + 1/ 28
double rate; if (income < 47450 ) rate = 0.22; else if (income < 114650 ) rate = 0.25; else if (income < 174700 ) rate = 0.28; else if (income < 311950 ) rate = 0.33; else if (income < 311950 ) rate = 0.35; graduated income tax calculation 0 - 47,450 22% Income Rate 47,450 – 114,650 25% 114,650 – 174,700 28% 174,700 – 311,950 33% 311,950 - 35% 5 mutually exclusive alternatives 29
if (income < 47450 ) rate = 0.22; else if (income < 114650 ) rate = 0.25; else if (income < 174700 ) rate = 0.28; else if (income < 311950 ) rate = 0.33; else if (income < 311950 ) rate = 0.35; if (income < 47450) rate = 0.22; else { if (income < 114650) rate = 0.25; else { if (income < 174700) rate = 0.28; else { if (income < 311950) rate = 0.33; else if (income < 311950) rate = 0.35; } } }
30
double rate = 0.35; if (income < 47450 ) rate = 0.22; if (income < 114650 ) rate = 0.25; if (income < 174700 ) rate = 0.28; if (income < 311950 ) rate = 0.33; wrong graduated income tax calculation 0 - 47,450 22% Income Rate 47,450 – 114,650 25% 114,650 – 174,700 28% 174,700 – 311,950 33% 311,950 - 35% 31
32
33 public class Gambler { public static void main(String[] args) { int stake = Integer.parseInt(args[ 0 ]); int goal = Integer.parseInt(args[ 1 ]); int T = Integer.parseInt(args[ 2 ]); int wins = 0 ; System.out.println(wins + " wins of " + T); } } // repeat experiment N times for (int t = 0 ; t < T; t++) { } // do one gambler's ruin experiment int cash = stake; while (cash > 0 && cash < goal) { } if (cash == goal) wins++; // flip coin and update if (Math.random() < 0.5) cash++; else cash--;
38
“ If I had eight hours to chop down a tree, I would spend six hours sharpening an axe. ” — Abraham Lincoln “As soon as we started programming, we found out to our surprise that it wasn't as easy to get programs right as we had thought. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs. ” — Maurice Wilkes 39
40
3757208/ 41
public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[ 0 ]) for (i = 0 ; i < N; i++) { while (N % i == 0 ) System.out.print(i + " ") N = N / i } } } this program has many bugs! as long as i is a factor, divide it out check if i is a factor
42 public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[ 0 ]) for (i = 0 ; i < N; i++) { while (N % i == 0 ) System.out.print(i + " ") N = N / i } } } % javac Factors.java Factors.java:6: ';' expected for (i = 2; i < N; i++) ^ 1 error
the first error 43 public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[ 0 ]); for (int i = 0 ; i < N; i++) { while (N % i == 0 ) System.out.print(i + " "); N = N / i; } } }
syntax (compile-time) errors need to declare variable i need terminating semicolons 44 public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[ 0 ]); for (int i = 0 ; i < N; i++) { while (N % i == 0 ) System.out.print(i + " "); N = N / i; } } }
% javac Factors.java % java Factors Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Factors.main(Factors.java:5) oops, no argument 45 public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[ 0 ]); for (int i = 0 ; i < N; i++) { while (N % i == 0 ) System.out.print(i + " "); N = N / i; } } }
% javac Factors.java % java Factors 98 Exception in thread "main" java.lang.ArithmeticExeption: / by zero at Factors.main(Factors.java:8) need to start at 2 because 0 and 1 cannot be factors
50
public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[ 0 ]); for (int i = 2 ; i < N; i++) { while (N % i == 0 ) { System.out.print(i + " "); N = N / i; } } if (N > 1) System.out.println(N); else System.out.println(); } }
% java Factors 11111111 11 73 11 137 % java Factors 11111111111 21649 51329 % java Factors 11111111111111 11 239 4649 909091 % java Factors 11111111111111111 2071723 very long wait (with a surprise ending) 51 public class Factors { public static void main(String[] args) { long N = Long.parseLong(args[ 0 ]); for (int i = 2 ; i <= N/i; i++) { while (N % i == 0 ) { System.out.print(i + " "); N = N / i; } } if (N > 1) System.out.println(N); else System.out.println(); } }
% java Factors 11111111 11 73 11 137 % java Factors 11111111111 21649 51329 % java Factors 11111111111111 11 239 4649 909091 % java Factors 11111111111111111 2071723 5363222357 fixes performance error: if N has a factor, it has one less than or equal to its square root 52
% java Factors 3757208 2 2 2 7 13 13 397 % java Factors 9201111169755555703 9201111169755555703
† estimated largest factor (^3) instant digits (i^ <=^ N) 6 0.15 seconds 9 77 seconds 12 21 hours † instant (i*i <= N) instant instant 0.16 seconds 15 2.4 years † 18 2.4 millennia † 2.7 seconds 92 seconds after a few minutes of computing…. 53