




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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 19, 2009 10:51 AM 2
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
4
5
audio CD ! y ( i ) = sin 2 "^ #^ i^ #^440 44 , 100 $ %^ &^ ' (^ ) 6
7
8
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
14
3757208/ 15
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
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; } } }
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; } } }
% 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; } } }
% 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; } } }
% 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(); } }
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(); } }
% 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
% 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 <= 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
29