clicker 1
play

Clicker 1 loops, figures, constants What is the base 10 equivalent - PowerPoint PPT Presentation

Topic 6 Clicker 1 loops, figures, constants What is the base 10 equivalent of the base 2 "Complexity has and will maintain a strong fascination for number 1011001 many people. It is true that we live in a complex world and strive to solve


  1. Topic 6 Clicker 1 loops, figures, constants What is the base 10 equivalent of the base 2 "Complexity has and will maintain a strong fascination for number 1011001 many people. It is true that we live in a complex world and strive to solve inherently complex problems, which often do A.27 require complex mechanisms. However, this should not B.89 diminish our desire for elegant solutions, which convince by their clarity and effectiveness. Simple, elegant solutions are C.93 more effective, but they are harder to find than complex ones, and they require more time, which we too often believe D.127 to be unaffordable." E.1011001 -Niklaus Wirth Based on slides bu Marty Stepp and Stuart Reges from http://www.buildingjavaprograms.com/ 1 2 Clicker 2 Clicker 3 What does 5! equal? Which of the following is closest to the value that overflows the Java int data type when A. 5 calculating N! B. 32 A. 1 C. 120 B. 15 D. 3125 C. 60 E. a lot D. 100 E. 1000 3 4

  2. Drawing complex figures Development strategy Use nested for loops to produce the Recommendations for managing complexity: following output. 1. Design the program (think about steps or methods needed). write an English description of steps required Why draw ASCII art? use this description to decide the methods Real graphics require more finesse #================# #================# 2. Create a table for patterns of characters ASCII art has complex patterns | <><> | | <><> | use tables to write your for loops Can focus on the algorithms | <>....<> | | <>....<> | | <>........<> | | <>........<> | |<>............<>| |<>............<>| |<>............<>| |<>............<>| | <>........<> | | <>........<> | | <>....<> | | <>....<> | | <><> | | <><> | 5 6 #================# #================# 1. Pseudo-code Pseudo-code algorithm pseudo-code : An English description of an #================# 1. Line | <><> | algorithm. # , 16 = , # | <>....<> | | <>........<> | 2. Top half |<>............<>| | Example: Drawing a 12 wide by 7 tall box of |<>............<>| spaces (decreasing) | <>........<> | stars <> ************ | <>....<> | dots (increasing) * * | <><> | <> * * print 12 stars. * * spaces (same as above) #================# for (each of 5 lines) { * * | * * print a star. 3. Bottom half (top half upside-down) ************ print 10 spaces. 4. Line print a star. } # , 16 = , # print 12 stars. 7 8

  3. Methods from pseudocode 2. Tables public class Mirror { A table for the top half: public static void main(String[] args) { line(); Compute spaces and dots expressions from line topHalf(); bottomHalf(); number line(); } public static void topHalf() { line line spaces spaces -2 * line + 8 dots dots 4 * line - 4 for (int line = 1; line <= 4; line++) { #================# // contents of each line | <><> | } 1 1 6 6 6 0 0 0 } | <>....<> | 2 2 4 4 4 4 4 4 | <>........<> | public static void bottomHalf() { for (int line = 1; line <= 4; line++) { |<>............<>| 3 3 2 2 2 8 8 8 // contents of each line |<>............<>| } 4 4 0 0 0 12 12 12 } | <>........<> | | <>....<> | public static void line() { // ... | <><> | } #================# } 9 10 Partial solution 3. Writing the code // Prints the expanding pattern of <> for // the top half of the figure. Useful questions about the top half: public static void topHalf() { for (int line = 1; line <= 4; line++) { What methods? (think structure and redundancy) System.out.print("|"); for (int space = 1; space <= (line * -2 + 8) ; space++) { Number of (nested) loops per line? System.out.print(" "); } #================# System.out.print("<>"); | <><> | for (int dot = 1; dot <= (line * 4 - 4) ; dot++) { | <>....<> | System.out.print("."); } | <>........<> | |<>............<>| System.out.print("<>"); |<>............<>| for (int space = 1; space <= (line * -2 + 8) ; space++) { System.out.print(" "); | <>........<> | } | <>....<> | System.out.println("|"); | <><> | } #================# } 11 12

  4. Scaling the mirror Modify the Mirror program so that it can scale. Class constants The current mirror (left) is at size 4; the right is at size 3. and scope We'd like to structure the code so we can scale the figure by changing the code in just one place. reading: 2.4 #================# #============# | <><> | | <><> | | <>....<> | | <>....<> | |<>........<>| | <>........<> | |<>........<>| |<>............<>| | <>....<> | |<>............<>| | <>........<> | | <><> | | <>....<> | #============# | <><> | #================# 13 14 Scope Limitations of variables scope : The part of a program where a Idea: Make a variable to represent the size. variable exists. Use the variable's value in the methods. Problem: A variable in one method can't be seen in From its declaration to the end of the { } braces others. A variable declared in a for loop exists only in that public static void main(String[] args) { loop. int size = 4; topHalf(); A variable declared in a method exists only in that printBottom(); method. } public static void topHalf() { public static void example() { i 's scope for (int i = 1; i <= size ; i++) { // ERROR: size not found int x = 3; ... for (int i = 1; i <= 10; i++) { x 's scope } } System.out.println(x); public static void bottomHalf() { } for (int i = size ; i >= 1; i--) { // ERROR: size not found // i no longer exists here ... } // x ceases to exist here } } 15 16

  5. Scope implications Class constants Variables whose scope does NOT overlap class constant : A fixed value visible to the whole program. can have same name. value can be set only at declaration; cannot be reassigned, hence the name: constant for (int i = 1; i <= 100; i++) { System.out.print("/"); Syntax: } for ( int i = 1 ; i <= 100; i++) { // OK public static final <type> <name> = <exp> ; System.out.print("\\"); } name in ALL_UPPER_CASE by convention int i = 5; // OK: outside of loop's scope A variable can't be declared twice or used Examples: out of its scope. public static final int DAYS_IN_WEEK = 7; for ( int i = 1 ; i <= 100 * line; i++) { public static final double INTEREST_RATE = 0.5; int i = 2; // ERROR: overlapping scope public static final int SSN = 658234569; System.out.print("/"); } i = 4; // ERROR: outside scope 17 18 Constants and figures Repetitive figure code public class Sign { Consider the task of drawing the following public static void main(String[] args) { drawLine(); scalable figure: drawBody(); drawLine(); } +/\/\/\/\/\/\/\/\/\/\+ public static void drawLine() { | | System.out.print("+"); | | for (int i = 1; i <= 10 ; i++) { Multiples of 5 occur many times | | System.out.print("/\\"); | | } System.out.println("+"); | | } +/\/\/\/\/\/\/\/\/\/\+ public static void drawBody() { for (int line = 1; line <= 5 ; line++) { +/\/\/\/\+ System.out.print("|"); for (int spaces = 1; spaces <= 20 ; spaces++) { | | System.out.print(" "); | | The same figure at size 2 } +/\/\/\/\+ System.out.println("|"); } } 19 20 }

Recommend


More recommend