Docsity

Java Programming: I/O, Functions, and Debugging, Slides of Computer Programming

Toggle sidebar
bg1
I/O (cont) and Program Development
Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · February 19, 2009 10:51 AM
2
A Foundation for Programming
objects
functions and modules
graphics, sound, and image I/O
arrays
conditionals and loops
Math text I/O
assignment statementsprimitive data types
what you've
done already
any program you might want to write
Standard Audio
4
Crash Course in Sound
Sound. Perception of the vibration of molecules in our eardrums.
Concert A. Sine wave, scaled to oscillate at 440Hz.
Other notes. 12 notes on chromatic scale, divided logarithmically.
docsity.com
pf3
pf4
pf5
pf8

Partial preview of the text

Download Java Programming: I/O, Functions, and Debugging and more Slides Computer Programming in PDF only on Docsity!

I/O (cont) and Program Development

Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · February 19, 2009 10:51 AM 2

A Foundation for Programming

objects functions and modules graphics, sound, and image I/O arrays conditionals and loops Math text I/O primitive data types assignment statements what you've done already any program you might want to write

Standard Audio

4

Crash Course in Sound

Sound. Perception of the vibration of molecules in our eardrums.

Concert A. Sine wave, scaled to oscillate at 440Hz.

Other notes. 12 notes on chromatic scale, divided logarithmically.

5

Digital Audio

Sampling. Represent curve by sampling it at regular intervals.

audio CD ! y ( i ) = sin 2 "^ #^ i^ #^440 44 , 100 $ %^ &^ ' (^ ) 6

Standard Audio

Standard audio. Library for playing digital audio.

7

Play That Tune

Goal. Read in pitches and durations from standard input,

and play using standard audio.

8

Warmup: Musical Tone

Musical tone. Create a music tone of a given frequency and duration.

public class Tone { public static void main(String[] args) { int sps = 44100 ; double hz = Double.parseDouble(args[ 0 ]); double duration = Double.parseDouble(args[ 1 ]); int N = (int) (sps * duration); double[] a = new double[N+ 1 ]; for (int i = 0 ; i <= N; i++) a[i] = Math.sin( 2 * Math.PI * i * hz / sps); StdAudio.play(a); } } ! y ( i ) = sin 2 "^ #^ i^ #^ hz 44 , 100 $ %^ &^ ' (^ ) % java Note 440 1. [ concert A for 1.5 seconds]

13

Debugging Example

Factor. Given an integer N > 1, compute its prime factorization.

Application. Break RSA cryptosystem (factor 200-digit numbers).

3,757,208 = 2^3 × 7 × 132 × 397

98 = 2 × 72

11,111,111,111,111,111 = 2,071,723 × 5,363,222,

14

Debugging Example

Factor. Given an integer N > 1, compute its prime factorization.

Brute-force algorithm. For each putative factor i = 2, 3, 4, …,

check if N is a multiple of i, and if so, divide it out.

3757208/ 15

Debugging: 95% of Program Development

Programming. A process of finding and fixing mistakes.

 Compiler error messages help locate syntax errors.

 Run program to find semantic and performance errors.

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 16 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:4: ';' expected for (i = 0; i < N; i++) ^ 1 error

Debugging: Syntax Errors

Syntax error. Illegal Java program.

 Compiler error messages help locate problem.

 Goal: no errors and a file named Factors.class.

the first error

17 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; } } }

Debugging: Syntax Errors

Syntax error. Illegal Java program.

 Compiler error messages help locate problem.

 Goal: no errors and a file named Factors.class.

syntax (compile-time) errors need to declare variable i need terminating semicolons 18 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; } } }

Debugging: Semantic Errors

Semantic error. Legal but wrong Java program.

 Run program to identify problem.

 Add print statements if needed to produce trace.

% javac Factors.java % java Factors Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Factors.main(Factors.java:5) oops, no argument 19 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; } } }

Debugging: Semantic Errors

Semantic error. Legal but wrong Java program.

 Run program to identify problem.

 Add print statements if needed to produce trace.

% 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 20 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; } } }

Debugging: Semantic Errors

Semantic error. Legal but wrong Java program.

 Run program to identify problem.

 Add print statements if needed to produce trace.

% javac Factors.java % java Factors 13 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 … infinite loop! indents do not imply braces

25 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; } } System.out.println(); } }

Performance error. Correct program, but too slow.

Solution. Improve or change underlying algorithm.

Debugging: Performance Error

fixes performance error: if N has a factor, it has one less than or equal to its square root % java Factors 98 2 7 7 % java Factors 11111111 11 73 101 % java Factors 11111111111111 11 239 4649 % java Factors 11111111111111111 2071723 missing last factor (sometimes) 26 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(); } }

Caveat. Optimizing your code tends to introduce bugs.

Lesson. Don't optimize until it's absolutely necessary.

Debugging: Performance Error

% java Factors 11111111 11 73 101 137 % java Factors 11111111111 21649 51329 % java Factors 11111111111111 11 239 4649 909091 % java Factors 11111111111111111 2071723 5363222357 need special case to print biggest factor (unless it occurs more than once) "corner case" 27

Q. How large an integer can I factor?

Note. Can't break RSA this way (experts are still trying).

% java Factors 3757208 2 2 2 7 13 13 397 % java Factors 9201111169755555703 9201111169755555703

Program Development: Analysis

† estimated largest factor (^3) instant digits (i^ <=^ N) 6 0.15 seconds 9 77 seconds 12 21 hours † instant (i <= N/i) instant instant 0.16 seconds 15 2.4 years † 18 2.4 millennia † 2.7 seconds 92 seconds after a few minutes of computing…. 28

Debugging

Programming. A process of finding and fixing mistakes.

1. Create the program.

2. Compile it.

Compiler says: That’s not a legal program.

Back to step 1 to fix syntax errors.

3. Execute it.

Result is bizarrely (or subtly) wrong.

Back to step 1 to fix semantic errors.

4. Enjoy the satisfaction of a working program!

5. Too slow? Back to step 1 to try a different algorithm.

29

U.S.S. Grace Murray Hopper