Outlne CIS 110: Introducton to Computer Programmng More on Cumulatve Algorthms Processng Text Tacklng Programmng Problems Lecture 11 Text Processng and More On Desgn ( 4.2-4.3) 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 1 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 2 Revew: Interactve Sum More Cumulatve Algorthms 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 3 Can you wrte a program that computes the sum of numbers from 0 to the user's nput mnus 1? Scanner n = new Scanner(System.n); System.out.prnt("n? "); nt n = n.nextint(); System.out.prntln(); Some storage declared outsde the loop. nt sum = 0; for (nt = 0; < n; ++) { sum += ; Update the storage nsde of the loop. System.out.prntln( "Sum of 1 to " + Use the updated storage after the loop. 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 4 Interactve Sum Trace (1) Interactve Sum Trace (2) n 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 5 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 6 CIS 110 (11fa) - Unversty of Pennsylvana 1
Interactve Sum Trace (3) Interactve Sum Trace (4) n n n 5 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 7 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 8 Interactve Sum Trace (5) Interactve Sum Trace (6) n n 5 n n 5 sum 0 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 9 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 10 Interactve Sum Trace (7) Interactve Sum Trace (8) 0 0 n n 5 sum 0 n n 5 sum 0 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 11 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 12 CIS 110 (11fa) - Unversty of Pennsylvana 2
Interactve Sum Trace (9) Interactve Sum Trace (10) 1 1 n n 5 sum 0 n n 5 sum 0 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 13 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 14 Interactve Sum Trace (11) Interactve Sum Trace (12) 1 2 n n 5 sum 1 n n 5 sum 1 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 15 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 16 Interactve Sum Trace (13) Interactve Sum Trace (13) 2 3 n n 5 sum 3 n n 5 sum 3 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 17 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 18 CIS 110 (11fa) - Unversty of Pennsylvana 3
Interactve Sum Trace (14) Interactve Sum Trace (15) 3 4 n n 5 sum 6 n n 5 sum 6 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 19 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 20 Interactve Sum Trace (16) Interactve Sum Trace (17) 4 5 n n 5 sum 10 n n 5 sum 10 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 21 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 22 Interactve Sum Trace (18) n n 5 sum 10 Cumulatve Algorthms and Condtonals Problem: calculate the mnmum of 10 numbers entered by the user. publc statc nt promptfornumber(scanner n, nt ) { System.out.prnt("Enter number " + + ": "); return n.nextint(); publc statc vod man(strng[] args) { Scanner n = new Scanner(System.n); nt mn = promptfornumber(n, 1); // Storage for (nt = 2; <= 10; ++) { nt num = promptfornumber(n, ); // Modfy storage f (num < mn) { mn = num; // Use storage System.out.prntln("The mnmum s " + mn + "."); 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 23 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 24 CIS 110 (11fa) - Unversty of Pennsylvana 4
Recall: charat(ndex) Text Processng Scanner n = new Scanner(System.n); Strng lne = n.nextlne(); for (nt = 0; < lne.length(); ++) { // charat() returns the th character of the strng // Remember strng ndces start at 0... System.out.prntln(lne.charAt()); 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 25 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 26 The char Type A prmtve that represents a sngle character. Represented as a 16-bt nteger (character value). All our prmtve operatons on numbers apply! char c1 = 'c'; f (c1 == 'c') { System.out.prntln(c1 + " s c!"); // Int + Char = Int whch s 32 bts, so we need to // cast to tell the compler we don't mnd the truncaton. char c2 = (char) (c1 + 5); System.out.prntln("5 chars from " + c1 + " s " + c2); // Tests to see f c2 s a lowercase number. f (c2 >= 'a' c2 <= 'z') { System.out.prntln(c2 + " s between 'a' and 'z'!"); 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 27 The Character class Helpful statc methods for dealng wth chars. nt fve = Character.getNumercValue('5'); System.out.prntln("The number value of 5 s " + fve); System.out.prntln("Is 6 a dgt? " + Character.sDgt('6')); System.out.prntln("Is q a letter? " + Character.sLetter('q')); System.out.prntln("Is z lowercase? " + Character.sLowerCase('z')); System.out.prntln("Is R uppercase? " + Character.sUpperCase('R')); System.out.prntln("Lowercase Z s " + Character.toLowerCase('Z')); System.out.prntln("Uppercase a s " + Character.toUpperCase('a')); 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 28 Cumulatve Text Algorthms Smlar to our numerc cumulatve algorthms. // Echos the lne of text, alternatng captal case of the nput. Scanner n = new Scanner(System.n); System.out.prnt("Enter a lne of text: "); Strng lne = n.nextlne(); Strng result = ""; for (nt = 0; < lne.length(); ++) { char letter = lne.charat(); f ( % 2 == 0) { letter = Character.toUpperCase(letter); result += letter; System.out.prntln(result); 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 29 System.out.prntf An alternatve to prntln/prnt that lets you format the output. System.out.prntf("Example of prntf: %d %.2f %s", 12, 1.241, "Chowder"); > Example of prntf: 12 1.24 Chowder Specfers have the form: %<formattng><type> Need to provde one argument per format specfer. They are consumed n-order. A format specfer. A placeholder for a thng to prnt. See p. 260 of the book for more nformaton about format specfers. 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 30 CIS 110 (11fa) - Unversty of Pennsylvana 5
System.out.prntf (example) Scanner n = new Scanner(System.n); for (nt = 0; < 3; ++) { System.out.prnt("Enter the employee's name: "); Strng name = n.nextlne(); System.out.prnt("Enter the employee's salary: "); double salary = n.nextdouble(); System.out.prntf("%10s -> %6.2f\n", name, salary); n.nextlne();// flush the buffer Tacklng Program Problems Enter the employee's name: Joe Enter the employee's salary: 1410.14 Joe -> 1410.14 Enter the employee's name: Lamont Enter the employee's salary: 4104.41 Lamont -> 4104.41 Enter the employee's name: Fred Enter the employee's salary: 10041.4190482 Fred -> 10041.42 See PalndromeChecker.java 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 31 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 32 Sample Problem Problem: wrte a program that reads n a Strng from the user, checks to see f that Strng s a palndrome, and nforms the user of the results of the check. Example output > Enter a strng to check: > abba > The reverse of the lne s: abba > The lne s a palndrome! Our Methodology 1. Try some example nputs to get a feel for the problem. 2. Start wth a skeleton of the soluton. 3. Decompose the problem nto sub-problems. 4. Make helper methods to solve the subproblems. 5. Use those helper methods to solve your man problem. 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 33 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 34 CIS 110 (11fa) - Unversty of Pennsylvana 6