Review Scope public class Scope Demo public static

Review: Scope public class Scope. Demo { public static final int SIZE = 7; public static void main(String[] args) { triangle. Top(); triangle. Bot(); } public static void triangle. Top() { char c = '\'; for (int row = 1; row <= SIZE; row++) { for (int col = 1; col <= row; col++) { System. out. print(c); } System. out. println(); } } } public static void triangle. Bot() { for (int col = 1; col <= SIZE; col++) { System. out. print('-'); } System. out. println(); } class scope method scope loop scope inner loop scope
![Review: Method overloading public class Blocks { public static void main(String[] args) { draw. Review: Method overloading public class Blocks { public static void main(String[] args) { draw.](http://slidetodoc.com/presentation_image_h2/20d51a6c1042515322b4e663c22f3eca/image-2.jpg)
Review: Method overloading public class Blocks { public static void main(String[] args) { draw. Block(1); draw. Block('+'); draw. Block(2); draw. Block('='); draw. Block(3); } public static void draw. Block() { draw. Block(' '); } public static void draw. Block(char c) { System. out. println("+-+"); System. out. println("|" + c + "|"); System. out. println("+-+"); } } public static void draw. Block(int i) { draw. Block((char)('0'+i)); } +-+ |1| +-+ |+| +-+ |2| +-+ |=| +-+ |3| +-+

Java has another output method: System. out. printf() that can succinctly specify output formatting. double x = 1. 2345; double y = 2. 5678; System. out. printf("[%3. 1 f, %3. 1 f]", x, y); output: [1. 2, 2. 6]

Examples of printf format specifiers: Type int Specifier Example Output %d 123 %5 d 123 %-5 d 123 double %f %8. 1 f %8. 4 f %-8. 2 f 1. 2300 1. 23 String %s hello %10 s hello %-5. 4 s hell General form: %(flags)(width)(. precision)specifier

Example: Improving the investment calculator Original using tabs: System. out. println(year + "t" + round(starting. Balance) + "t" + deposit + "t" + round(interest) + "t" + round(balance)); starting ending year balance deposit 1 1000. 0 50. 0 2 1050. 0 100. 0 57. 5 3 1207. 5 100. 0 65. 38 intrest 1050. 0 1207. 5 1372. 88 balance Improved using printf: System. out. printf("%4 d %8. 2 fn", year, starting. Balance, deposit, interest, balance); year 1 2 3 starting balance deposit interest 1000. 00 50. 00 100. 00 57. 50 1207. 50 100. 00 65. 38 ending balance 1050. 00 1207. 50 1372. 88

Activity: Improving the Energy Audit using printf What formats would you use? // print results System. out. println("Computer Energy Audit: "); System. out. println(“Used for "+usage. Hours. Per. Year+" hours per year"); System. out. println("It will use "+ usage. KWHPer. Year+ " KWH/year"); System. out. println("Which will cost " + cost. Per. Year + " $/year"); System. out. println(“And produce "+ lbs. CO 2 Per. Year + " lbs of CO 2"); Computer Energy Audit: Used for 480. 0 hours per year It will use 18. 72 KWH/year Which will cost 2. 71439999995 $/year And produce 11. 01016799998 lbs of CO 2 ß print as 480 ß print as 18. 7 ß print as 2. 71 ß print as 11. 0 %(flags)(width)(. precision)specifier

// print results System. out. println("Computer Energy Audit: "); System. out. println(“Used for "+usage. Hours. Per. Year+" hours per year"); System. out. println("It will use "+ usage. KWHPer. Year+ " KWH/year"); System. out. println("Which will cost " + cost. Per. Year + " $/year"); System. out. println(“And produce "+ lbs. CO 2 Per. Year + " lbs of CO 2"); Computer Energy Audit: Used for 480. 0 hours per year It will use 18. 72 KWH/year Which will cost 2. 71439999995 $/year And produce 11. 01016799998 lbs of CO 2 ß print as 480 ß print as 18. 7 ß print as 2. 71 ß print as 11. 0 // print results System. out. println("Computer Energy Audit: "); System. out. printf("Used for %. 0 f hours per yearn", usage. Hours. Per. Year); System. out. printf("It will use %. 1 f KWH/yearn", usage. KWHPer. Year); System. out. printf("Which will cost %. 2 f $/yearn", cost. Per. Year); System. out. printf("And produce %. 1 f lbs of CO 2n", lbs. CO 2 Per. Year); Computer Energy Audit: Used for 480 hours per year It will use 18. 7 KWH/year Which will cost 2. 71 $/year And produce 11. 0 lbs of CO 2

Activity: Improving the histogram using printf // print histogram for (int i = 3; i <= 18; i++) { System. out. print("["+i+"]t"); System. out. printf("[%2 d] ", i); for (int j = 0; j < counts[i]; j += 100) System. out. print("*"); System. out. println(" "+counts[i]); } output: [3] [ 3] ** ** 122157 [4] [ 4] **** 391394 [5] [ 5] ********* 864824 [6] [ 6] *************** 13581424 [7] [ 7] ********************** 20682155 [8] [ 8] ****************************** 2914 2919 [9] [ 9] *********************************** 3477 3452 [10] ********************3818 3783 [11] ********************3806 3731 [12] *********************************** 3455 3409 [13] ***************2936 2898 [14] ********************* 2053 2074 [15] ************** 1361 1357 [16] ********* 854891 [17] *****406 396 [18] ** ** 117136

In java: System. in represents the input stream of characters coming from the keyboard. We could read it character by character using int c = System. in. read(); To make input easier we create a Scanner to help us process System. in Scanner scnr = new Scanner(System. in); String s = scnr. next(); int i = scnr. next. Int(); . . . Likewise we can use a Scanner to process the information in a String or a file. When done, we could call System. in. close(); // but no one does that for the keyboard

Scanner on a String (book’s example modified) String user. Info = "Amy Smith 19"; Scanner str. Scnr = new Scanner(user. Info); // Scanner of String // Parse name and age values from string String first. Name = str. Scnr. next(); String last. Name = str. Scnr. next(); int user. Age = str. Scnr. next. Int(); We will see a real example next lecture with Line Based Processing when we read files line by line and then further re-process each line.

In java: System. out represents the output stream of characters going to the console We could write it character by character using System. out. write(c); To make output easier System. out is already a Print. Writer object System. out. print("Hello, "); System. out. println("my name is " + name); System. out. printf("I am %d years oldn", age); . . . Likewise we can use a Print. Writer to output to a String or a file. When done, we could call System. out. close(); // but no one does that for the console

Book’s example (modified) String. Writer str. Writer = new String. Writer(); // for building string Print. Writer print. Writer = new Print. Writer(str. Writer); // for print. . . // Create formatted string print. Writer. print(last. Name); print. Writer. print(", "); print. Writer. print(first. Name); print. Writer. printf(" %d", age); if (age < 21) print. Writer. print(" (minor)"); String s = str. Writer. to. String(); Simpson Bart 12 "Simpson, Bart 12 (minor)" WHY BOTHER? There are easier ways to make a string. String s = last. Name + ", " + first. Name + " " + age + (age < 21 ? " (minor)" : ""); However we will be using a print. Writer for creating files.

Programming Challenge: Draw stacks of objects
- Slides: 13